windows 检查注册表值是否等于 1 无法正常工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17710364/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 10:43:21  来源:igfitidea点击:

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

windowspowershellregistry

提问by user2593163

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', the script should then create a text file using the machine's name as the name of the text file.

我已经将 PowerShell 的各个部分放在一起,以远程查询存储在 .csv 文件中的机器列表以获取注册表值。如果注册表项的值等于“1”,则脚本应使用机器的名称作为文本文件的名称创建一个文本文件。

Everything works great. The script runs happily without any errors. The problem is that when I go back and remotely check a targeted registry value, I find that the value isn't 1. The script is simply creating a file for every line in the .csv.

一切都很好。脚本运行愉快,没有任何错误。问题是,当我返回并远程检查目标注册表值时,我发现该值不是 1。脚本只是为 .csv 中的每一行创建一个文件。

What am I doing wrong?

我究竟做错了什么?

EDIT*** I found aproblem I had a typo in the $key variable for the registry path. 7/17/2013 2:21p

编辑*** 我发现了一个问题,我在注册表路径的 $key 变量中有一个错字。2013/7/17 2:21p

$File = Import-Csv 'c:\temp\machines.csv'

foreach ($line in $file)
{
  $machinename = $line.machinename
  trap [Exception] {continue}
  $reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey("LocalMachine",$MachineName)
  $key = "SOFTWARE\Microsoft\Windows NT\CurrentVersion\WinLogon"
  $regkey = ""
  $regkey = $reg.opensubkey($key)
  $keyValue = ""
  $keyValue = $regKey.GetValue('AutoAdminLogon')

  if ($keyValue = "1")
  {
    try
    {
      $textFile = New-Item -Path "c:\temp\autologin" -Name $MachineName -ItemType "File"
    }
    catch
    {
      $msg = $_
      $msg
    }
  }
  $Results = $MachineName , $keyValue
  Write-host $Results

  #Output Below Here:
}

回答by Ansgar Wiechers

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

在 PowerShell 中=是赋值运算符,而不是比较运算符。改变这一行:

if ($keyValue = "1")

into this:

进入这个:

if ($keyValue -eq "1")

For more information see Get-Help about_Operators.

有关更多信息,请参阅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)
}