$mbservers = @("server1", "server2")
Foreach ($mbserver in $mbservers) {
#Check for reg key
Invoke-Command -Computername $mbserver -Credential $credsschedtask -ScriptBlock {
$regkey = "HKLM:\SYSTEM\CurrentControlSet\Control\Lsa\DisableLoopbackCheck"
$checkregkey = (Test-Path $regkey)
If ($checkregkey = "True") {
Write-Host "'DisableLoopbackCheck' key exists on $mbserver".
}
ElseIf ($checkregkey = "False") {
Write-Host "'DisableLoopbackCheck' key does not exist on $mbserver."
}
Else {
Write-Host "Unable to confirm if 'DisableLoopbackCheck' key exists on $mbserver."
}
}
}
我正在尝试锻炼Write-Host
变量,使其显示为server1
等
(Write-Host "'DisableLoopbackCheck' key exists on $mbserver".
)
以各种方式尝试了以下方法:
For($i=0; $i -le $mbservers.getupperBound(0);$i++){
write-host $mbservers[$i]
write-host "line 2 :," $mbservers[$i]
感谢您的帮助。
答案 0 :(得分:1)
您的If
和ElseIf
比较目前不起作用:
=
用于设置值,-eq
用于比较值Test-Path
不返回字符串"True"
,而是返回布尔值$True
由于布尔仅具有两个状态$True
或$False
,因此您只需要If/Else
:
Foreach ($mbserver in $mbservers) {
#Check for reg key
Invoke-Command -Computername $mbserver -Credential $credsschedtask -ScriptBlock {
$checkregkey = Test-Path "HKLM:\SYSTEM\CurrentControlSet\Control\Lsa\DisableLoopbackCheck"
If ($checkregkey -eq $True) {
Write-Host "'DisableLoopbackCheck' key exists on $mbserver".
}
Else {
Write-Host "'DisableLoopbackCheck' key does not exist on $mbserver."
}
}
}