获取远程注册表值

lar*_*400 24 powershell powershell-2.0

我有以下脚本,我希望它出去到多个服务器并获得注册表的价值.不幸的是,它目前只是回发我正在运行脚本的机器的本地注册表值.

如何让脚本针对远程注册表运行?

脚本:

clear
#$ErrorActionPreference = "silentlycontinue"

$Logfile = "C:\temp\NEWnetbackup_version.log"

Function LogWrite
{
    param([string]$logstring)

    Add-Content $Logfile -Value $logstring
}

$computer = Get-Content -Path c:\temp\netbackup_servers1.txt

foreach ($computer1 in $computer){

$Service = Get-WmiObject Win32_Service -Filter "Name = 'NetBackup Client Service'" -ComputerName $computer1

    if (test-connection $computer1 -quiet) 
    {
            $NetbackupVersion1 = $(Get-ItemProperty hklm:\SOFTWARE\Veritas\NetBackup\CurrentVersion).PackageVersion

            if($Service.state -eq 'Running')
            {
                LogWrite "$computer1 STARTED $NetbackupVersion1"
            }
            else
            {
                LogWrite "$computer1 STOPPED $NetbackupVersion1"
            }
    }
    else 
    {
        LogWrite "$computer1 is down" -foregroundcolor RED
    }
}
Run Code Online (Sandbox Code Playgroud)

CB.*_*CB. 44

您可以尝试使用.net:

$Reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $computer1)
$RegKey= $Reg.OpenSubKey("SOFTWARE\\Veritas\\NetBackup\\CurrentVersion")
$NetbackupVersion1 = $RegKey.GetValue("PackageVersion")
Run Code Online (Sandbox Code Playgroud)


Sha*_*evy 15

尝试远程注册表模块,注册表提供程序无法远程操作:

Import-Module PSRemoteRegistry
Get-RegValue -ComputerName $Computer1 -Key SOFTWARE\Veritas\NetBackup\CurrentVersion -Value PackageVersion 
Run Code Online (Sandbox Code Playgroud)

  • 太棒了 - 正是我需要的 - 感谢你 - 完美地工作. (2认同)

Mus*_*idi 6

如果您具有Powershell远程处理和CredSSP设置,则可以将代码更新为以下内容:

$Session = New-PSSession -ComputerName $Computer1 -Authentication CredSSP
$NetbackupVersion1 = Invoke-Command -Session $Session -ScriptBlock { $(Get-ItemProperty hklm:\SOFTWARE\Veritas\NetBackup\CurrentVersion).PackageVersion}
Remove-PSSession $Session
Run Code Online (Sandbox Code Playgroud)