首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >希望使用PowerShell将不同字符串拆分为多个部分的通用方法

希望使用PowerShell将不同字符串拆分为多个部分的通用方法
EN

Stack Overflow用户
提问于 2020-05-26 14:04:32
回答 2查看 302关注 0票数 2

我正在研究一种从注册表读取UninstallStrings软件的方法。然后我尝试执行这些字符串来卸载软件。

当我打印出保存字符串信息的变量时,它会正确地打印整个字符串(带有参数),但我无法运行这些字符串。这个问题有多个部分。

有些安装字符串的格式如下

代码语言:javascript
复制
c:\file path\blah\app.exe /uninstall
"c:\file path\blah\app.exe" /uninstall
c:\file path\blah\app.exe --uninstall
'c:\file path\blah\app.exe' /uninstall

我想要做的是找出最佳的方法,以便能够以“通用”的方式运行卸载程序。有办法有效地做到这一点吗?

我试着用两种不同的方式执行字符串。

代码语言:javascript
复制
    & $uninstaller

代码语言:javascript
复制
    Start-Process -FilePath cmd.exe -ArgumentList '/c', $uninstaller -Wait 

两者似乎都不起作用。没有错误,但它们似乎没有运行,因为当我检查应用程序时,它仍在安装。

我试着把课文分成几个部分。

代码语言:javascript
复制
    $Uninstaller.split("/")[0]
    $Uninstaller.split("/",2)[1]
    $($Uninstaller) | Invoke-Expression
    $Uninstaller.Substring(0,$Uninstaller.lastIndexOf('.exe '))
    $Uninstaller.split('^(*?\.exe) *')

提前感谢!

EN

回答 2

Stack Overflow用户

发布于 2020-05-26 21:02:12

弄明白了。也许有更好的方法,但这似乎对我有用。

代码语言:javascript
复制
CLS

$Software = "OneDrive"
$Filter = "*" + $Software + "*"
$Program = $ProgUninstall = $FileUninstaller = $FileArg = $NULL

try 
{
    if (Test-Path -Path "HKLM:\SOFTWARE\WOW6432Node") 
    {
        $programs = Get-ItemProperty -Path "HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*" -ErrorAction Stop
    }

    $programs += Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*" -ErrorAction Stop
    $programs += Get-ItemProperty -Path "Registry::\HKEY_USERS\*\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*" -ErrorAction SilentlyContinue
} 
catch 
{
    Write-Error $_
    break
}

foreach($Program in $Programs)
{
    $ProgDisplayName = $Program.DisplayName
    $ProgUninstall = $Program.UninstallString

    if($ProgDisplayName -like $Filter)
    {
        if($ProgUninstall -like "msiexec*")
        {
            $FileUninstaller = $ProgUninstall.split(" ")[0]
            $FileArg = ($($ProgUninstall).split(" ",2)[1])
        }
        else
        {
            if(($ProgUninstall -like '"*"*') -or ($ProgUninstall -like "'*'*"))
            {
                #String has quotes, don't need to do anything

            }
            else
            {
                if($NULL -ne $ProgUninstall)
                {
                    #String doesn't have quotes so we should add them
                    $ProgUninstall = '"' + ($ProgUninstall.Replace('.exe','.exe"'))                    
                }
            }

            #Let's grab the uninstaller and arguments
            $FileUninstaller = $ProgUninstall.split('"')[1]
            $FileArg = $ProgUninstall.split('"')[-1]
        }

        #Debug
        #$FileUninstaller
        #$FileArg

        #Run the Uninstaller
        Start-Process $FileUninstaller -ArgumentList $FileArg -wait -ErrorAction SilentlyContinue
    }
}
票数 0
EN

Stack Overflow用户

发布于 2020-05-26 21:44:51

这对于msi安装非常容易:

代码语言:javascript
复制
get-package *whatever* | uninstall-package

下面是一个非msi静默卸载示例,但您必须添加"/S“或任何静默卸载选项:

代码语言:javascript
复制
get-package notepad++* | 
  % { & $_.Meta.Attributes['UninstallString'] /S }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62023960

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档