防止注册表项在重新启动时自动删除

时间:2016-12-14 15:21:07

标签: powershell windows-7 registry

在使用Windows 7对计算机进行映像后,我使用PowerShell创建了一个AutoLogon实例。该脚本旨在自动执行安装需要重新启动的加密软件的过程。这是我到目前为止的脚本。

#Registry path declaration
$RegPath = "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon"
$RegROPath = "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce"

Write-Host "Installing Encryption."
Write-Host "Please note that a reboot is required to complete the installation process."
Write-Host "Enter the local administrator credentials when promoted!"
Write-Host ""
Write-Host "Press any key to continue..."
[void][System.Console]::ReadKey($true)

Clear

#Get Auto Logon credentials.
$credential = Get-Credential $env:username

#Install 01_ClientPreinstall then restart the computer
Start-Process -FilePath "c:\encrypt_installer\01_ClientPreinstall.msi" -argumentlist "/passive" -Wait

#Set registry properties for auto logon
Set-ItemProperty $RegPath "AutoAdminLogon" -Value "1" -Type String
Set-ItemProperty $RegPath "DefaultUserName" -Value $credential.GetNetworkCredential().username -Type String
New-ItemProperty $RegPath "DefaultPassword" -Value $credential.GetNetworkCredential().password -Type String

#Add script to registry to continue after logon
New-ItemProperty $RegROPath "Continue" -Value "C:\encrypt_installer\sre02.bat" -Type String

Restart-Computer -Force

我遇到的问题是,脚本运行并将值添加到注册表中:

enter image description here

但是当计算机重新启动时,它不会登录。我必须手动登录。

我通过注释掉重启,进入注册表并查看值,然后手动重启来验证密码是否正确。如果我在登录时再次运行脚本,它将完成相同的过程,但这次它将自动登录。所以在图像之后,我尝试在运行脚本之前先重启机器,但它仍然不起作用。

我发现在运行脚本后第一次重新启动计算机时,会自动删除DefaulPassword注册表项。有没有办法可以防止这种情况发生?

值得注意的是,默认情况下注册表中不存在DefaultPassword密钥,但我知道可以从脚本中的New-ItemProperty调用中收集。

0 个答案:

没有答案
相关问题