我正在编写一个简单的PowerShell脚本来检查文件的两个可能位置。检查数组后,如果值返回True,则会添加一个注册表项。但是,如果值为false,则返回表明未安装的消息。
# Verify if directory exists
$reg6432path = "HKLM:\SOFTWARE\WOW6432Node\xabx\123"
$reg32path = "HKLM:\SOFTWARE\xabx\123"
$CheckLoc64 = "${env:ProgramFiles(x86)}\xabx\123.abc"
$CheckLoc32 = "${env:ProgramFiles}\xabx\123.abc"
$CheckLocArray = $CheckLoc32, $CheckLoc64
$PathExists = Test-Path $CheckLocArray
IF ($PathExists -eq $True) {
write-host "xabx is installed"
write-host "Writing registry values"
new-itemProperty -Force -Path $reg6432path -Name "LogonSettingsPath" -Value $CheckLoc64
}
ELSE { new-itemProperty -Force -Path $reg32path -Name "LogonSettingsPath" -Value $CheckLoc32 }
IF ($PathExists -eq $False) {
write-host "xabx Not Installed" }
这有效,但我很好奇是否有任何改进可以帮助清理它。
由于
答案 0 :(得分:0)
如果使用test-path检查数组,并且1为true而另一个为false,则返回true。你需要个性化测试。 我会做这样的事情。
# Verify if directory exists
$reg6432path = "HKLM:\SOFTWARE\WOW6432Node\xabx\123"
$reg32path = "HKLM:\SOFTWARE\xabx\123"
$CheckLoc64 = "${env:ProgramFiles(x86)}\xabx\123.abc"
$CheckLoc32 = "${env:ProgramFiles}\xabx\123.abc"
$Path64 = Test-Path $CheckLoc64
$Path32 = Test-Path $CheckLoc32
IF ($Path64 -eq $True) {
write-host "xabx is installed"
write-host "Writing registry values"
new-itemProperty -Force -Path $reg6432path -Name "LogonSettingsPath" -Value $CheckLoc64
}
elseif ($Path32 -eq $True) {
new-itemProperty -Force -Path $reg32path -Name "LogonSettingsPath" -Value $CheckLoc32
}
else {
write-host "xabx Not Installed"
}
或者你可以在if语句中编入索引。 [1]告诉if语句查看变量$ PathExists中的第二个对象。
# Verify if directory exists
$reg6432path = "HKLM:\SOFTWARE\WOW6432Node\xabx\123"
$reg32path = "HKLM:\SOFTWARE\xabx\123"
$CheckLoc64 = "${env:ProgramFiles(x86)}\xabx\123.abc"
$CheckLoc32 = "${env:ProgramFiles}\xabx\123.abc"
$CheckLocArray = $CheckLoc32, $CheckLoc64
$PathExists = Test-Path $CheckLocArray
IF ($PathExists[1] -eq $True) {
write-host "xabx is installed"
write-host "Writing registry values"
new-itemProperty -Force -Path $reg6432path -Name "LogonSettingsPath" -Value $CheckLoc64
}
ELSE { new-itemProperty -Force -Path $reg32path -Name "LogonSettingsPath" -Value $CheckLoc32 }
IF ($PathExists -eq $False) {
write-host "xabx Not Installed"
}