【问题标题】:How can I finish this PowerShell script?我怎样才能完成这个 PowerShell 脚本?
【发布时间】:2022-01-31 01:42:22
【问题描述】:

我的任务是从所有 Windows 10 计算机上卸载“Rhino 6”。如果我跑:

gci "HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall" | foreach { gp $_.PSPath } | ? { $_ -match "Rhino 6" } | select UninstallString

然后我得到一个非常有用的:

UninstallString
---------------
"C:\ProgramData\Package Cache\{680b3429-6aab-4318-a4e1-74347755c79d}\Bootstrapper.exe"  /uninstall

所以,我写了这个:

Start-Process "C:\ProgramData\Package Cache\{75519ff1-80d4-4471-96c0-970328e182f1}\Bootstrapper.exe" -ArgumentList "/uninstall -quiet"

这很好用,除了有许多版本的“Rhino 6”并且它们每个都吐出不同的卸载字符串。

如何制作一个执行第一个命令的脚本,然后使用第二个命令中答案的路径,而不会吐出所有其他内容?

【问题讨论】:

  • 如果您正在寻找一种通用方法来调用存储在UninstallString 注册表值中的命令行,请参阅this answer
  • 这能回答你的问题吗? How to uninstall MSIs using the Uninstall Path
  • 我在那篇帖子中没有看到我的问题的答案。我在问如何去除输出的“UninstallString ---------------”部分,以便我可以将它用作变量。

标签: windows powershell script


【解决方案1】:

这样的事情应该可以工作。分配 get child item (gci) 的输出,然后 foreach 循环访问分配给 $rhino.UninstallString 的值

#HashTable
$rhino = gci "HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall" | foreach { gp $_.PSPath } | ? { $_ -match "Rhino 6" } | Select UninstallString
#Output of $rhino: @{UninstallString=C:\somepath\some.exe}@{UninstallString=C:\someotherpath\some.exe}

#Loop through assigning each to $uninstall and use $uninstall.UninstallString to start-process
foreach ($uninstall in $rhino) {
    Start-Process $uninstall.UninstallString -ArgumentList "/uninstall -quiet"
}

【讨论】:

  • 您好,这不起作用,因为 $rhino 的输出有一堆附加文本,即: UninstallString ---------------
  • 卸载字符串 ---- 不会分配给 $rhino 变量。您可以通过执行 'write-host $rhino' 来验证这一点
  • 查看example 请注意,输出中缺少UninstallString -----------------
  • 您在上面运行的 gci 命令未将其分配给变量 $rhino 将简单地输出表格。有关 cmdlet 的默认输出的更多信息可以找到 here
  • 奇怪的是它在测试中不起作用。我得玩这个。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-17
  • 2015-01-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多