PowerShell的 - 查询远程注册表键值,并生成文本文件,如果值等于1 [英] Powershell - Query remote registry key value and generate text file IF value equals 1

查看:416
本文介绍了PowerShell的 - 查询远程注册表键值,并生成文本文件,如果值等于1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

第一次发帖。彻底享受该网站,所有这一切停止在人才助阵。

我的PowerShell位越出停车一起远程查询机器列表,存储在一个.csv文件,对注册表值。如果该注册表项的值等于1,脚本应该使用计算机的名称作为文本文件的名称,然后创建一个文本文件。

一切的伟大工程。该脚本顺利地执行没有任何错误。问题是,当我回去远程检查针对性的注册表值,我觉得值不为1,脚本是创建在.csv每一行的文件。

我在做什么错了?

编辑***我发现 问题,我在注册表路径$关键变量一个错字。 2013年7月17日2:21P

  $文件=导入-CSV'C:\\ TEMP \\ machines.csv的foreach($中的文件$行)
{
  $ MACHINENAME = $ line.machinename
  陷阱[异常] {}继续
  $章= [Microsoft.Win32.RegistryKey] :: OpenRemoteBaseKey(LOCALMACHINE,$计算机名)
  $键=SOFTWARE \\\\ \\\\微软的Windows NT \\\\ \\\\ CURRENTVERSION WinLogon的
  $ REGKEY =
  $ REGKEY = $ reg.opensubkey($键)
  $的keyValue =
  $的keyValue = $ regKey.GetValue(AutoAdminLogon)  如果($的keyValue =1)
  {
    尝试
    {
      $文本文件=新建项目-PathC:\\ TEMP \\自动登录杂牌$计算机名-ItemType文件
    }
    抓住
    {
      $味精= $ _
      $味精
    }
  }
  $结果= $计算机名,$的keyValue
  写主机$结果  #Output以下位置:
}


解决方案

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

 如果($的keyValue =1)

这个:

 如果($的keyValue -eq1)

有关详细信息,请参见 获取帮助about_Operators

您正在做这样太复杂了,顺便说一句。像这样的东西应该足够了:

$键名='SOFTWARE \\\\ \\\\微软的Windows NT \\\\ \\\\ CURRENTVERSION WinLogon的进口-CSV'C:\\ TEMP \\ machines.csv| %{
  $章= [Microsoft.Win32.RegistryKey] :: OpenRemoteBaseKey(LOCALMACHINE
           $ _。MACHINENAME)
  $键= $ reg.OpenSubkey($键名)
  $值= $ key.GetValue(AutoAdminLogon)
  如果($值当量1){
    $文件名=联接路径C:\\ TEMP \\自动登录$ _ MACHINENAME
    尝试{
      触摸$文件名
      $ TEXTFILE = GET-项目$文件名
    } {抓
      $ _
    }
  }
  写主机($ _。计算机名,$值)
}

first time posting. Thoroughly enjoy the site and all of the talent that stops in to help out.

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.

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.

What am I doing wrong?

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

解决方案

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

这篇关于PowerShell的 - 查询远程注册表键值,并生成文本文件,如果值等于1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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