1

Not too sure why the below is not working. Basically, there are multiple elseif statements based on the values of keys.

I tested a server with setting $valuePassword equal to random characters I put in and I also set $valueAutoLogin to '1'. But, the result I get is below which does not show anything?

Also - can you think of a better way of doing this instead of multiple elseif statments?

>

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"}

}

> RESULT:

Checking SV160666
Checking SV160668
Checking SV160670
Checking SV160672
Checking SV160674
Checking SV180435
Checking SV193865
Checking SV193885
4

1 回答 1

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"
  }
}
于 2013-05-15T13:13:30.167 回答