Steve Campbell
Steve Campbell

Reputation: 93

No output from Powershell command when run from script vs cli

This one command doesn't write output to the screen when running it in a script, but it works when executing it in the PowerShell ISE cli:

$toptenseverity = $csvData | select Severity, Title -Unique | sort Severity -Descending | select -First 11
$toptenseverity

Code:

Write-Host "`r`nTop 10 most severe vulnerabilities:"
$toptenseverity = $csvData | select Severity, Title -Unique | sort Severity -Descending | select -First 11
$toptenseverity
Write-Host "Trying again to write output of toptenseverity using write host toptenseverity:"
Write-Host $toptenseverity
Write-Host "Trying again to write output of toptenseverity using write output toptenseverity:"
Write-Output $toptenseverity

Output:

Generating P1 report. Please wait...

Total P1 count:    352
Severity 5 total:  11
Severity 4 total:  16
Severity 3 total:  325

Top 10 most severe vulnerabilities:

Trying again to write output of toptenseverity using write host toptenseverity:
@{Severity=5; YouDon'tNeedToKnowThis} @{Severity=4; Title=YouDon'tNeedToKnowThis} @{Severity=4; Title=YouDon'tNeedToKnowThis
} @{Severity=4; Title=YouDon'tNeedToKnowThis} @{Severity=4; Title=YouDon'tNeedToKnowThis} 
Trying again to write output of toptenseverity using write output toptenseverity:

When I run it from the cli in PS ISE I get this output:

Severity Title                                                                                  
-------- -----                                                                                  
5        YouDon'tNeedToKnowThis                                                  
4        YouDon'tNeedToKnowThis
4        YouDon'tNeedToKnowThis                                    
4        YouDon'tNeedToKnowThis                             
4        YouDon'tNeedToKnowThis                               

Upvotes: 2

Views: 1503

Answers (1)

G42
G42

Reputation: 10019

Use:

Write-Output $toptenseverity | Format-Table 

This forces the object to be formatted as a table, which is what's going on in the ISE (by default)

Upvotes: 3

Related Questions