【发布时间】:2022-01-08 06:13:07
【问题描述】:
大家好, 我浏览了一些以前的帖子,不幸的是,无法找到适用于我的特定情况的答案。背景是我有一个脚本,它删除了 Visual Studio 及其所有有效的目录路径/注册表条目。我遇到的问题是验证。我正在尝试验证某些路径/目录已被删除。
发生的情况是,该函数只会查看第一个“if”语句,并且似乎会忽略我包含的其他 elseif 和 else 语句。我知道我可能将它们格式化错误,因为我是 powershell 的新手。可能有更好的方法来做到这一点,如果是这样,请随时分享。代码如下:
function scriptvalidation{
$l1 = Test-Path -path "Path placeholder Number One"
$l2 = Test-Path -path "Path placeholder Number Two"
$l3 = Test-Path -path "Path placeholder Number Three"
$r1 = Test-Path -path "Registry path Number One"
$r2 = Test-Path -path "Registry path Number Two"
$r3 = Test-Path -path "Registry path Number Three"
$DirectoryArray = @($l1, $l2, $l3)
$RegistryArray = @($r1, $r2, $r3)
上面是我使用的变量和数组,下面是语句:
if ($l1 -eq $true) {
Write-Host ("Path Placeholder number one was found and needs to be removed")
}
elseif ($l2 -eq $true) {
Write-Host ("Path Placeholder Number Two was found and needs to be removed")
}
elseif ($l3 -eq $true) {
Write-Host ("Path Placeholder Number three was found and needs to be removed")
}
else{[String] "$DirectoryArray -eq $false" Write-Host "Directory paths were removed successfully"
}
if ($r1 -eq $true) {
Write-Host ("Registry Placeholder Number One was found and needs to be removed")
}
elseif ($r2 -eq $true) {
Write-Host ("Registry Placeholder Number Two was found and needs to be removed")
}
elseif ($r3 -eq $true) {
Write-Host ("Registry Placeholder Number Three was found and needs to be removed")
}
else{[String] "$RegistryArray -eq $false Write-Host "Registry paths were removed, script completed successfully"
}
scriptvalidation
我无法正常运行的这部分脚本到此结束。当我安装有问题的程序时,我得到的输出如下: “找到第一个路径占位符,需要删除” “已找到第一个注册表占位符,需要删除” 仅此而已。
我知道我设置错了,但是作为新手,很难知道哪里错了。
任何帮助将不胜感激。
【问题讨论】:
标签: arrays powershell if-statement path format