PowerShell用于获取已安装的程序

时间:2013-10-22 16:06:39

标签: powershell

我将在远程服务器(只读)上托管文件,并要求人们在其计算机上运行该文件以收集已安装的程序信息。我希望将文件保存到用户空间中的桌面,以便我可以将它们发送给我们。

我有脚本,但我无法从“ SOFTWARE \ Microsoft \ Windows \ CurrentVersion \ Uninstall ”和“ Software \ Wow6432Node \ Microsoft \ Windows”获取信息\ CurrentVersion \ Uninstall “在同一输出文件中。我显然缺少一些本质上显而易见的东西,因为PowerShell显然能够做到这一点,而且我要求有人请救我脱离我的PEBKAC问题!

提前谢谢,谢谢!

这是我的代码;

$computers = "$env:computername"

$array = @()

foreach($pc in $computers){

$computername=$pc

$UninstallKey="SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall" 
$UninstallKey="Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall"

$reg=[microsoft.win32.registrykey]::OpenRemoteBaseKey('LocalMachine',$computername) 

$regkey=$reg.OpenSubKey($UninstallKey) 

$subkeys=$regkey.GetSubKeyNames() 

Write-Host "$computername"
foreach($key in $subkeys){

    $thisKey=$UninstallKey+"\\"+$key 

    $thisSubKey=$reg.OpenSubKey($thisKey) 

    $obj = New-Object PSObject

    $obj | Add-Member -MemberType NoteProperty -Name "ComputerName" -Value $computername

    $obj | Add-Member -MemberType NoteProperty -Name "DisplayName" -Value $($thisSubKey.GetValue("DisplayName"))

    $obj | Add-Member -MemberType NoteProperty -Name "DisplayVersion" -Value $($thisSubKey.GetValue("DisplayVersion"))

    $obj | Add-Member -MemberType NoteProperty -Name "InstallLocation" -Value $($thisSubKey.GetValue("InstallLocation"))

    $obj | Add-Member -MemberType NoteProperty -Name "Publisher" -Value $($thisSubKey.GetValue("Publisher"))

    $array += $obj

    } 

}

$array | Where-Object { $_.DisplayName } | select ComputerName, DisplayName, DisplayVersion, Publisher | export-csv C:\Users\$env:username\Desktop\Installed_Apps.csv

1 个答案:

答案 0 :(得分:1)

现在,以下两行设置了相同的变量:

$UninstallKey="SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall" 
$UninstallKey="Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall"

使用此:

$UninstallKey = @(
    'SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall',
    'SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall'
)

然后将真实逻辑包装在:

$UninstallKey | ForEach-Object {
    $regkey=$reg.OpenSubKey($_)

    # the rest of your logic here
}