Home > Software engineering >  Powershell - Format Hastable as Table, output not as expected
Powershell - Format Hastable as Table, output not as expected

Time:12-31

I have a powershell hashtable which is generated from looking at services and their status,

the code to get generate the values is as follows:

$table = @{}

foreach ($service in $services)
{
    $obj = Get-Service -name $service -ErrorAction SilentlyContinue

    if ($obj -ne $null){
        $table[$service] = $obj.status
    }
}

'attempt 1 at outputting the table, shows the inline response
$table | out-string | write-host

'attempt 2 at outputting the table, gives no output
$table

'attempt 3 at outputting the table, shows no output
write-output $table 

I am trying to write the values to the screen as follows (just by calling the hastable or using write-output, the results are identical,

This works fine in a test example but not within my script:

PS C:\WINDOWS\system32> $table

Name                           Value
----                           -----
Service1                      Running
Service2                      Running

Within my script I get this:

Name                           Value
----                           -----
{Service1, Service2 ... {Stopped, Stopped, Stopped}

I have attempted to force the output using format-table, and various combinations of write-output / write-host

Can someone please give me a pointer?

CodePudding user response:

why you decided for a hashtable? don't see any reason in this code.

#define some names
$services=@('LicenseManager','NPSMSvc_1242e4','dummy')

#simple query - pure PS objects, no strange checks or so ever
$services|get-service -ErrorAction Ignore|select name,status|export-csv -nti servicesStatus.csv

BTW formatting functions are formatting to the given output - you were using screen formatting and trying to push it to the file. that is wrong. use export functions. you can of course try simple out-file but in general objects are complex creatures with some hierarchy. screen is only for your eyes - stop thinking in what you see but what is really is.

CodePudding user response:

Another possibility, assuming you don't need the Hash-Table for further processing, which gives you more control over the output format:

Clear-Host
$MyServices = 
  Get-Service -Name  @("HomeGroup*","USBDLM*","Macrium*",
                       "Remote*","MsKey*","VSS","Wsearch",
                       "DiagTrack")
$fmtServices = @{Expression={$_.Name};Label="Service Name";
                                      Width=25;Align="Left"},
               @{Expression={$_.Status};Label="Status";
                                        Width=10;Align="Left"}

$MyServices | Format-Table $fmtServices | Tee Test.txt

Output:

Service Name              Status    
------------              ------    
DiagTrack                 Running   
MacriumService            Running   
MsKeyboardFilter          Stopped   
RemoteAccess              Stopped   
RemoteRegistry            Stopped   
USBDLM                    Running   
VSS                       Stopped   
Wsearch                   Running   
  • Related