【问题标题】:Powershell script cannot get applications list data from windows 7 machinePowershell 脚本无法从 Windows 7 机器获取应用程序列表数据
【发布时间】:2011-02-05 17:19:21
【问题描述】:

最近,我编写了一个脚本来列出本地和远程机器中所有已安装的应用程序,并在 excelsheet 中以结构化的方式给出输出。

看起来像这样:

$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\Microsoft\Windows\CurrentVersion\Uninstall”而不是

“SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall”。

使用这个不同的路径,当我再次运行相同的脚本时,我得到一个错误:

使用“2”参数调用“OpenRemoteBaseKey”的异常:“找不到网络路径。

"

在:line:24 char:62

  • $BaseKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey(

可能,我还需要在脚本中更改其他内容? 我运行脚本的机器是 Windows XP SP3 机器。

【问题讨论】:

    标签: powershell registry


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

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

      Get-WmiObject Win32_Product
      

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

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

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

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

      【讨论】:

        【解决方案3】:

        我记得前段时间我在一家 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
        

        【讨论】:

          猜你喜欢
          • 2020-10-09
          • 1970-01-01
          • 2022-07-03
          • 2017-01-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多