如何在PowerShell中解析数​​组

时间:2015-09-04 19:06:07

标签: powershell

$serversoffline = @()
function checkServerByName {
    $serverlist = Get-Content $SERVERLIST
    $processlist = Get-Content $PROCESSMONITOR
    $results = @()
    $processnotrunning = @()

    foreach ($server in $serverlist) {
        if((Test-Connection -Cn $server -BufferSize 16 -Count 1 -ea 0 -quiet)) {
            Write-Host "Server Online"
            Write-Host "Checking For Process"
            foreach($process in $processlist) {
                checkProcessByName $server $process
            }
        } else {
            Write-Host "Server Offline"
            $serversoffline += $server
        }
    }
    sendEmailServersOffline
}

function sendEmailServersOffline {
    $message = $message + "The following servers are OFFLINE as of $((get-date).DateTime)." + "<br><br>"
    foreach($server in $serversoffline)
    {
        $message += $server + "<br>"
    }
    Send-MailMessage -To $to -Subject $subject -BodyAsHtml $message -From $from -Credential $cred -SmtpServer $emailserver -Debug
}

checkServerByName

上述代码不起作用。它将所有服务器放在一起,没有任何空格或换行符

1 个答案:

答案 0 :(得分:1)

除非你在其他地方声明,否则你不会将$serversoffline声明为数组。你只是简化字符串。

例如:

PS C:\Users\mmaeda> $Concat += "This"
PS C:\Users\mmaeda> $Concat += "That"
PS C:\Users\mmaeda> $Concat
ThisThat
PS C:\Users\mmaeda> $Array = @()
PS C:\Users\mmaeda> $Array += "This"
PS C:\Users\mmaeda> $Array += "That"
PS C:\Users\mmaeda> $Array
This
That

作为旁注:$results$processnotrunning的内容是什么?我没有看到它们被用于当前代码中的任何内容。