不太清楚为什么以下不起作用。基本上,有多个基于键值的elseif语句。
我测试的服务器的设置$valuePassword
等于我输入的随机字符,我还将$valueAutoLogin
设置为'1'
。但是,我得到的结果是低于哪个没有显示任何东西?
另外 - 你能想到一个更好的方法来做这个而不是多个elseif的声明吗?
>
clear
Import-Module PSRemoteRegistry
$computers = Get-Content -Path C:\MANNY\Servers.txt
foreach($computer in $computers)
{
Write-Host "Checking $computer"
#Check if key exists
$valuePassword = Get-RegValue -ComputerName $Computer -Key 'SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon' -Value DefaultPassword
$valueAutoLogin = Get-RegValue -ComputerName $Computer -Key 'SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon' -Value AutoAdminLogon
if($valuePassword -eq $null -and $valueAutoLogin -eq $null)
{Write-Host "$computer not set"}
elseif($valuePassword -notcontains $null -and $valueAutoLogin -eq $null)
{Write-Host "$computer has Password set"}
elseif($valuePassword -eq $null -and $valueAutoLogin -eq '1')
{Write-Host "$computer has Autologin set but not password"}
elseif($valuePassword -notcontains $null -and $valueAutoLogin -eq '1')
{Write-Host "$computer has Autologin set and password set - AUTOLOGIN IS POSSIBLY SET"}
}
> 结果:
Checking SV160666
Checking SV160668
Checking SV160670
Checking SV160672
Checking SV160674
Checking SV180435
Checking SV193865
Checking SV193885
答案 0 :(得分:1)
尝试使用内置方法时会得到什么结果?此外,您应添加else
分支以报告意外值。
Get-Content "C:\MANNY\Servers.txt" | % {
$reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey("LocalMachine", $_)
$key = $reg.OpenSubKey("SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon")
$valuePassword = $key.GetValue("DefaultPassword")
$valueAutoLogin = $key.GetValue("AutoAdminLogon")
$key.Close()
$reg.Close()
if ($valuePassword -eq $null -and $valueAutoLogin -eq $null) {
Write-Host "$_ not set"
} elseif ($valuePassword -ne $null -and $valueAutoLogin -eq $null) {
Write-Host "$_ has Password set"
} elseif ($valuePassword -eq $null -and $valueAutoLogin -eq '1') {
Write-Host "$_ has Autologin set but not password"
} elseif ($valuePassword -ne $null -and $valueAutoLogin -eq '1') {
Write-Host "$_ has Autologin set and password set - AUTOLOGIN IS POSSIBLY SET"
} else {
Write-Host "$_: unexpected AutoLogin value: $valueAutoLogin"
}
}