I currently have a script that pings a list of servers and checks the status of services running on each server. I am wanting to write to log.csv.
I want to show which computers are offline and show which service is in the Stopped status.
How can I get the computer or machine name with PSCustumObject? The CSV output just has a line that says offline but it doesn't list a computer name in front of it.
$serviceList = Get-Content C:\services.txt
$results = Get-Content C:\servers.txt | ForEach-Object {
if (Test-Connection -ComputerName $_ -BufferSize 16 -Count 1 -EA 0 -Quiet) {
foreach ($service in $serviceList) {
if ($s=get-service -computer $_ -name $service -ErrorAction SilentlyContinue)
{
$s | select MachineName, ServiceName, Status, StartType
} else {
# "$_ - Service '$service' does not exist."
}
}
} else {
$status = Write-Output "Offline"
}
[pscustomobject][ordered]@{
Machine = $_
Status = $status
}
}
$results | Export-CSV C:\log.csv -notypeinformation -Append