检查注册表值是否等于 1 无法正常工作 [英] Checking if registry value is equal to 1 is not working correctly

查看:73
本文介绍了检查注册表值是否等于 1 无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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.

我做错了什么?

EDIT*** 我发现a 问题 我在注册表路径的 $key 变量中有一个拼写错误.2013/7/17 2:21p

EDIT*** I found a problem I had a typo in the $key variable for the registry path. 7/17/2013 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:
}

推荐答案

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

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

if ($keyValue = "1")

进入这个:

if ($keyValue -eq "1")

有关详细信息,请参阅获取有关_Operators 的帮助.

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

这篇关于检查注册表值是否等于 1 无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆