2013-02-25 109 views
15

我有下面的腳本,我希望它出去多個服務器並獲得註冊表的值。不幸的是,它現在只是發回我正在運行腳本的機器的本地註冊表值。獲取遠程註冊表值

如何讓腳本對遠程註冊表運行?

SCRIPT:

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

回答

31

您可以嘗試使用.NET:

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

感謝 - 這工作;我如何將這個結合到LogWrite中,我希望將該值輸出到我擁有的日誌文件中? – lara400 2013-02-25 15:01:21

+1

@ lara400當你在你的代碼中執行:'LogWrite「$ computer1 STARTED $ NetbackupVersion1」'。但也許我不明白你的問題... – 2013-02-25 15:05:10

+0

非常感謝 - 你的人做了詭計......就像謝伊的! – lara400 2013-02-25 15:08:12

4

如果你有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 
+0

如果您有遠程處理並且不想使用第三方模塊,請使用此選項。閱讀比直接方法調用要容易得多。 CredSSP是雙跳的東西嗎?我不需要這個。謝謝! – 2014-05-05 18:07:17

12

嘗試Remote Registry Module,註冊表提供程序無法遠程操作:

Import-Module PSRemoteRegistry 
Get-RegValue -ComputerName $Computer1 -Key SOFTWARE\Veritas\NetBackup\CurrentVersion -Value PackageVersion 
+0

哦,我不知道這存在! neat-o – jbockle 2013-02-25 14:53:35

+0

@ shaylevy你對這個模塊做了很棒的工作! – 2013-02-25 15:00:44

+2

太棒了 - 正是我所需要的 - 感謝這一點 - 完美運作。 – lara400 2013-02-25 15:08:41

2

對於遠程註冊表你如果需要用戶的SID使用.NET使用PowerShell 2.0

$w32reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine',$computer1) 
$keypath = 'SOFTWARE\Veritas\NetBackup\CurrentVersion' 
$netbackup = $w32reg.OpenSubKey($keypath) 
$NetbackupVersion1 = $netbackup.GetValue('PackageVersion') 
+1

感謝這一點 - 似乎與C.B's類似,而且工作。 – lara400 2013-02-25 15:09:25

2

和瀏覽遠程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/

-2

使用python和wmi模塊。

import wmi 

conn = wmi.WMI('172.20.58.34', user='UserName', password='Password') 
command = r'cmd /c reg query "HKLM\SOFTWARE\Microsoft" /ve > C:\output.txt' 
conn.Win32_Process.Create(CommandLine=command) 

更多信息$ reg /?在命令提示符下。

+0

對此負面評價的人應該解釋爲什麼他們認爲這不是一個好的答案。這樣每個人都可以改進。 – Carol 2017-10-16 17:16:20

0

另一種選擇......需要遠程...

(invoke-command -ComputerName mymachine -ScriptBlock {Get-ItemProperty HKLM:\SOFTWARE\VanDyke\VShell\License -Name Version }).version