2013-02-25 11 views
15

Ho lo script seguente che voglio che venga distribuito su più server e ottenga il valore di un registro. Sfortunatamente, al momento sta semplicemente postando il valore del registro locale della macchina su cui sto eseguendo lo script.Ottieni il valore di registro remoto

Come si esegue lo script sul registro remoto?

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

risposta

31

Puoi provare a utilizzare .net:

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

Grazie - questo funziona; come faccio a incorporare questo nel LogWrite che voglio che il valore venga emesso nel file di log che ho? – lara400

+1

@ lara400 Come stai facendo nel tuo codice: 'LogWrite" $ computer1 STARTED $ NetbackupVersion1 "'. Ma forse non ho capito la tua domanda ... –

+0

Grazie mille - la tua ha fatto il trucco ...... come ha fatto Shay! – lara400

4

Se si dispone di PowerShell servizi remoti e la configurazione CredSSP poi si può aggiornare il codice di seguito:

$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

Se si dispone di servizi remoti e non si desidera utilizzare moduli di terze parti, utilizzare questo. È molto più facile da leggere rispetto alle chiamate dirette ai metodi. CredSSP è una cosa a doppio salto? Non ne avevo bisogno per questo. Grazie! –

12

Provare il Remote Registry Module, il provider del Registro di sistema non può operare in remoto:

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

oh non sapevo che esistesse! neat-o – jbockle

+0

@shaylevy Hai fatto un ottimo lavoro con questo modulo! –

+2

fantastico - proprio quello di cui avevo bisogno - grazie per questo - ha funzionato perfettamente. – lara400

2

Per registro remoto è necessario utilizzare NET con PowerShell 2,0

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

grazie per questo - sembra simile a C. B e che ha funzionato. – lara400

2

Se avete bisogno di SID dell'utente e sfogliare la cartella HKEY_USERS remota, è possibile seguire questo script:

<# 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’) 

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

-2

Utilizzo del modulo python e 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) 

Ulteriori informazioni $ reg /? al prompt dei comandi.

+0

Le persone che effettuano lo svuotamento verso il negativo dovrebbero fornire una spiegazione del motivo per cui suggeriscono che questa non è una buona risposta. In questo modo tutti possono migliorare. – Carol

0

un'altra opzione ... ha bisogno di comunicazione remota ...

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