要写入主机的数组成员名称

时间:2018-11-20 12:12:03

标签: powershell

$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]

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

您的IfElseIf比较目前不起作用:

  • =用于设置值,-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."
        }
    }
}