0

I have this PowerShell script which is outputting to HTML. Below is a snippet of the code.

#Services Check
Write-Host "Services Check"
$html = $html + @"
    <h3>Services Checks</h3>
    <table>
        <tr>
            <th>Name</th>
            <th>Result</th>
        </tr>
"@
get-service -computername server01 -Name *service* |
    foreach-object{
    if($_.Status -ne "Running"){
        Write-Host $_.Name "is not running" -ForegroundColor "red"
        $html = $html + @"
        <tr>
            <td class="name">$_.Name</td>
            <td class="red">is not running</td>
        </tr>
"@
    }else{
        Write-Host $_.Name "is running" -ForegroundColor "green"
        $html = $html + @"
        <tr>
            <td class="name">$_.Name</td>
            <td class="green">is running</td>
        </tr>
"@
    }
}
$html = $html + "</table>"

My issue is on the PS console output it lists the name of teh services fine but in the HTML output it has a result as below (in table format).

Services Check System.ServiceProcess.ServiceController.Name is running

Is there anything I can do to pull in the name oppose to this undescriptive listing.

Thanks in advance

1 Answer 1

1

try to write in $():

$($_.Name)

In a string (double quotes) you need to force the properties variable expansion in this way.

Sign up to request clarification or add additional context in comments.

2 Comments

+1 If you simply reference the object, it uses the toString() method to provide the value, but when you need to reference a property of an object inside a string, you need to wrap it as an expression like C.B.'s answer shows
@Graimer Thanks for the better clarification!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.