user2299515
user2299515

Reputation:

PowerShell HTML Output not displaying correctly

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

Upvotes: 0

Views: 731

Answers (1)

CB.
CB.

Reputation: 60918

try to write in $():

$($_.Name)

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

Upvotes: 1

Related Questions