I am developing a simple solution which needs to send an email alert if any service is stopped on my remote server.
Below is the flow:
Everything is working fine as per expectation but I am getting System.ServiceProcess.ServiceController
instead of the actual service name. I am printing things on the PowerShell console and there I am getting the service name.
Code:
$Services = Get-Content D:\Services.txt
foreach ($Service in $Services) {
Write-Host "Service Name: "$Service
}
When I execute this, I am getting service name as expected.
if ($Service.Status -eq "Stopped") {
Write-Host "Service is stopped"
$dataRow = "<tr><td width='10%'>199.199.50.512</td><td width='10%' >$Service</td><td width='10%' align='center'>Stopped</td></tr>"
Add-Content $ServiceReport $dataRow;
}
Here where $Service
is written, I am expecing my service name but I am getting System.ServiceProcess.ServiceController
in HTML report.
HTML Report:
You need to be specific about which property of the object you want to return. You likely want DisplayName, so do this:
$dataRow = "<tr><td width='10%'>199.199.50.512</td><td width='10%' >$($Service.displayname)</td><td width='10%' align='center'>Stopped</td></tr>"
Note that this is surrounded in a subexpression operator $()
in order to access the property of the object correctly from within a double quoted string.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments