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")
}
######修改registry來enable 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
}
}
Step2、Running windows PowerShell as administrator。
Step 3、Running the PowerShell script
Step 4、To check OS registry
Step 5、Restart your PC to auto logon
沒有留言:
張貼留言