1

Pretty new to powershell and just playing with some code here but ran into something I cannot find an answer to. I have a function that gets $serverList containing a server name and service name and returns output with the status of each service. I have this in a loop running every few seconds, but only the first run of the function contains column herders, after that I only get the data, no headers. What's the deal here!

Function ServiceStatus()
{
    $objOutput = @()
    foreach ($objRecord in $serverList)
    {
        $obj = New-Object System.Object
        $serviceStatus = get-service -ComputerName $objRecord.Server -Name $objRecord.Service
        $obj | Add-Member -membertype NoteProperty -name Server -value $objRecord.Server
        $obj | Add-Member -membertype NoteProperty -name Service -value $objRecord.Service
        $obj | Add-Member -membertype NoteProperty -name Status -value $serviceStatus.status
        Write-Output $obj
    }    
}

For(;;)
{
    $currentDate = Get-Date
    clear-host
    Write-Host Service Checked: $currentDate
    ServiceStatus
    Start-Sleep -Milliseconds 5000
}

1 Answer 1

2

When looping, default behavior is to append only the data without headers.

PS:> while (1) { Start-Sleep 1; get-process powershell}

Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id ProcessName
-------  ------    -----      ----- -----   ------     -- -----------
    487      34   100608     107400   636     8.07   4916 powershell
    450      34   100632     107824   636     8.08   4916 powershell
    450      34   100632     107872   636     8.08   4916 powershell
    452      34   100948     108188   636     8.08   4916 powershell
    450      34   101264     108504   636     8.08   4916 powershell

Specify the format if you want headers.

PS:> while (1) { Start-Sleep 1; get-process powershell|ft}

Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id ProcessName
-------  ------    -----      ----- -----   ------     -- -----------
    512      34   102592     106756   636     8.16   4916 powershell

Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id ProcessName
-------  ------    -----      ----- -----   ------     -- -----------
    463      34   103164     107340   636     8.17   4916 powershell
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.