0

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

我有脚本,但我无法从同一输出文件中的“ 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
4

1 回答 1

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
}
于 2013-10-23T16:51:48.433 回答