如果应该为真,则测试路径返回false

时间:2015-05-12 16:49:45

标签: powershell

我想知道为什么test-path在两个陈述中返回真假,是否可以解释或建议原因?

$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"

1 个答案:

答案 0 :(得分:3)

我认为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("'`"")

这应该删除尾随和领先的单引号和双引号。

相关问题