I'm trying to monitor a service "ENTSSO" which is on 2 servers. This service must be running on one of the 2 servers but cannot be running on both.
I wrote this powershell that works at the powershell command line however it refuses to run as a SAM template.
It looks like the get-service doesn't return anything. I added the message so I can see what is happening.
I'm using the solarwinds server and domain admin credentials for testing
The script takes a minimum of 2 arguments but I'm using 3.
Script arguments: ENTSSO,server1,server2
Script:
#Get service Name
$servicename = $args.get(0);
#get remaining arguments as server names
$list = @();
for($i=1;$i -lt $args.count; $i++)
{
$list = $list + $args.get($i);
}
#set result to false
$serviceup = $false;
$res = ""
try
{
$command = {Get-Service | where {$_.Name -eq $Using:servicename} }
$service = $service = Invoke-Command -ComputerName $list -Credential '${CREDENTIAL}' -ScriptBlock $command
$res = $res + $list + ": " + $service.Name + " " + $service.Status;
if ($service)
{
# service found on server
# do something
if ($service.Status -eq "Running")
{
#found service and it is running
$serviceup = $true;
}
}
if ($serviceup)
{
Write-Host 'Statistic.Service: 1';
Write-Host "Message.Service: $serviceup" $res;
}
else
{
Write-Host 'Statistic.Service: 0';
Write-Host "Message.Service: $servicename $serviceup" $list $list.count $res;
}
}
catch
{
Write-Host 'Statistic.Service: 0';
Write-Host "Message.Service: $_.Exception.Message";
}
Exit 0;
Output Results:
Message.Service: ENTSSO False serv029 serv030 2 serv029 serv030:
Statistic.Service: 0
So its should look like this:
Statistic.Service: 1
Message.Service: True serv029 serv030: ENTSSO ENTSSO Stopped Running
This is what you get if you run it on the SAM server using the same credentials but at the powershell prompt.
Any help in working out why this does not work will be gratefully received.