首页 > 解决方案 > 来自注册表提取的 PowerShell 剩余字符

问题描述

从注册表中提取此安装字符串时,有一个不可见的前导字符。

我无法运行卸载字符串或删除此字符。拆分、替换、连接等的各种迭代对字符串起作用,但对改变错误行为没有任何作用。我在 PowerShell 或 Windows 控制台中尝试过。

Write-Output $uninst显示正确的字符串:

MsiExec.exe  /x {1F4D7BAB-E816-43DF-B4B1-5A41A2DA13E8} /qn

在 PowerShell 中执行该字符串时,会msiexec弹出帮助气泡。在 Windows CMD shell 中执行该字符串时,白色方形字符位于行首。

# pull ESET uninstall string

$esetVer = Get-ChildItem -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall, HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall |
           Get-ItemProperty |
           Where-Object { $_.DisplayName -match "ESET Endpoint Antivirus" } |
           Select-Object -Property DisplayName, UninstallString

foreach ($ver in $esetVer) {
    if ($ver.UninstallString) {
        $uninst = $ver.UninstallString
        $uninst = $uninst.Replace('/I{',' /x {').Replace('}','} /qn')
        Invoke-Expression $uninst
        Write-Output $uninst
    }
}

删除第一个字符只会删除 M。

性格不好

标签: powershell

解决方案


在我的问题中,我关注 msiexec.exe 命令行之前的一些错误字符。这显然不是问题所在。问题在于应用 ID ( {1F4D7BAB-E816-43DF-B4B1-5A41A2DA13E8} ) 周围的括号。他们需要一个反引号。因此,只需在我的替换行中的大括号之前包含反引号即可修复代码。

旧:$uninst = $uninst.Replace('/I{',' /x {').Replace('}','} /qn')

新:$uninst = $uninst.Replace('/I{',' /x {').Replace('}','} /qn')

此修改适用于 2 台 Windows 7 Pro 计算机。


推荐阅读