cosine83

Admin Mod

Sep 1st, 2014
364
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Import-Module activedirectory
  2.  
  3. $ErrorActionPreference = "Continue"
  4.  
  5. Write-Host "Checking if log folder exists and making it if not"
  6. $Path = "C:\PowerShell Logs"
  7. If (!(Test-Path -Path $Path ))
  8. {
  9.     New-Item -ItemType Directory -Path $Path
  10. }  
  11. Else
  12. {
  13.     Write-Host "Folder exists, proceeding with script execution"
  14. }  
  15.  
  16. $Date = Get-Date -format MM.dd.yyyy
  17.  
  18. $Comp = Read-Host "Please define a computer"
  19. $OU = Read-Host "Please define an OU"
  20. $askNoGPI = Read-Host "Are the computers under the No GP Inheritance OU? (y/n)"
  21.  
  22. $baseAD = "OU=Computers,OU=Test,DC=domain,DC=com"
  23. $Date = Get-Date -format MM.dd.yyyy
  24.  
  25. $Comp = Read-Host "Please define a computer"
  26. $OU = Read-Host "Please define an OU"
  27. $askNoGPI = Read-Host "Are the computers under the No GP Inheritance OU? (y/n)"
  28.  
  29. $searchbase = $baseAD
  30.  
  31. if ($askNoGPI -eq 'y') { $searchbase = "OU=No GP Inheritence,$searchbase" }
  32. if ($OU) { $searchbase = "OU=$OU,$searchbase" }
  33. if ($Comp) { $searchbase = "CN=$Comp,$searchbase" }
  34.  
  35. $query = Get-ADComputer -Filter * -SearchBase $searchbase | Select Name | Sort Name
  36. $computers = $query.Name
  37.  
  38. $LocalAdminPassword = Read-Host -AsSecureString "Please set the new local admin password"
  39. $LocalUserPassword = Read-Host -AsSecureString "Please set the new local user password"
  40. $LocalAdminPW = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($LocalAdminPassword))
  41. $LocalUserPW = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($LocalUserPassword))
  42.  
  43. foreach ($computer in $computers)
  44. {
  45.     Write-Host "Connecting..."
  46.     If(Test-Connection $computer -Count 1 -Quiet) #Check that PC is online
  47.     {
  48.         Write-Host "Changing local admin password on $computer"
  49.         $LocalAdmin = [adsi]"WinNT://$computer/Administrator,user"
  50.         $LocalAdmin.SetPassword($LocalAdminPW)
  51.         $LocalAdmin.SetInfo()
  52.         $LocalAdmin.InvokeSet("UserFlags",($LocalAdmin.UserFlags[0] -BOR 2))
  53.         $LocalAdmin.SetInfo()
  54.         Write-Host "Local admin password is set and account disabled"
  55.         $LocalComputer = ([adsi]"WinNT://$computer,computer")
  56.         $LocalComputerUsers = ($LocalComputer.psbase.children | Where-Object {$_.psBase.schemaClassName -eq "User"} | Select-Object -expand Name)
  57.         $LCUFound = $LocalComputerUsers -contains "AG"
  58.         $LocalUser = [adsi]"WinNT://$computer/AG,user"
  59.         If($LCUFound)
  60.         {
  61.         Write-Host "Local user already exists, setting password and user properties"
  62.         $LocalUser.SetPassword($LocalUserPW)
  63.         $LocalUser.InvokeSet("UserFlags",($LocalUser.UserFlags[0] -BXOR 2))
  64.         $LocalUser.SetInfo()
  65.         Write-Host "Local user modified and password set"      
  66.         }
  67.         Else
  68.         {
  69.         Write-Host "New local admin does not exist, creating"
  70.         $NewUser = "AG"
  71.         $CreateLocalUser = $LocalComputer.Create('User', $NewUser)
  72.         $CreateLocalUser.SetPassword($LocalUserPW)
  73.         $CreateLocalUser.SetInfo()
  74.         $LocalUser.InvokeSet("UserFlags",($LocalUser.UserFlags[0] -BXOR 2))
  75.         $LocalUser.SetInfo()
  76.         ([adsi]"WinNT://$computer/Administrators,group").Add("WinNT://$computer/AG")
  77.         Write-Host "New user created, password set, and added to local admin group"
  78.         }
  79.     }
  80.     else
  81.     {
  82.         $offline = $computer | Out-File -Append -noClobber -filePath "C:\PowerShell Logs\Offline Computers $Date.csv" -width 20
  83.         Write-Host -ForegroundColor Red "Can't connect to $computer"
  84.         $offline
  85.         Write-Host "Added to offline log file for $Date"
  86.     }
  87. }
  88. Write-Host "All offline computers have been written to a log file"
  89. Write-Host "Opening log file..."
  90. Invoke-Item "C:\PowerShell Logs\Offline Computers $Date.csv"
Advertisement
Add Comment
Please, Sign In to add comment