Author Posts

July 23, 2014 at 11:13 am

Heres my hash Table

          

$h = [ordered]@{
Computername=$env:computername
ServiceState = "wscsvc", "CcmExec" | foreach {Get-WmiObject -Class Win32_Service -Filter "Name = '$($_)'" | select Name, State }
ServiceStartUp = "wscsvc", "CcmExec" | foreach {Get-WmiObject -Class Win32_Service -Filter "Name = '$($_)'" | select Name, StartMode}

}

[pscustomobject]$h

$h | select -expandproperty values

# I  get this

Name                           Value                                                                        
----                           -----                                                                        
Computername                   BR-ADMIN3                                                                    
ServiceState                   {@{Name=wscsvc; State=Running}, @{Name=CcmExec; State=Running}}              
ServiceStartUp                 {@{Name=wscsvc; StartMode=Auto}, @{Name=CcmExec; StartMode=Auto}}  

how do i remove the {@{

thank you

July 23, 2014 at 11:21 am

That's how Powershell displays arrays and PSObjects as text. (Arrays go into a pair of curly braces with comma-separated values, and PSObjects are displayed like hashtable literals.) You can certainly change the text that's displayed on screen if you like, but you'd have to decide what you want it to look like.

July 23, 2014 at 11:24 am

I'm not sure what you're doing with that hashtable. Here's a different approach which builds an array of objects, which are displayed in a more user-friendly way by Format-Table:

$services = "wscsvc", "CcmExec" | foreach {
    Get-WmiObject -Class Win32_Service -Filter "Name = '$($_)'" |
    select  @{Name = 'ComputerName'; Expression = { $env:computername }}, Name, State, StartMode
}

$services | Format-Table -AutoSize

July 23, 2014 at 11:26 am

Hi Dave id like it to look like this if possible i would also like to export-csv to results

I am remoting into 70 machines and building a report basec on 2 criteria state and startup mode

Name                                                                               State                                                                             
----                                                                               -----                                                                             
wscsvc                                                                             Running                                                                           
CcmExec                                                                            Running                                                                           

July 23, 2014 at 11:29 am

That's easy enough, but you don't have ComputerName or StartMode in that output, both of which were part of your original code. Should they be there as well? (If so, use the code from my previous post.)

$services = "wscsvc", "CcmExec" | foreach {
    Get-WmiObject -Class Win32_Service -Filter "Name = '$($_)'" |
    select  Name, State
}

$services | Format-Table -AutoSize
$services | Export-Csv -Path C:\Some\Csv\File.csv

July 23, 2014 at 11:31 am

actually your example is perfect thank you