Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 卸载以特定字符串开头的所有软件_Powershell_Batch File_Cmd_Command Line_Uninstallation_Wmic - Fatal编程技术网

Powershell 卸载以特定字符串开头的所有软件

Powershell 卸载以特定字符串开头的所有软件,powershell,batch-file,cmd,command-line,uninstallation,wmic,Powershell,Batch File,Cmd,Command Line,Uninstallation,Wmic,下面,我想卸载所有的国家仪器软件。从第一次开始,在CMD中输入wmic。然后使用命令product get name我得到一组软件,都是从NI开始的: NI Logos 19.0 NI Trace Engine NI-MXDF 19.0.0f0 for 64 Bit Windows WIF Core Dependencies Windows 19.0.0 NI-VISA USB Passport 19.0.0 NI-VISA SysAPI x64 support 19.0.0 NI Contro

下面,我想卸载所有的国家仪器软件。从第一次开始,在CMD中输入
wmic
。然后使用命令
product get name
我得到一组软件,都是从
NI
开始的:

NI Logos 19.0
NI Trace Engine
NI-MXDF 19.0.0f0 for 64 Bit Windows
WIF Core Dependencies Windows 19.0.0
NI-VISA USB Passport 19.0.0
NI-VISA SysAPI x64 support 19.0.0
NI Controller Driver 19.0 64-bit
NI ActiveX Container (64-bit)
Math Kernel Libraries
NI MXS 19.0.0
NI LabWindows/CVI 2019 Network Variable Library
NI-VISA GPIB Passport 19.0.0
NI LabWindows/CVI 2017 Low-Level Driver (Original)
NI-RPC 17.0.0f0 for Phar Lap ETS
NI LabWindows/CVI 2017 .NET Library (64-bit)
...
我可以单独卸载它们,例如:

product where name="NI Logos 19.0" call uninstall
然后我必须选择
y
/
y
。考虑到我必须卸载很多这样的软件,我想知道如何使这个过程自动化。步骤应如下所示:

  • 找到
    product get name
    中以
    NI
    开头的所有行,并从中列出一个列表
  • 上面列表中运行的for循环,其中name=list[i]调用uninstall,默认值为
    y
    /
    y
  • 如果你能帮我解决这个问题,我将不胜感激。提前感谢您的支持


    p.S.Powershell解决方案也可以。事实上,使用任何其他方法卸载所有这些的任何其他解决方案对我来说都是可以的

    如果应用程序是从MSI安装的,则可以使用以下PowerShell代码。如果使用了其他安装程序,则可以将静默卸载参数添加到循环中的
    $uninstallString

    $productNames  = @("^NI")
    $uninstallKeys = @('HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall',
                       'HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall')
    
    foreach ($key in (Get-ChildItem $uninstallKeys)) 
    {
        foreach ($productName in $productNames)
        {
            $name = $key.GetValue("DisplayName")
            if ($name -match $productName) 
            {
                $uninstallString = $key.GetValue("UninstallString")
                if ($uninstallString -match "^msiexec(\.| )")
                {
                    $uninstallString = ($uninstallString -replace "/I{","/X{" -replace "/X{", '/X "{' -replace "}",'}"')  + " /qn /norestart"
                }
    
                Write-Host "Removing '$name' using '$uninstallString'..."
                & cmd.exe /C $uninstallString
            }
        }
    }
    

    您应该能够将
    Like
    运算符与一起使用

    WMIC产品,其中“名称如‘NI%”调用卸载/NoInteractive
    

    WMIC产品,其中“名称如'NI%%'”调用卸载/NoInteractive
    

    没有可用于
    卸载调用的命令行选项文档,因此这里提供的使用
    /NoInteractive
    更多的是希望,而不是作为所述提示的最终解决方案。

    您忘了提到这是一个
    wmic
    命令,我猜你没有完全遵循@LotPings不是
    wmic
    CMD命令的提示吧?有什么区别?我没有看到上面的帖子。我使用了。@Foad
    %SystemRoot%\System32\wbem\wmic.exe
    是一种可执行文件,它输出的文本不是像大多数可执行文件那样每个字符有一个字节,而是使用UTF-16小尾端编码的Unicode。在处理
    wmic
    的文本输出时,必须考虑字符编码的这种差异。尝试过这种方法后,软件似乎正在卸载,但重新启动后,一切仍然存在!如果是这样的话,那么这不是我回答的错误,我使用了与你所说的相同的
    WMIC
    方法。您是否以管理员身份运行脚本,以确保所需的卸载权限已到位?这确实不是您回答的错误,但事实上我认为我在原始帖子中引入的命令正在工作。显然不是这样!我以前没有看到
    Call Uninstall
    没有按照您描述的方式工作的迹象,当然也没有在调用时显示出某种错误,毕竟这是一种有文档记录的方法。另外,您知道使用
    win32\u产品
    仅涵盖使用Windows Installer安装的产品,对吗?我不确定是否正确理解了这一点。“静默卸载参数”是什么意思?如果您能详细说明,我将不胜感激。嗯,我不知道这些卸载程序需要哪些参数,因此可能需要添加一个
    /s
    /quiet
    或其他什么,使它们无人值守。但是只需在
    &cmd…
    语句之前添加一个
    #
    ,然后运行代码。如果所有使用的卸载字符串都以msiexec开头,则无需更改任何内容