27

我有下面的脚本,我希望它发送到多个服务器并获取注册表的值。不幸的是,它目前只是发回我正在运行脚本的机器的本地注册表值。

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

脚本:

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

6 回答 6

51

您可以尝试使用 .net:

$Reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $computer1)
$RegKey= $Reg.OpenSubKey("SOFTWARE\\Veritas\\NetBackup\\CurrentVersion")
$NetbackupVersion1 = $RegKey.GetValue("PackageVersion")
于 2013-02-25T14:44:31.693 回答
15

试试Remote Registry Module,registry provider无法远程操作:

Import-Module PSRemoteRegistry
Get-RegValue -ComputerName $Computer1 -Key SOFTWARE\Veritas\NetBackup\CurrentVersion -Value PackageVersion 
于 2013-02-25T14:49:22.470 回答
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
于 2013-02-25T14:46:37.777 回答
3

另一种选择...需要远程处理...

(invoke-command -ComputerName mymachine -ScriptBlock {Get-ItemProperty HKLM:\SOFTWARE\VanDyke\VShell\License -Name Version }).version
于 2018-02-05T03:51:09.320 回答
2

对于远程注册表,您必须使用带有 powershell 2.0 的 .NET

$w32reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine',$computer1)
$keypath = 'SOFTWARE\Veritas\NetBackup\CurrentVersion'
$netbackup = $w32reg.OpenSubKey($keypath)
$NetbackupVersion1 = $netbackup.GetValue('PackageVersion')
于 2013-02-25T14:50:25.857 回答
2

如果您需要用户的 SID 并浏览远程 HKEY_USERS 文件夹,您可以按照以下脚本进行操作:

<# Replace following domain.name with yours and userAccountName with remote username #>
$userLogin = New-Object System.Security.Principal.NTAccount(“domain.name“,”userAccountName“)
$userSID = $userLogin.Translate([System.Security.Principal.SecurityIdentifier])

<# We will open HKEY_USERS and with accurate user’s SID from remoteComputer #>
$remoteRegistry = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey(‘Users’,”remoteComputer“)

<# We will then retrieve LocalName value from Control Panel / International subkeys #>

$key = $userSID.value+”\Control Panel\International”
$openKey = $remoteRegistry.OpenSubKey($key)

<# We can now retrieve any values #>

$localName = $openKey.GetValue(‘LocaleName’)

来源:http ://techsultan.com/how-to-browse-remote-registry-in-powershell/

于 2015-05-07T18:48:02.370 回答