Author Posts

August 30, 2018 at 2:32 pm

Need some help on formatting and displaying more than one result in the proper way.

I am building a function called Get-PageFileInfo . So far I have this and it works as expected for one computer.


Function Get-PageFileInfo
{



[CmdletBinding()]
Param(
[string[]]$ComputerName
)
Get-CimInstance -Class Win32_PageFileUsage -ComputerName $ComputerName |
Select-Object AllocatedBaseSize, Description, Peakusage, TempPageFile

}

That code produces this output:


PS C:\Scripts> Get-PageFileInfo Srv01

 

AllocatedBaseSize Description     Peakusage TempPageFile

----------------- -----------     --------- ------------

8192 C:\pagefile.sys       203        False

and if i add a second computer, I get this:


PS C:\Scripts> Get-PageFileInfo srvwin0767, crd-dc-wp01

 

AllocatedBaseSize Description     Peakusage TempPageFile

----------------- -----------     --------- ------------

8192 C:\pagefile.sys       203        False

26624 I:\pagefile.sys     11396        False

Ok, that's awesome.... but I want to add a field called computername and i would like to rename the parameter names. So I thought I would build a PSCUSTOMOBJECT like so:

 

Except that gives me this output:


PS C:\Scripts> Get-PageFileInfo srvwin0767, crd-dc-wp01 | ft

 

ComputerName              PageFileTotalSize PageFilePath                       PageFileCurrentUsage PageFilePeakUsage TempPageFileInUse

------------              ----------------- ------------                       -------------------- ----------------- -----------------

{srvwin0767, crd-dc-wp01} {8192, 26624}     {C:\pagefile.sys, I:\pagefile.sys} {$null, $null}       {203, 11396}      {False, False}

How do I get each member of the aray listed on it's own line?

 

 

August 30, 2018 at 2:40 pm

I have no idea what happened to the formatting.... but if you pop out the code in examples in new windows; it makes sense.

Also, I didn't paste my PSCUSTOMOBJECT


$PageFileStats = Get-CimInstance -Class Win32_PageFileUsage -ComputerName $ComputerName |
Select-Object AllocatedBaseSize, Description, Peakusage, TempPageFile
$Results = [PSCustomObject]@{
ComputerName = $ComputerName
PageFileTotalSize = $PageFileStats.AllocatedBaseSize
PageFilePath = $PageFileStats.description
PageFileCurrentUsage = $PageFileStats.CurrentUsage
PageFilePeakUsage = $PageFileStats.Peakusage
TempPageFileInUse = $PageFileStats.TempPageFile
} #END PSCUSTOMOBJECT

$Results

August 30, 2018 at 3:25 pm

Hi Mike,

I think what you are looking to do is iterate your $ComputerName parameter as your function does not accept pipeline input. Also, for your custom properties – you want to add custom properties using expressions with your Select-Object cmdlet – I added one for the Computer. You can rename your properties to get your desired output. See: https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/select-object?view=powershell-6

Function Get-PageFileInfo
{

[CmdletBinding()]
Param(
[string[]]$ComputerName
)

    foreach($computer in $ComputerName)
    {
        Get-CimInstance -Class Win32_PageFileUsage -ComputerName $Computer |
        Select-Object @{Name="ComputerName";Expression={$computer}},AllocatedBaseSize, Description, Peakusage, TempPageFile
    }
    

}

HTH,
Steve

August 30, 2018 at 3:27 pm

$PageFileStats is a collection here, hence
$PageFileStats.AllocatedBaseSize and all other properties will be array.
Here CustomObject has to be build using ForEach-Object cmdlet.

Get-CimInstance -Class Win32_PageFileUsage -ComputerName $ComputerName | ForEach-Object -Process {
   [PSCustomObject]@{
    ComputerName = $_.PSComputerName
    PageFileTotalSize = $_.AllocatedBaseSize
    PageFilePath = $_.description
    PageFileCurrentUsage = $_.CurrentUsage
    PageFilePeakUsage = $_.Peakusage
    TempPageFileInUse = $_.TempPageFile
  } #END PSCUSTOMOBJECT
} #End of ForEach

August 30, 2018 at 3:32 pm

$PageFileStats is a collection when there are multiple computers, so for cherry-picking , you will have to use ForEach-Object

Get-CimInstance -Class Win32_PageFileUsage -ComputerName $ComputerName | ForEach-Object -Process {
    [PSCustomObject]@{
    ComputerName = $_.PsComputerName
    PageFileTotalSize = $_.AllocatedBaseSize
    PageFilePath = $_.description
    PageFileCurrentUsage = $_.CurrentUsage
    PageFilePeakUsage = $_.Peakusage
    TempPageFileInUse = $_.TempPageFile
  } #END PSCUSTOMOBJECT
} #end of ForEach

August 30, 2018 at 3:44 pm

Steve,

Thank you! I had the idea of a foreach but was thinking of it all wrong. Sometimes, it's easier for someone else to see the solution then for you to see the solution yourself.

I am not sure why I complicated things so much in my head the ForEach of the computername is simple and it works!. Also, appreciate the idea on the expressions, another great idea.

August 30, 2018 at 3:59 pm

This one liner will do everything.

Get-CimInstance -Class Win32_PageFileUsage -ComputerName $ComputerName|Select-Object @{Name="ComputerName";Expression={$_.PSComputerName}},AllocatedBaseSize, Description, Peakusage, TempPageFile