######################################################################################### # # Ownership taker # ######################################################################################### # this script can be use to take the ownership of an account # specify your name in the list of $list_of_known_users with your autologonname (put your login name if you don't know your Pbkdf2 Hash) # and put the name of your object in the variable $own_user_objectName # then execute the script, specifiy the targeted user and the targeted database(s) # if you want to revert back the configuration, replace the variable $own_user_objectName with the name of the object of the person you took the ownership ######################################################################################### # # Database Value # ######################################################################################### #other #$DBInstance = 'DB NAME'# #$DBACredential = Get-Credential -UserName pgadmin -Message "Credentials for $DBInstance" #DBPassword = $DBACredential.GetNetworkCredential().Password #$DBUsername = $DBACredential.UserName #Database=$database $ConnectionString = "Server=$DBInstance;User Id=$DBUsername;Password=$DBPassword;TrustServerCertificate=True" $VerbosePreference = 'Continue' ######################################################################################### # # User value # ######################################################################################### $list_of_known_users = [ordered]@{ Admin = [PSCustomObject]@{ "Name"="Admin" "username"="admin" "autologonname"="" }; Administrator = [PSCustomObject]@{ "Name"="Administrator" "username"="administrator" "autologonname"="" }; Anastasia = [PSCustomObject]@{ "Name"="Anastasia" "username"="akroupkina" "autologonname"="" }; Henry = [PSCustomObject]@{ "Name"="Henry" "username"="hchung" "autologonname"="" }; Frans = [PSCustomObject]@{ "Name"="Frans" "username"="frans" "autologonname"="" }; Blake = [PSCustomObject]@{ "Name"="Blake" "username"="bparrish" "autologonname"="" }; Besfort = [PSCustomObject]@{ "Name"="Besfort" "username"="bhyseni" "autologonname"="" }; Kyle = [PSCustomObject]@{ "Name"="Kyle" "username"="kpatel" "autologonname"="" }; Pierre = [PSCustomObject]@{ "Name"="Pierre" "username"="pierretotal" "autologonname"="" } } ######################################################################################### # Fill with your user object name ######################################################################################### $own_user_objectName = 'Pierre' $ownAutologonname = $list_of_known_users.$own_user_objectName.autologonname #get the target $targetObjectName = $list_of_known_users.keys |Out-GridView -PassThru -Title "Select the target" $targetName = $list_of_known_users.$targetObjectName.Name $targetUserName = $list_of_known_users.$targetObjectName.Username Write-output "You selected the user $targetName to take ownership " $QuerySelectDatabases= "SELECT name FROM sys.databases order by name" $targeted_DB= Invoke-Sqlcmd -ConnectionString $ConnectionString -Query $QuerySelectDatabases |Out-GridView -PassThru -Title "Select the targeted databases" foreach( $database in $targeted_DB ) { $DB_name = $database.Name write-output "updating $DB_name" $queryVerifyPresenceUser = " SELECT CASE WHEN EXISTS ( select pk_userid,username, createcomputer , firstname, lastname , autologonname, isactive from $DB_name.dbo.tblUser where username = '$targetUserName' ) THEN 'TRUE' ELSE 'FALSE' END " write-output $queryVerifyPresenceUser $boolean_presence_users = Invoke-Sqlcmd -ConnectionString $ConnectionString -Query $queryVerifyPresenceUser write-verbose $boolean_presence_users.Column1 if($boolean_presence_users.Column1 -eq "FALSE"){ write-output "the user doesn't exist in the following database : $DB_name" continue } $QueryUpdatingUser= " update $DB_name.dbo.tblUser set isactive = 1, autologonname = '$ownAutologonname' where username = '$targetUserName'; " Write-Output "Query to execute :" Write-Output $QueryUpdatingUser Write-Warning "Are you sure to proceed ?" -WarningAction Inquire Invoke-Sqlcmd -ConnectionString $ConnectionString -Query $QueryUpdatingUser }