Skype for Business Service Health for Multiple Devices

83 Views Asked by At

I've been working at this script for a while and I can't seem to figure it out:

$servers = Get-Content -path c:\users\jason\documents\skyperservers.txt
Foreach ($server in $servers){get-cswindowservice -computername $servers | where-object {$_.status -eq "running"}}

I keep getting the error

Get-CSWindowService : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'ComputerName'. Specified method is not supported...

Essentially, I'm trying to display all services from the command for each skype server and their service health status whether "running" or "stopped"

1

There are 1 best solutions below

3
Theo On

Two things about your code:

  1. You are iterating the collection $servers using variable $server, but you do not use that as parameter for the Get-CSWindowService cmdlet. Instead you feed it the entire collection $servers (System.Object[]), which is what the error is telling you.
  2. If you also want to see servers where the CsWindowService is stopped, add this to your Where-Object clause and best return objects so you can combine the service status with the server name.

Try

$servers = Get-Content -Path 'c:\users\jason\documents\skyperservers.txt'
$result = foreach ($server in $servers) {
    Get-CsWindowService -ComputerName $server | 
    # or use regex:  Where-Object {$_.Status -match 'Running|Stopped'}
    Where-Object {$_.Status -eq 'Running' -or $_.Status -eq 'Stopped'} |
    # include the server name in the output
    Select-Object @{Name = 'ComputerName'; Expression = {$server}}, Status
}

Now you can display the results on screen

$result

or for instance save the results to a Csv file

$result | Export-Csv -Path 'X:\SkypeServers_Status.csv' -NoTypeInformation