cosine83

Local Admin Update

Jul 4th, 2014
949
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Import-Module activedirectory
  2.  
  3. $ErrorActionPreference = "Stop"
  4.  
  5. $searchbase = "OU=Testing,DC=domain,DC=com"
  6.  
  7. $query = Get-ADComputer -Filter * -SearchBase $searchbase | Select Name | Sort Name
  8. $computers = $query.Name
  9.  
  10. $LocalAdminPassword = Read-Host -AsSecureString "Please set the new local admin password"
  11. $LocalUserPassword = Read-Host -AsSecureString "Please set the new local user password"
  12. $LocalAdminPW = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($LocalAdminPassword))
  13. $LocalUserPW = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($LocalUserPassword))
  14.  
  15. foreach ($computer in $computers)
  16. {
  17.     Write-Host "Connecting..."
  18.     If(Test-Connection $computer -Count 1 -Quiet) #Check that PC is online
  19.     {
  20.         If(Test-WSMan $computer) #PC is online so check if WinRM enabled
  21.         {
  22.             #ADSI fallback in case WinRM is unavailable
  23.             Write-Host "WinRM unavailable, using ADSI..."
  24.             Write-Host "Changing local admin password on $computer!"
  25.             $LocalAdmin = [adsi]"WinNT://$computer/Administrator,user"
  26.             $LocalAdmin.SetPassword($LocalAdminPW)
  27.             $LocalAdmin.SetInfo()
  28.             $LocalAdmin.InvokeSet("UserFlags",($LocalAdmin.UserFlags[0] -BOR 2 + 65536))
  29.             $LocalAdmin.SetInfo()
  30.             Write-Host "Local admin password is set and account disabled!"
  31.             $LocalUser = [adsi]"WinNT://$computer/AG,user"
  32.             If($LocalUser -eq $Null)
  33.             {
  34.             Write-Host "Creating new local admin user!"
  35.             $NewUser = "AG"
  36.             $LocalComputer = [adsi]"WinNT://$computer,computer"
  37.             $CreateLocalUser = $LocalComputer.Create('User', $NewUser)
  38.             $CreateLocalUser.SetPassword($LocalUserPW)
  39.             $CreateLocalUser.SetInfo()
  40.             $LocalUser.InvokeSet("UserFlags",($LocalUser.UserFlags[0] -BXOR 2 -BOR 65536))
  41.             $LocalUser.SetInfo()
  42.             ([adsi]"WinNT://$computer/Administrators,group").Add("WinNT://$computer/AG")
  43.             Write-Host "New user created, password set, and added to local admin group!"
  44.             }
  45.             Else
  46.             {
  47.             Write-Host "Local user already exists, setting password and user properties!"
  48.             $LocalUser.SetPassword($LocalUserPW)
  49.             $LocalUser.InvokeSet("UserFlags",($LocalUser.UserFlags[0] -BXOR 2 -BOR 65536))
  50.             $LocalUser.SetInfo()
  51.             Write-Host "Local user modified and password set!"
  52.             }
  53.         }
  54.         else
  55.         {
  56.             #WinRM is enabled
  57.             Write-Host "WinRM is enabled on $computer, proceeding..."
  58.             Write-Host "Changing local admin password on $computer!"
  59.             #This Invoke-Command script block must be all on a single line or it will not work
  60.             Invoke-command -ComputerName $computer -Command { net user administrator $using:LocalAdminPW /active:no /passwordchg:no /expires:never }
  61.             Write-Host "Local admin password is set and account disabled!"
  62.             Write-Host "Creating new local admin user!"
  63.             Invoke-command -ComputerName $computer -Command { net user ag $using:LocalUserPW /add /active:yes /passwordchg:no /expires:never }
  64.             Invoke-command -ComputerName $computer -Command { net localgroup administrators $computer\ag /add }
  65.             Write-Host "New user created, password set, and added to local admin group!"
  66.         }
  67.     }
  68.     else
  69.     {
  70.     Write-Host -ForegroundColor Red "Can't connect to $computer!"
  71.     }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment