Need help iterating results in my function

Welcome Forums General PowerShell Q&A Need help iterating results in my function

This topic contains 6 replies, has 3 voices, and was last updated by

 
Participant
3 months, 2 weeks ago.

  • Author
    Posts
  • #110507

    Participant
    Points: 22
    Rank: Member

    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?

     

     

  • #110510

    Participant
    Points: 22
    Rank: Member

    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
    
    
    • #110513

      Participant
      Points: 93
      Rank: Member

      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

    • #110522

      Participant
      Points: 22
      Rank: Member

      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.

  • #110515

    Participant
    Points: 878
    Helping Hand
    Rank: Major Contributor

    $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
    
  • #110518

    Participant
    Points: 878
    Helping Hand
    Rank: Major Contributor

    $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
    
  • #110525

    Participant
    Points: 878
    Helping Hand
    Rank: Major Contributor

    This one liner will do everything.

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

The topic ‘Need help iterating results in my function’ is closed to new replies.