Powershell 检查多个密钥的注册表值并将结果导出到 csv

问题描述 投票:0回答:1

我需要一些帮助,因为我想要完成的事情似乎有点超出了我的经验范围。

这就是我正在尝试做的事情:

运行脚本来验证 8 个注册表项是否存在以及为这些项设置的值。 将结果导出到 csv 文件以供审核。

输出应如下所示:

电脑名称 |注册密钥 |价值|存在 0 或 1|

我尝试了许多不同的脚本选项来使用 Get-ItemProperty、Get-RegistryValue、Get-ItemPropertyValue 返回结果,但没有得到我正在寻找的结果。

$Computer = Get-Content "Path\PCEnabledList.txt"

ForEach ($PC in $Computers) {

   Get-ItemProperty -Path 'RegistryPath' -ValueName "Value"
} | Select-Object $PC,Value, | Export-CSV "Path\RegCheck.csv" -NoTypeInformation -Append

我知道上面的脚本到处都是(我已经尝试过很多次了,太多了,无法在这里列出。我可以让一个脚本从其中一个键返回 1 值,但不会在旁边显示计算机名称)输出,所以我不知道哪些机器返回“1”值。
其他错误包括: -路径不存在(但已在远程计算机上检查并且密钥在那里) - 一个或多个计算机名称无效,如果您尝试传递 URI,请使用 -ConnectionUri 参数,或传递 URI 对象而不是字符串 -WinRM 无法处理该请求。使用 Kerberos 身份验证时发生以下错误:找不到计算机。

我知道发生了很多事情,但如果我能得到一些帮助来找出一个可以完成我需要做的事情的脚本,那就太好了。

powershell registry
1个回答
0
投票

导出所需信息的一种方法是使用PSCustomObject。这将允许我们在循环中保存您想要的信息(PC 名称、注册表项、项是否存在、项的任何属性以及属性值)。

对于列表中的每台计算机,我们循环遍历要检查的注册表项列表。如果该注册表项存在,我们将获取密钥属性和属性值。该信息被保存到自定义对象中。

# list computers
$computers = @("MYPC1","MYPC2")
# list of KEYS you want to check
$regKeyPaths = @("REGISTRY::HKEY_LOCAL_MACHINE\SOFTWARE\Software\sub1","REGISTRY::HKEY_LOCAL_MACHINE\SOFTWARE\Software\sub2")



$info = ForEach ($PC in $Computers) {
    # check each key
    foreach ($regKey in $regKeyPaths) {
    # check if key exists 
    $pathExists  = Invoke-command -ComputerName $PC {Test-Path -Path $using:regKey}
    # if it exists get the key properties and the values
    if ($pathExists){
        $properties = Get-Item -Path $regKey | Select-object -ExpandProperty Property
        $values = foreach ($property in $properties){
          Get-ItemPropertyValue -Path $regKey -Name $property
        }
    }
    # if key doesn't exist set properties and values to null
    else{
        $properties= $null
        $values = $null
    }
    # custom object
    [PSCustomObject]@{
        PCName = $PC
        RegKey = $regKey
        Property = $properties
        Value = $values
        Exists = $pathExists
    }
}
}
# export object to csv. Expanding "Property" and "Value"
# https://www.millersystems.com/powershell-exporting-multi-valued-attributes-via-export-csv-cmdlet/
$info | Select "PCName","RegKey",@{Name='Property';Expression={[string]::join(“;”, ($_.Property))}},
@{Name='Value';Expression={[string]::join(“;”, ($_.Value))}},'Exists' | Export-Csv  -Path info.csv -NoTypeInformation 

输出

注释

今晚晚些时候我会尝试添加更多解释。

© www.soinside.com 2019 - 2024. All rights reserved.