Avaryan

Set-AutoLogon.ps1

Aug 10th, 2016
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Set-AutLogon.ps1
  2. # This script does the following:
  3. # - Force logoff all users.
  4. # - Edits AutoLogon registry keys with specified input.
  5. # - Restarts the computer.
  6. #
  7. # The supplied password is stored in plain text within the Registry; however,
  8. # this value is delete once the specified number of logons has occurred.
  9. #
  10. # Tested with PowerShell 4.0 on Windows 7 Enterprise.
  11.  
  12.     Param(
  13.         [parameter(Mandatory=$true)]
  14.         [String[]]
  15.         $ComputerID,
  16.  
  17.         [parameter(Mandatory=$true)]
  18.         [String]
  19.         $Username,
  20.  
  21.         [parameter(Mandatory=$true)]
  22.         [String]
  23.         $Password,
  24.  
  25.         [parameter(Mandatory=$false)]
  26.         [Int]
  27.         $TimesToLogin = 1
  28.     )
  29.  
  30.     $credentials = Get-Credential
  31.  
  32.     ForEach ($computer in $ComputerID) {
  33.  
  34.         Invoke-Command -ComputerName $computer -Credential $credentials -ScriptBlock {
  35.             Param($Times, $User, $Pass)
  36.            
  37.             $shutdown = $env:SystemRoot + "\System32\shutdown.exe"
  38.             Start-Process -FilePath $shutdown -ArgumentList '-l -f' -Wait
  39.  
  40.             $path = 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon'
  41.             Set-ItemProperty -Path $path -Name AutoAdminLogon -Value 1
  42.             Set-ItemProperty -Path $path -Name DefaultUserName -Value $User
  43.            
  44.             try {
  45.                 if (-Not (Get-ItemProperty -Path $path | Select-Object -ExpandProperty "DefaultPassword" -ErrorAction SilentlyContinue )) {
  46.                     New-ItemProperty -Path $path -Name DefaultPassword -Value $Pass
  47.                 }
  48.            
  49.             } catch {
  50.                 New-ItemProperty -Path $path -Name DefaultPassword -Value $Pass
  51.             }
  52.  
  53.             Set-ItemProperty -Path $path -Name DefaultPassword -Value $Pass
  54.  
  55.             try {
  56.                 if (-Not (Get-ItemProperty -Path $path | Select-Object -ExpandProperty "AutoLogonCount" -ErrorAction SilentlyContinue )) {
  57.                     New-ItemProperty -Path $path -Name AutoLogonCount -Value $Times
  58.                 }
  59.            
  60.             } catch {
  61.                 New-ItemProperty -Path $path -Name AutoLogonCount -Value $Times
  62.             }
  63.  
  64.             Set-ItemProperty -Path $path -Name AutoLogonCount -Value $Times
  65.            
  66.             Restart-Computer -Force
  67.            
  68.         } -ArgumentList $TimesToLogin,$Username,$Password
  69.     }
  70.  
  71. # Examples:
  72. # Specify a single Hostname or IP address. This example will AutoLogon 1 time with the specified account.
  73. # ./Set-AutoLogon.ps1 -ComputerID 10.20.20.105 -Username "Domain\Username" -Password "myPassword" -TimesToLogin 1
  74. #
  75. # Specify a range of either Hostnames or IP address by importing from a CSV file.
  76. # The '.Hosts' listed below references the column heading in the CSV file.
  77. # ./Set-AutoLogon.ps1 -ComputerID $( (Import-Csv -Path "C:\hosts.csv").Hosts ) -Username "Domain\Username" -Password "myPassword"
Add Comment
Please, Sign In to add comment