【问题标题】:using POWERShell CmdLet to list installed Software and extract software name使用 POWERShell CmdLet 列出已安装的软件并提取软件名称
【发布时间】:2014-11-27 18:41:06
【问题描述】:

我有一个 POWERSHELL 脚本来检查是否存在预定义的软件列表: $SoftList = "Microsoft Visual C++ 2005 Redistributable","TotoInexistant","GIMP 2.6.11" 定义我要检查的软件列表。

我使用一个循环来比较上面列表中每个软件的名称,以便 cmdlet 的结果如下:

 foreach($i in $SoftList)
{
$x = Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | 
select DisplayName, Publisher, InstallDate | Where-Object {$_.DisplayName -like $i} 
$x
$x.DisplayName >> ListOf.txt # or we can to display it on screen 
}

我面临的问题是 $x.DisplayName 没有写出“Microsoft Visual C++ 2005 Redistributable”的名称。 如何从 $x 变量中提取软件名称?

【问题讨论】:

  • 你检查过输出的包吗?我没有安装它,所以我不能为你做。只需运行Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*,手动查看哪个包是mvc++ 2005,看看是否有包含名称的属性
  • 是的,已经检查过了。事实上,当我使用 $x 显示它时,DisplaynamepublisherinstallDate 都会显示在屏幕上。问题是当我尝试使用 $x.DisplayName 访问它时
  • 嗯,这很奇怪,所以 $x 包含一个属性 Displayname,它有一个值但 $x.Displayname 不输出它?
  • exactly $x.displayName 适用于 GIMP 和其他软件,但对于 Microsoft Visual C++ 2005 Redistributable 则失败。也许是长度问题!
  • 在我的系统上,该软件称为Microsoft Visual C++ 2005 Redistributable (x64),这就是它不会显示的原因。请参阅 Arco 的回答。

标签: powershell scripting powershell-cmdlet


【解决方案1】:

你可以试试这个版本吗?它正在使用您的代码,并带有 Arco 的修复程序。

$SoftList = "Microsoft Visual C++ 2005 Redistributable","TotoInexistant","GIMP"

foreach($i in $SoftList)
{
    $x = Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | 
    select DisplayName, Publisher, InstallDate | Where-Object {$_.DisplayName -like $("$i*")} 

    $x.DisplayName 

    $x.DisplayName | Out-File "ListOf.txt" -Append -Force
} 

我得到的输出是:

Microsoft Visual C++ 2005 Redistributable (x64)
GIMP 2.8.14

【讨论】:

  • 当我将此版本与 64 位 POWERShell 一起使用时,我得到输出:Microsoft Visual C++ 2005 Redistributable (x64)。它看起来不错,因为我没有安装 64 位版本。但是在 32 位 POWERSHELL 中,它不显示Microsoft Visual C++ 2005 Redistributable (x64)
  • 我不知道,但在显示 $x 变量时。 “Microsoft Visaul c++ Redistribuable”出现了两次。也许这会阻止使用命令 $x.DisplayName 获取名称。
【解决方案2】:

感谢您的帮助, 我找到了解决方案,实际上Microsoft Visual C++ 2005 Redistributable 的 $x 变量是一个数组,可能是因为它安装了两次,
Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | select DisplayName, Publisher, InstallDate
返回两行包含Microsoft Visual C++ 2005 Redistributable 的信息。 在这种情况下,为了获得 displayName,我将我的脚本调整如下:

 if($x-is [array])
 {
   for($k=0;$k-le $x.length-1;$k++)
   {
     $x[$k].DisplayName
   } 
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-13
    • 2022-01-16
    • 1970-01-01
    • 1970-01-01
    • 2021-03-13
    • 1970-01-01
    相关资源
    最近更新 更多