May 9, 2020

Powershell Script to Drop Logins across multile servers.







[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO') | out-null



$Error.Clear()



cls







$servers = Get-Content E\LoginNames\Servers.txt



$logins = @(login name to be dropped)







foreach($server in $servers)



{



$srv = New-Object ('Microsoft.SqlServer.Management.Smo.Server') $server







#drop database users



foreach($database in $srv.Databases)



{



foreach($login in $logins)



{



if($database.Users.Contains($login))



{



$database.Users[$login].Drop();



}



}



}







#drop server logins



foreach($login in $logins)



{



if ($srv.Logins.Contains($login))



{



$srv.Logins[$login].Drop();



}



}



}







$FilePath = "E:\LoginNames\"



$OutFile = Join-Path -path $FilePath -childPath ("logins_" + (get-date).toString('yyyyMMdd_hhmmtt') + ".log")






No comments:

Post a Comment

If you have any doubt or question, please contact us.