我想知道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/