【问题标题】:powershell to un-install muliple applications ?powershell 卸载多个应用程序?
【发布时间】:2011-12-17 19:51:42
【问题描述】:

我是 PowerShell 新手,我正在寻找一种卸载多个应用程序的方法。我在一个文本文件中有一个要卸载的应用程序列表。这是我到目前为止的代码:

# Retrieve names of all softwares to un-install and places in variable $app

$App = Get-Content "C:\temp\un-installApps.txt"

# Cycle through each of the softwares to un-install and store in the WMI variable

Foreach ($AppName in $App)
{
    $AppTmp = Get-WmiObject -query "Select * from win32_product WHERE Name like" + $AppName 
    $AppNames = $AppNames + $AppTmp
}

foreach ($Application in $AppNames )
{
    msiexec /uninstall $Application.IdentifyingNumber
}

以下几行会导致问题

$AppTmp = Get-WmiObject -query "Select * from win32_product WHERE Name like" + $AppName 
$AppNames = $AppNames + $AppTmp"

有什么想法可以让它工作吗?

【问题讨论】:

    标签: powershell wmi powershell-2.0 uninstallation wmi-query


    【解决方案1】:

    我认为是因为like和应用程序名称之间没有空格,应用程序名称周围需要有单引号。该部分应类似于like '" + $AppName + "'"

    但是,您可以像这样更简单地编写整个脚本:

    $App = Get-Content "C:\temp\un-installApps.txt"
    
    gwmi win32_product|
        where { $App -contains $_.Name }|
        foreach { $_.Uninstall() } | out-null
    

    【讨论】:

    • Void 引起了我的问题,所以我使用了这个:$App = Get-Content "C:\temp\un-installApp.txt" gwmi win32_product|其中 { $App -包含 $_.Name }| foreach { $_.Uninstall() }
    • 试试@manojlds 编辑。如果我将管道括在括号中,[void] 会起作用。
    • 是否有“不重启”通用命令行标志?或者那个程序是特定的? (脚本正在运行,但它在我没想到的时候重启了我的机器!)
    • @granadaCoder Uninstall() 方法没有选项。您可以将foreach 更改为使用msiexec。它看起来像 `foreach { msiexec.exe /norestart /q/x $_.IdentifyingNumber REMOVE=ALL }' 请参阅:social.technet.microsoft.com/Forums/scriptcenter/en-US/…
    猜你喜欢
    • 1970-01-01
    • 2018-08-11
    • 2016-01-14
    • 2010-09-11
    • 2011-03-25
    • 1970-01-01
    • 1970-01-01
    • 2020-07-01
    • 1970-01-01
    相关资源
    最近更新 更多