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
}