2010-04-08 20 views
0

最近,我做了一个脚本,列出所有已安装的应用程序在本地&远程机器&以结构化的方式在excelsheet中给出输出。Powershell脚本无法从Windows 7机器获取应用程序列表数据

它看起来像这样:

$a = Read-Host "Enter machine name" | Out-File -filepath C:\machine.txt 
$computerName = Get-Content C:\machine.txt 
$a = New-Object -comobject Excel.Application 
$a.visible = $True 

$b = $a.Workbooks.Add() 
$c = $b.Worksheets.Item(1) 

$c.Cells.Item(1,1) = "Name" 
$c.Cells.Item(1,2) = "Publisher" 
$c.Cells.Item(1,3) = "InstalledDate" 
$c.Cells.Item(1,4) = "Version" 
$c.Cells.Item(1,5) = "UninstallString" 

$d = $c.UsedRange 
$d.Interior.ColorIndex = 19 
$d.Font.ColorIndex = 11 
$d.Font.Bold = $True 

$i = 2 
function Get-InstalledAppReg ([string]$ComputerName) { 

    $RegPath = "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall" 
    $BaseKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey("LocalMachine", $ComputerName) 
    $OpenSubKey = $BaseKey.OpenSubKey($RegPath) 
$i =2 
    $OpenSubKey.GetSubKeyNames() | ForEach { 
    $Path = "$RegPath\$_" 
    $BaseKey.OpenSubKey($Path).GetValue("DisplayName") 
     $BaseKey.OpenSubKey($Path).GetValue("Publisher") 
     $BaseKey.OpenSubKey($Path).GetValue("InstalledDate") 
     $BaseKey.OpenSubKey($Path).GetValue("Version") 
     $BaseKey.OpenSubKey($Path).GetValue("UninstallString") 
$c.Cells.Item($i,1) = $BaseKey.OpenSubKey($Path).GetValue("DisplayName") 
$c.Cells.Item($i,2) = $BaseKey.OpenSubKey($Path).GetValue("Publisher") 
$c.Cells.Item($i,3) = $BaseKey.OpenSubKey($Path).GetValue("InstalledDate") 
$c.Cells.Item($i,4) = $BaseKey.OpenSubKey($Path).GetValue("Version") 
$c.Cells.Item($i,5) = $BaseKey.OpenSubKey($Path).GetValue("UninstallString") 
$i ++ 
    } 
} 
Get-InstalledAppReg($computerName) 

$d.EntireColumn.AutoFit() 
$b.SaveAs("c:\softhive.xlsx") 
$b.Close() 
$a.Quit() 
Get-Process | Where { $_.Name -Eq "Excel" } | Kill 

这个脚本完全跑了具有XP作为操作系统的所有远程计算机。

问题开始时,我开始在远程机器上运行Windows &机器。

最初,它给了错误的道路的错误,当我意识到,对于Windows 7,我可能不得不使用

“SOFTWARE \ Wow6432Node \微软\的Windows \ CurrentVersion \卸载”,而不是

“SOFTWARE \微软\的Windows \ CurrentVersion \卸载”。

有了这个不同的道路,当我再次运行相同的脚本,我得到一个错误:

异常调用“OpenRemoteBaseKey”与“2”参数(S):“网络路径找不到

在:行:24字符:62

  • $ BaseKey = [Microsoft.Win32.RegistryKey] :: OpenRemoteBaseKey(< < < <“LocalMachine”,$ ComputerName)

也许我需要在脚本中更改其他东西? 我的机器,我从哪里运行脚本,是一台Windows XP SP3机器。

回答

0

而不是梳理注册表,我会为此使用WMI。见Win32_Productfriends如:

Get-WmiObject Win32_Product 

需要注意的是,如果我在64位我的Windows 7 x64系统上运行此PowerShell提示符下它会显示所有安装的应用程序(32位和64位):

Get-WmiObject Win32_Product| sort Vendor | Format-Table Name,InstallDate,Vendor 

要查看所有可用的属性执行:

Get-WmiObject Win32_Product | Select -First 1 | Format-List * 
1

不幸的是,WMI Win32_Product类不报告在控制面板的“添加或删除程序”中的所有应用程序.. 。

注册表散步似乎是不可避免的,请参阅: http://powergui.org/thread.jspa?threadID=17068 http://learningpcs.blogspot.fr/2011/10/powershell-get-installed-software.html

0

我记得前阵子我在一家IT公司做了这样的事情,我们只是在C:目录中搜索以.exe结尾的所有程序的名称,以便优化我们将要磨合的特定应用程序,我们正在寻找。我们建立了一个批次,可以根据我们想要的结果通过或失败。请记住这是一个批处理文件,但是这个想法很相似。

echo ================= >>Software_Scan.txt 
echo Below is a list of all wireless networks. Saved networks will be found in the Wireless Profiles folder 
set filePath= 
for /R "C:\Program Files (x86)" /D %%a in (*) do if exist "%%a\YahooMessenger.exe" set filePath=%%a& goto continue 
:continue 
if defined filePath echo %COMPUTERNAME% FAIL Yahoo Messenger >> Software_Scan.txt 
if NOT defined filePath echo %COMPUTERNAME% PASS Yahoo Messenger >> Software_Scan.txt 
相关问题