powershell - 测试路径在应为true时返回false

标签 powershell

我想知道test-path为什么用两个语句返回true和false,有人可以解释或建议原因吗?

$app2find = "Easeus"

### search ###
$appSearch = Get-ChildItem -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall, HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall  |
    Get-ItemProperty |
        Where-Object {$_.DisplayName -match $app2find } |
            Select-Object -Property DisplayName, UninstallString

### search results ###
if (!$appSearch) { "No apps named like $app2find were found" }

### uninstall ###
ForEach ($app in $appSearch) {

    If ($app.UninstallString) {

        Test-Path $app.UninstallString
        Test-Path "C:\Program Files (x86)\EaseUS\EaseUS Partition Master 10.5\unins000.exe"

        #& cmd /c $($app.UninstallString) /silent
    }
}

输出:
False
True

所需的输出:
True
True

非常感谢

*编辑
$app.UninstallString是注册表中的值,它提供了卸载特定应用程序的方法。在这种情况下,将完全打印:
"C:\Program Files (x86)\EaseUS\EaseUS Partition Master 10.5\unins000.exe"

最佳答案

我认为Etan根据您向我们展示的内容正确。我们唯一能想到的是$app.UninstallString不像您认为的那样包含绝对路径。最好的猜测是该字符串已在注册表中被引用。 Test-Path无法解析带有外引号的字符串。

考虑以下示例

PS Z:\> test-path "c:\temp"
True

PS Z:\> test-path "'c:\temp'"
False

PS Z:\> test-path "'c:\temp'".Trim("'")
True 

也许您只需要修剪引号?

Test-Path $app.UninstallString.Trim("'`"")

那应该删除尾随和前导的单引号和双引号。

关于powershell - 测试路径在应为true时返回false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30196984/

相关文章:

windows - 如何将路径映射到 svn 使用的底层位置的正确大小写

powershell - 在远程计算机上监视进程时获取进程命令给出负值

c# - 自定义 PSHostUserInterface 被 Runspace 忽略

powershell - 重用静态代码的最佳方法是什么

powershell - PowerShell 中 Power BI Rest API 的错误处理

powershell - 在 PowerShell 中替换子字符串

sql-server - 用于 DACPAC 部署的 SQL Azure 和 Powershell

azure - 是否可以通过 Powershell 访问 Azure 中安全中心建议的严重性?

powershell - 使用 PowerShell 作为默认 Shell,但在批处理文件运行时将其丢弃

c# - 如何插入软连字符