我想知道为什么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"
答案 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("'`"")
这应该删除尾随和领先的单引号和双引号。