PowerShell脚本(隐藏密码)

bzzcjhmw  于 2023-10-18  发布在  Shell
关注(0)|答案(1)|浏览(117)

我创建了一个PS脚本,在执行后重新启动并自动登录Windows

$RegPath = "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon"
$DefaultUsername = "Username-Example"
$DefaultPassword = "Password-Example"
Set-ItemProperty $RegPath "AutoAdminLogon" -Value "1" -type String 
Set-ItemProperty $RegPath "DefaultUsername" -Value "$DefaultUsername" -type String 
Set-ItemProperty $RegPath "DefaultPassword" -Value "$DefaultPassword" -type String
Set-ItemProperty $RegPath "AutoLogonCount" -Value "1" -type DWord
Start-Sleep -Seconds 15 ; Restart-Computer -Force

是否有一个命令,我可以集成在这个脚本中隐藏/加密纯文本密码,仍然是可读/可用的脚本/窗口?
我的PS编码技能是有限的,所以任何帮助是赞赏

0x6upsns

0x6upsns1#

若要在PowerShell脚本中安全地存储和检索凭据,可以使用Get-Credential小工具提示用户输入凭据,然后使用Windows凭据管理器安全地存储凭据。这种方法避免了在脚本中存储明文密码。以下是如何修改脚本:

# Prompt the user for their credentials and store them securely
$credential = Get-Credential -Message "Enter your credentials"

# Store the credentials in Windows Credential Manager
$credential | New-StoredCredential -Target "MyTargetName"

# Set up autologon with the stored credentials
$RegPath = "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon"
Set-ItemProperty $RegPath "AutoAdminLogon" -Value "1" -Type String
Set-ItemProperty $RegPath "DefaultUsername" -Value "$($credential.UserName)" -Type String
Set-ItemProperty $RegPath "AutoLogonCount" -Value "1" -Type DWord

# Restart the computer
Start-Sleep -Seconds 15
Restart-Computer -Force

相关问题