远程注册表项提取程序PowerShell脚本

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了远程注册表项提取程序PowerShell脚本相关的知识,希望对你有一定的参考价值。

我正在尝试创建一个PowerShell脚本,远程检查域上的每台计算机上是否有注册表项,然后将该键值与计算机名一起输出到.csv文件。

到目前为止,脚本将域中的所有计算机输出到.csv文件,但将其本地注册表项值设置为远程计算机的值。

任何帮助将不胜感激,这是我迄今为止所拥有的。

Import-Module ActiveDirectory

$SRVS = Get-ADComputer -Filter * -SearchBase 'DC=mydomain,DC=local' |
        select dnshostname

foreach ($SRV in $SRVS) {
    $REG = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $SRV.name)
    $REGKEY = $REG.OpenSubKey("SOFTWAREMicrosoftWindowsCurrentVersionQualityCompat")
    $MELT = $REGKEY.GetValue('cadca5fe-87d3-4b96-b7fb-a231484277cc')
    "$($SRV);$($MELT)" | Out-File C:Usersuser1Desktop
egkeys.CSV -Append
}
答案

一旦它被实例化,RegistryKey类就不会暴露它是一个远程密钥。这意味着您必须自己记录计算机名称。远程注册表值也没有标准格式。

如果我有一个PowerShell v5 +,我会使用这样的东西:

Import-Module ActiveDirectory

# No need for the Select-Object here since we're using $SRV.Name later
$SRVS = Get-ADComputer -Filter * -SearchBase 'DC=mydomain,DC=local'

# Create an arraylist to save our records
$Report = New-Object System.Collections.ArrayList

# This try finally is to ensure we can always write out what we've done so far
try {
    foreach ($SRV in $SRVS) {
        # Test if the remote computer is online
        $IsOnline = Test-Connection -ComputerName $SRV.Name -Count 1 -Quiet;
        if ($IsOnline) {
            # If system is Online
            $REG = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $SRV.name)
            $REGKEY = $REG.OpenSubKey("SOFTWAREMicrosoftWindowsCurrentVersionQualityCompat")
            $MELT = $REGKEY.GetValue('cadca5fe-87d3-4b96-b7fb-a231484277cc')

            # Create a PSObject record for convenience
            $Record = [PSCustomObject]@{
                ComputerName = $SRV;
                Key          = $REGKEY.Name;
                Value        = $MELT;
            }
        }
        else {
            # If system is Offline
            # Create a PSObject record for convenience
            $Record = [PSCustomObject]@{
                ComputerName = $SRV;
                Key          = '';
                Value        = '';
            }
        }

        # Add our record to the report
        $Report.Add($Record);
    }
}
finally {
    # Always write out what we have whether or not we hit an error in the middle
    $Report | Export-Csv -Path "C:Usersuser1Desktop
egkeys.csv" -NoTypeInformation
}

这可能适用于PowerShell v3 +,但我不再需要它来测试。

另一答案

该声明

$SRVS = Get-ADComputer ... | select dnshostname

为您提供仅包含一个属性的自定义对象列表:dnshostname。但是在你的循环中,你试图使用属性name,这些自定义对象没有。因此声明

[Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $SRV.name)

有效地成为

[Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $null)

意味着您打开本地注册表,而不是远程主机上的注册表。

$SRV.name改为$SRV.dnshostname,问题就会消失。

另一答案

你试图打印出实际的regkey与仅检查它是否存在的任何原因?

它存在或不存在。说用...之类的东西

Clear-Host 
Import-Module ActiveDirectory

$SRVS = (Get-ADComputer -Filter * -SearchBase (Get-ADDomainController).DefaultPartition)
$MeltHive = 'HKLM:SOFTWAREMicrosoftWindowsCurrentVersionQualityCompat'
$MeltHiveKey = 'cadca5fe-87d3-4b96-b7fb-a231484277cc'

ForEach($Srv in $SRVS)
{
    Invoke-Command -ComputerName $Srv.Name -ScriptBlock {
            If (Get-ItemProperty -Path $Using:MeltHive -Name $MeltHiveKey -ErrorAction SilentlyContinue)
            {"On Host $env:COMPUTERNAME MELT registry information exists"}
            Else {Write-Warning -Message "On host $env:COMPUTERNAME MELT registry information does not exist"}
        }
}



ForEach($Srv in $SRVS)
{
    Invoke-Command -ComputerName $Srv.Name -ScriptBlock {
            If ((Get-ChildItem -Path HKLM:SOFTWAREMicrosoftWindowsCurrentVersion) -match 'QualityCompat')
            {"On Host $env:COMPUTERNAME MELT registry information exists"}
            Else {Write-Warning -Message "On host $env:COMPUTERNAME MELT registry information does not exist"}
        }
}


Results of both the above is:

WARNING: On host DC01 MELT registry information does not exist
WARNING: On host EX01 MELT registry information does not exist
WARNING: On host SQL01 MELT registry information does not exist
On Host IIS01 MELT registry information exists

以上是关于远程注册表项提取程序PowerShell脚本的主要内容,如果未能解决你的问题,请参考以下文章

PowerShell添加或修改注册表开机启动项脚本

如何在 PowerShell 中调用带有输出参数的方法?

如何获取注册表项的值,并且仅使用 powershell 获取值

一键帮你复制多个文件到多个机器——PowerShell小脚本(内附PS远程执行命令问题解析)

在远程机器上启动应用程序并保持运行,powershell 脚本不应等待进程完成

powershell通过注册表路径判断系统是否需要重启