0

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
Kade Williams
  • 1,041
  • 2
  • 13
  • 28

1 Answers1

2

When you're outputting the results from Get-Service you're selecting MachineName:

$s | select MachineName, ServiceName, Status, StartType

Then for the offline computer you're building an object using Machine:

[pscustomobject][ordered]@{
    Machine = $_
    Status = $status
}

You need to update your custom object to MachineName so it matches the properties you selected above.

It also needs to move into the else{} (replacing $status = Write-Output "Offline") so it's only called when the computer is offline.

You'll then get the output you're expecting:

MachineName ServiceName    Status StartType
----------- -----------    ------ ---------
localhost   spooler       Running Automatic
localhost   DusmSvc       Running Automatic
localhost   DeviceInstall Stopped    Manual
noname                    Offline          

Updated code, with a bonus update for services that don't exist:

$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 {
                [pscustomobject][ordered]@{
                    MachineName = $_
                    ServiceName = $service
                    Status  = "NotFound"
                } 
            }
        }
    }
    else {
        [pscustomobject][ordered]@{
            MachineName = $_
            Status  = "Offline"
        } 
    }
}

$results | Export-CSV C:\log.csv -notypeinformation -Append
henrycarteruk
  • 12,708
  • 2
  • 36
  • 40
  • I just tried that and only 2 columns are outputted to the CSV. Do I need to add a ServiceName and StartType under the [pscustomobject]? **EDIT** I just added a ServiceName = $blank and StartType = $blank This seems to work. – Kade Williams Mar 06 '18 at 17:07
  • The line of output that has the Service status now has quotation marks around each word in the column. Do you happen to know why it does this? I'll test some things out and post another questions if I need. I didn't know if it was something simple. Example: "localhost" "spooler" "Running" "Automatic" – Kade Williams Mar 06 '18 at 17:21
  • That is correct as it's the specification for a CSV, your code is working correctly. – henrycarteruk Mar 06 '18 at 17:24
  • You can remove the quotes: `$results | ConvertTo-CSV -NoTypeInformation | foreach {$_.replace('"','')} | Append-File C:\log.csv` – henrycarteruk Mar 06 '18 at 17:27