Quantcast
Channel: The Official Scripting Guys Forum! forum
Viewing all articles
Browse latest Browse all 15028

Powershell to remove users from Local Administrators group

$
0
0

Friends, 

I need to remove user accounts from the local administrators group on 500+ servers. The servers & accounts are on multiple domains & forests. I'd rather not have to log into each box individually for this. I was planning to use powershell and came across the script below. Can someone tell me what i need to tweak in the script and source.txt files for this to work in a multi-domain & forests? 

https://gallery.technet.microsoft.com/scriptcenter/Remove-Users-From-Local-bd4abe33

if (Test-Path "C:\Scripts\SonuclarRemove.txt") 
{ Remove-Item "C:\Scripts\SonuclarRemove.txt" -Force 
Write-Host "C:\Scripts\SonuclarRemove.txt found and deleted" -ForegroundColor White -BackgroundColor Red 
} if (Test-Path "C:\Scripts\RemoveGroupHostunreachable.txt") 
{ Remove-Item "C:\Scripts\RemoveGroupHostunreachable.txt" -Force 
Write-Host "C:\Scripts\RemoveGroupHostunreachable.txt found and deleted" -ForegroundColor White -BackgroundColor Red 
} $servers=gc "C:\Scripts\Servers.txt" foreach ($srv in $servers)  
{  $starttime=get-date 
Write-Host "Starting Control for Computer $srv at $starttime `n" -ForegroundColor Cyan -BackgroundColor DarkBlue | out-default $pingStatus = Get-WmiObject -Query "Select * from win32_PingStatus where Address='$srv'" if($pingStatus.StatusCode -eq 0)  
{ $users=gc "C:\Scripts\users.txt" foreach ($usr in $users)  
{ 
Write-Host "Ping Control for Computer $srv at $starttime successfully. Now connecting PSEXEC `n" -ForegroundColor Cyan -BackgroundColor DarkBlue | out-default 
C:\Scripts\PsExec.exe \\$srv net localgroup administrators COZUMPARK\$usr /delete $stoptime=get-date $rapor="$srv;$usr removed from local admin group;$stoptime" 
Write-Host $rapor `n $rapor | Out-File "C:\Scripts\SonuclarRemove.txt" -Append 
} 
Write-Host "Successfully finished user remove from local admin for Computer $srv at $stoptime `n" -ForegroundColor Cyan -BackgroundColor DarkBlue | out-default 
} else { 
Write-Host "Ping Control for Computer $srv at $starttime failed. `n" -ForegroundColor Cyan -BackgroundColor DarkBlue | out-default $srv | Out-File "C:\Scripts\RemoveGroupHostunreachable.txt" -Append 
} if (Test-Path "C:\Scripts\SonuclarRemove.txt") 
{ $sonuc=gc "C:\Scripts\SonuclarRemove.txt" 
Write-Host "#########################  SUCCESSFULLY CONNECTED AND USER REMOVED FROM LOCAL ADMINISTRATORS GROUP SYSTEMS  #################################" 
Write-Host "======================================================================================================================" foreach ($item in $sonuc)  
{  
Write-Host $item 
} 
} if (Test-Path "C:\Scripts\RemoveGroupHostunreachable.txt") 
{ $sonuc1=gc "C:\Scripts\RemoveGroupHostunreachable.txt" 
Write-Host "#########################  CONNECTION FAILED SYSTEMS  #################################" 
Write-Host "======================================================================================================================" foreach ($item1 in $sonuc1)  
{  
Write-Host $item1 
} 
} 
}


Viewing all articles
Browse latest Browse all 15028

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>