首页 文章

PowerShell用于获取已安装的程序

提问于
浏览
0

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

我有脚本,但我无法在同一输出文件中从“SOFTWARE \ Microsoft \ Windows \ CurrentVersion \ Uninstall ", and " Software \ Wow6432Node \ Microsoft \ Windows \ CurrentVersion \ Uninstall”获取信息 . 我'm obviously missing something inherently obvious, as PowerShell is clearly able to do this, and I'米要求有人请救我脱离我的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 回答

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

相关问题