Hash Table

This topic contains 5 replies, has 2 voices, and was last updated by Profile photo of H Man H Man 2 years, 4 months ago.

  • Author
    Posts
  • #17423
    Profile photo of H Man
    H Man
    Participant

    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

  • #17424
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    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.

  • #17426
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    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
  • #17427
    Profile photo of H Man
    H Man
    Participant

    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                                                                           
    
    
  • #17428
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    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
    
  • #17429
    Profile photo of H Man
    H Man
    Participant

    actually your example is perfect thank you

You must be logged in to reply to this topic.