Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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

share|improve this question

1 Answer 1

up vote 1 down vote accepted

try to write in $():

$($_.Name)

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

share|improve this answer
1  
+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 –  Frode F. May 21 '13 at 8:53
    
@Graimer Thanks for the better clarification! –  CB. May 21 '13 at 8:55
    
Agreed excellent :) –  user2299515 May 21 '13 at 9:08

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.