使用 powershell 从多台计算机中提取注册表值 [英] Extract registry value from multiple computers using powershell

查看:36
本文介绍了使用 powershell 从多台计算机中提取注册表值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我和一位同事正在尝试创建一个 powershell 脚本,该脚本使用一个 CSV 文件,该文件包含我们公司网络上所有计算机的名称,并使用这些名称连接到远程注册表并提取特定值.

A colleague and I are attempting to create a powershell script which uses a CSV file that contains the names of all our computers on the company network and uses those names to connect to a remote registry and extract a specific value.

这是我们目前所拥有的:

This is what we have so far:

$strMachineName = import-csv .\computer_name.csv
foreach ($line in $strMachineName)
{
                $reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $line.computer)
                $regkey = $reg.OpenSubkey("SOFTWARE\\Olympus\\DSSPlayerPro\\Transcription Module\\UserInformation")
                $serialkey = $regkey.GetValue("SerialNumber")
                write-host $line","$serialkey
}

现在,此脚本运行良好,但存在输出问题,这使得许多计算机难以理解.

Now, this script works fine but there are output issues that make it hard to understand with many computers.

我们希望能够将 stdout 输出到另一个文件,无论是 csv 还是 txt,我们还希望能够将 stderr 输出到 null,或者一个单独的文件,以便我们以后可以检查它.

We would like to be able to output stdout to another file, either a csv or txt, and we would also like to be able to output stderr to null, or a seperate file so we can inspect it later.

我的研究让我相信 write-host 不是适合使用的命令,因为它在使用变量时无法输出到文件 (??),所以我尝试使用 echo但是我在让变量按照我的意愿工作时遇到了问题.

My research has made me believe that write-host is not the appropriate command to be using since it cannot output to a file when variables are used (??), so I tried to use echo but I'm having problems getting variables to work as I would like.

感谢您的帮助,谢谢!

推荐答案

您需要使用 try/catch 来捕获异常.

You'll need to catch exceptions with a try/catch.

$strMachineName = import-csv .\computer_name.csv
foreach ($line in $strMachineName)
{
    try {
        $reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $line.computer)
        $regkey = $reg.OpenSubkey("SOFTWARE\\Olympus\\DSSPlayerPro\\Transcription Module\\UserInformation")
        $serialkey = $regkey.GetValue("SerialNumber")

        ('"{0}","{1}"' -f $line.computer, $serialkey) | out-file C:\stdout.csv -append -encoding ascii
    } catch {
        ('"{0}","{1}"' -f $line.computer, $_) | out-file C:\stderr.csv -append -encoding ascii
    }
}

这会创建两个文件(stdout.csv 和 stderr.csv).成功的查询存储在 stdout.csv 中,失败的查询存储在 stderr.csv 中.

This creates two files (stdout.csv and stderr.csv). Successful queries are stored in stdout.csv and failed queries are stored in stderr.csv.

这篇关于使用 powershell 从多台计算机中提取注册表值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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