Checking if registry value is equal to 1 is not working correctly

前端 未结 1 1972
余生分开走
余生分开走 2020-12-20 03:55

I have slopped together bits of PowerShell to remote query a list of machines, stored in a .csv file, for a registry value. If the registry key\'s value is equal to \'1\',

相关标签:
1条回答
  • 2020-12-20 04:27

    In PowerShell = is an assignment operator, not a comparison operator. Change this line:

    if ($keyValue = "1")
    

    into this:

    if ($keyValue -eq "1")
    

    For more information see Get-Help about_Operators.

    You're making this way too complicated, BTW. Something like this should suffice:

    $keyname = 'SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\WinLogon'
    
    Import-Csv 'C:\temp\machines.csv' | % {
      $reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey("LocalMachine",
               $_.machinename)
      $key = $reg.OpenSubkey($keyname)
      $value = $key.GetValue('AutoAdminLogon')
      if ($value -eq "1") {
        $filename = Join-Path "c:\temp\autologin" $_.machinename
        try {
          touch $filename
          $textFile = Get-Item $filename
        } catch {
          $_
        }
      }
      Write-Host ($_.machinename , $value)
    }
    
    0 讨论(0)
提交回复
热议问题