2014-02-21 56 views
4

我將以下腳本拼湊在一起,列出本地系統上所有已安裝的應用程序並將其寫入日誌文件中,但我不知道如何在使用PSRemoteRegistry時獲得相同的輸出,實際輸入列表我需要這個反對將是所有遠程目標。在遠程服務器上列出已安裝的應用程序

有沒有人有經驗將此相同的代碼放入可通過PSRemoteRegistry使用的cmdlet中?具體來說,我需要它來枚舉鍵HKLM發現每個已安裝的應用程序的顯示名:\軟件\微軟\ CURRENTVERSION \卸載

這件作品,我需要幫助的漸入PSRemoteRegistry cmdlet的是:

Get-ChildItem 'HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall' | ForEach-Object {Log (Get-ItemProperty $_.pspath).DisplayName}

,這是整個腳本:

clear 
    #$ErrorActionPreference = "silentlycontinue" 

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

    Function Log 
    { 
     param([string]$logstring) 

     Add-Content $Logfile -Value $logstring 
    } 

    $target_list = Get-Content -Path c:\temp\installed_apps_targets.txt 

    foreach ($system in $target_list){ 

     if (test-connection $system -quiet) 
     { 
      Log "---------------Begin $system---------------" 
      Get-ChildItem 'HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall' | ForEach-Object {Log (Get-ItemProperty $_.pspath).DisplayName} 
      Log "---------------End $system---------------" 
     } 
     else 
     { 
      Log "**************$system was unreachable**************" 
     } 

} 
+1

爲什麼不使用PowerShell Remoting部署腳本,並將結果返回到主機會話中?我建議從函數中返回對象,而不是將文本寫入日誌文件。這將有助於您更有效地處理結果。 –

+0

我唯一的問題是我們正在談論26個域名和3000多個服務器。我不能依靠主機級別的手動配置修改來適應執行腳本。 PowerShell Remoting是否需要觸摸每一個以確保PowerShell和適當的配置適用於目標系統? –

回答

1

可以適應這樣的事情:

$Computer = "ComputerName" 

$Reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $Computer) 
$RegKey= $Reg.OpenSubKey('Software\Microsoft\Windows\CurrentVersion\Uninstall') 

$Keys = $RegKey.GetSubKeyNames() 

$Keys | ForEach-Object { 
    $Subkey = $RegKey.OpenSubKey("$_") 
    Write-Host $Subkey.GetValue('DisplayName') 
} 
1

你見過Invoke-Command嗎?

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

Function Log() { 
    param([string]$logstring) 

    Add-Content $Logfile -Value $logstring 
} 

$scriptbock = {Get-ChildItem 'HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall' | ForEach-Object {Log (Get-ItemProperty $_.pspath).DisplayName}} 

Get-Content -Path c:\temp\installed_apps_targets.txt | % { 
    if (test-connection $_ -quiet) { 
     Log "---------------Begin $system---------------" 
     Log $(Invoke-Command -ComputerName $_ -ScriptBlock $scriptblock) 
     Log "---------------End $system---------------" 
    } 
    else 
    { 
     Log "**************$system was unreachable**************" 
    } 

} 
相關問題