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

The above code doesn't work. It puts all the servers together without any spaces or linebreaks

4
  • 1
    How are you printing/outputting $msg? Why do you think <br> is going to do anything for you there? Commented Sep 4, 2015 at 19:06
  • Yes BR puts line breaks in my HTML email. PowerShell is creating the message then emailing it. Commented Sep 4, 2015 at 19:10
  • <br> is an HTML tag and does not create a line feed in PowerShell output. Use "n"` for line feeds. Commented Sep 4, 2015 at 19:13
  • Yes but I am creating an HTML email. When the email is sent, there is 2 line breaks after the Date/Time and before the "servers" Commented Sep 4, 2015 at 19:21

1 Answer 1

1

It would apear that( unless you are declaring it elsewhere ) you are not declaring $serversoffline as an array. You are simply concatinating strings.

Example:

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

As a side note: What are $results and $processnotrunning for? I dont see them being used for anything in the current code.

Sign up to request clarification or add additional context in comments.

1 Comment

What does the output look like? And what does the input look like(serverlist, processlist)? I'm thinking it could be a problem with either scoping or input.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.