Home > Blockchain >  Powershell srite ouptut formatting
Powershell srite ouptut formatting

Time:10-11

I am working on Windows Health check script where if any step breached threshold it will be written as failed/Warning in the output log something like below.

[ OScheck ]    : [ INFO ] : Windows 10 Enterprise
[ UpTime ]    : [ GOOD ] : Server is up for 1 Days
[ Hardware ]   : [ INFO ] : Its a Physical server.
[ CPU ]    : [ GOOD ] : Server CPU utilization 2.74%
[ MEMORY ]    : [ WARN ] : Server MEMORY utilization 82.75%

Once threshold MEMORY 82.75% is breached I want to print last 5 high memory utilization process as below format.

[ MEMORY ]    : [ WARN ] : Server MEMORY utilization 82.75%
[ MEMORY ]    : [ INFO ] : Top 5 high memory utilization process%
                                 Count Name               Memoryusage(Total)
                                 ----- ----               ------------------
                                     1 Memory Compression 398.582MB
                                    20 chrome             1,615.891MB
                                     9 Teams              990.699MB
                                     1 OUTLOOK            241.316MB
                                     1 MsMpEng            194.027MB

But currently I am getting output printed as below.

[ MEMORY ]    : [ WARN ] : Server MEMORY utilization 82.75%
[ MEMORY ]    : [ INFO ] : Top 5 high memory utilization process
Count Name               Memoryusage(Total)
----- ----               ------------------
    1 Memory Compression 398.582MB
   20 chrome             1,615.891MB
    9 Teams              990.699MB
    1 OUTLOOK            241.316MB
    1 MsMpEng            194.027MB

Is there any way to do such formatting? Below is the code whic i am running.

Function Get-Memory{
$CompObject =  Get-CIMInstance -Class WIN32_OperatingSystem
$Memory = ((($CompObject.TotalVisibleMemorySize - $CompObject.FreePhysicalMemory)*100)/ $CompObject.TotalVisibleMemorySize)
$Memory | % {$_.ToString("#.##")}
}

$highMemUsage = Get-Process | Sort-Object -Property ws -Descending | Group-Object -Property ProcessName | Select-Object -first 5 Count, Name, @{Name='Memoryusage(Total)';Expression={'{0:N3}MB' -f (($_.Group | Measure-Object WorkingSet -Sum).Sum / 1MB)}} | out-string
$ram = Get-Memory
if ($ram -ge 90)
{
    "[ MEMORY ]    : [ FAIL ] : Server MEMORY utilization $($ram)%"
    "[ MEMORY ]    : [ INFO  ] : Top 5 high memory utilization process $($highMemUsage)"
} elseif($ram -gt 80 -And $ram -le 90) {
    "[ MEMORY ]    : [ WARN ] : Server MEMORY utilization $($ram)%"
    "[ MEMORY ]    : [ INFO  ] : Top 5 high memory utilization process $($highMemUsage)"
} elseif($ram -le 80){
    "[ MEMORY ]    : [ GOOD ] : Server MEMORY utilization $($ram)%"
}

CodePudding user response:

  • Use Out-String with the -Stream switch in order to return the lines that make up the formatted representation one by one (without -Stream, a single, multi-line string is returned).

  • Filter out empty / all-whitespace lines with -match '\S' (\S is a regex that matches any non-whitespace character).

  • Prepend the necessary number of spaces to each resulting line to get the desired alignment (' ' * 33 creates a string composed of 33 spaces), using string concatenation ( ).

Get-Process | 
  Sort-Object -Property ws -Descending | 
  Group-Object -Property ProcessName | 
  Select-Object -first 5 Count, Name, @{Name='Memoryusage(Total)';Expression={'{0:N3}MB' -f (($_.Group | Measure-Object WorkingSet -Sum).Sum / 1MB)}} |
  Out-String -Stream  | 
  ForEach-Object { if ($_ -match '\S') { ' ' * 33   $_ } }

In the context of your code, use the following, which prepends a newline (... represents the pipeline above):

$highMemUsage = [Environment]::NewLine   (...)
  • Related