2014年6月16日 星期一

Auto logon(1):Create auto logon with WMI by using Power Shell Script


Step1、將以下的指令寫入文字編輯器,並存成"autologon.ps1

######宣告變數,$computer也可以為$computer="localhost
param (
  [Parameter(Mandatory=$false)][String[]]$computer = ".",
  [Parameter(Mandatory=$false)][String[]]$computerName = $env:computername,
  [Parameter(Mandatory=$false)][String]$DefaultDomainName = $env:USERDOMAIN,
  [Parameter(Mandatory=$false)][String]$DefaultUserName = "test",
  [Parameter(Mandatory=$false)][String]$DefaultPassword  = "pwd",
  [Parameter(Mandatory=$false)][String]$Group  = "administrators",
  [Parameter(Mandatory=$false)][Int]$AutoLogonCount
)


######使用net use建立新user account
function create_user_netuser(){
net user /add $DefaultUserName $DefaultPassword   ######建立新user account
net localgroup $Group $DefaultUserName /add  ######分配administrator的權限給新user account
}

######使用ADSI (Active Directory Service Interfaces)建立新user account
function create_user_ADSI(){
    ######建立新user account
$objou=[ADSI]"WinNT://$computerName,computer"
$objuser=$objou.Create("user", $DefaultUserName)
$objuser.SetInfo()
$objuser.SetPassword($DefaultPassword)
######分配administrator的權限給新user account
$objou=[ADSI]("WinNT://$computerName/$Group,group")
$objou.add("WinNT://$DefaultUserName,user")
}

######修改registryenable auto logon
function add_autologon(){
 foreach($computer in $computerName){ 
      #do local modifications here
      Write-Verbose "Adding required registry values on $($computer)"
      Write-Verbose "Saving curent location"
      Push-Location
      Set-Location "HKLM:\Software\Microsoft\Windows NT\Currentversion\WinLogon"
      New-ItemProperty -Path $pwd.Path -Name "AutoAdminLogon" -Value 1 -PropertyType "String" -Force | Out-Null
      New-ItemProperty -Path $pwd.Path -Name "DefaultUserName" -Value $DefaultUserName -PropertyType "String" -Force | Out-Null
      New-ItemProperty -Path $pwd.Path -Name "DefaultPassword" -Value $DefaultPassword -PropertyType "String" -Force | Out-Null
      New-ItemProperty -Path $pwd.Path -Name "DefaultDomainName" -Value $DefaultDomainName -PropertyType "String" -Force | Out-Null
      if ($AutoLogonCount) {
        New-ItemProperty -Path $pwd.Path -Name "AutoLogonCount" -Value $AutoLogonCount -PropertyType "Dword" -Force | Out-Null
      }
      Write-Verbose "restoring earlier location"
      Pop-Location
}
}

Step2Running windows PowerShell as administrator



Step 3Running the PowerShell script 


Step 4To check OS registry



Step 5Restart your PC to auto logon



沒有留言:

張貼留言