2014-02-21 51 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**************" 
    } 

} 
相关问题