获取已安装程序的PowerShell
我将在远程服务器上托管一个文件(只读),并要求用户在其机器上运行该文件以收集已安装的程序信息。我想将文件保存到他们的用户空间中的桌面上,这样我就可以让他们将其发送给我们 我有脚本,但无法从同一输出文件中的“SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall”和“SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall”获取信息。很明显,我遗漏了一些本质上显而易见的东西,因为PowerShell显然能够做到这一点,我请求有人帮我解决PEBKAC问题 提前谢谢,谢谢 这是我的密码获取已安装程序的PowerShell,powershell,Powershell,我将在远程服务器上托管一个文件(只读),并要求用户在其机器上运行该文件以收集已安装的程序信息。我想将文件保存到他们的用户空间中的桌面上,这样我就可以让他们将其发送给我们 我有脚本,但无法从同一输出文件中的“SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall”和“SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall”获取信息。很明显,我遗漏了一些本质上显而易见的东西,因为P
$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
现在,以下两行设置了相同的变量:
$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
}
当然,这是假设用户不必事先运行“powershell set executionpolicy unrestricted”。你也许可以告诉我,我是新来的PowerShell!为什么
ForEach对象
而不是ForEach(stuff)
,您可能会问。。。这是个人的偏好:ForEach对象
延续管道并可以异步处理foreach(stuff)
已经过时了,必须在构造迭代器之前收集所有对象。