Files
Usefull_scripts/OwnershipTaker/CLI/ownership_taker.ps1
T
2024-07-17 07:54:44 +00:00

164 lines
5.5 KiB
PowerShell

#########################################################################################
#
# 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
#
#########################################################################################
####acceptance
$DBInstance = 'tot-2020-sql-acceptance.privatelink.03ddfc49f186.database.windows.net'#
$DBPassword = '6^_przWQ$?Ve7,n9'
$DBUsername = 'pgadmin'
###production
#$DBInstance = 'tot-2020-sql-production.privatelink.03ddfc49f186.database.windows.net'
#$DBPassword = 'zWdtg7oCZrAmTe1ec9fSv5OJ'
#$DBUsername = 'pgadmin'
###production
#$DBInstance = 'rosercloud-rpsql02.cy4z2xpdf3he.eu-central-1.rds.amazonaws.com'
#$DBUsername = 'admin'
#$DBPassword = 'wQNMJg3qgMCcU3e05Rsy'
#other
#$DBInstance = 'roserdb01.cy4z2xpdf3he.eu-central-1.rds.amazonaws.com'#
#$DBACredential = Get-Credential -UserName pgadmin -Message "Credentials for $DBInstance"
#$DBUsername = 'pgadmin'
#DBPassword = $DBACredential.GetNetworkCredential().Password
#$DBUsername = $DBACredential.UserName
#$DBUsername = 'pgadmin'
#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"="9mBIVk9rp6tQ9BFMbz21YQ=="
};
Henry = [PSCustomObject]@{
"Name"="Henry"
"username"="hchung"
"autologonname"="DW+WmQw34KPOJb2Ty9+DSg=="
};
Frans = [PSCustomObject]@{
"Name"="Frans"
"username"="fhaarman"
"autologonname"="Y2vW6jgau0cnKls57Bht7Q=="
};
Blake = [PSCustomObject]@{
"Name"="Blake"
"username"="bparrish"
"autologonname"="bparrish"
};
Besfort = [PSCustomObject]@{
"Name"="Besfort"
"username"="bhyseni"
"autologonname"="ExEghFzbxMdTNJ/Ygo/N3A=="
};
Kyle = [PSCustomObject]@{
"Name"="Kyle"
"username"="kpatel"
"autologonname"="LzD0rK0qkyaSGo6lfnuB2Q=="
};
Pierre = [PSCustomObject]@{
"Name"="Pierre"
"username"="pierretotal"
"autologonname"="/jLXczx+RxoY7ECZfyhy2A=="
}
}
#########################################################################################
# Fill with your user object name
#########################################################################################
$own_user_objectName = 'Besfort'
$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
}