许多软件的安装会在注册表注册自己。这里段代码能很好的从本地或远程注册表主键获取已安装的32位或64位软件。脚本也可以作为一个很好的远程读取注册表的例子:
# NOTE: RemoteRegistry Service needs to run on a target system!
$Hive = 'LocalMachine'
# you can specify as many keys as you want as long as they are all in the same hive
$Key = 'SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall', 'SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall'
# you can specify as many value names as you want
$Value = 'DisplayName', 'DisplayVersion', 'UninstallString'
# you can specify a remote computer name as long as the RemoteRegistry service runs on the target machine,
# you have admin permissions on the target, and the firewall does not block you. Default is the local machine:
$ComputerName = $env:COMPUTERNAME
# add the value "RegPath" which will contain the actual Registry path the value came from (since you can specify more than one key)
$Value = @($Value) + 'RegPath'
# now for each regkey you specified...
$Key | ForEach-Object {
# ...open the hive on the appropriate machine
$RegHive = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey($Hive, $ComputerName)
# ...open the key in that hive...
$RegKey = $RegHive.OpenSubKey($_)
# ...find names of all subkeys...
$RegKey.GetSubKeyNames() | ForEach-Object {
# ...open subkeys...
$SubKey = $RegKey.OpenSubKey($_)
# ...and read all the requested values from each subkey
# ...to store them, use Select-Object to create a simple new object
$returnValue = 1 | Select-Object -Property $Value
$Value | ForEach-Object {
$returnValue.$_ = $subkey.GetValue($_)
}
# ...add the current regkey path name
$returnValue.RegPath = $SubKey.Name
# return the values:
$returnValue
# close the subkey
$SubKey.Close()
}
# close the regkey
$RegKey.Close()
# close the hive
$RegHive.Close()
} | Out-GridView
心得:
该脚本我们学习到了如何去读取一个注册表,中间“$returnValue=1|Select-Object -Property $Value”这种用法让我们学习到如何快速创建一个带属性的集合,$Value = @($Value) + ‘RegPath’又让我们学习到如何追加集合,同时多重管道+嵌套循环的应用也充分反应出作者清晰的编程思路,此文不亏是一篇好的基础范例文章。
原文地址: Reading Installed Software Remotely
本文链接: https://www.pstips.net/reading-installed-software-remotely.html
请尊重原作者和编辑的辛勤劳动,欢迎转载,并注明出处!
请尊重原作者和编辑的辛勤劳动,欢迎转载,并注明出处!
