Skip to main content

Saving data While it Travels Through the PowerShell Pipeline

A good question came up in class today while we were studying PowerShell Format commands.  We took a good look at how Format cmdlets consume the original object and make piping limited.  One of the questions that came up was if it is possible to save the information to a file and still be able to use it in the pipeline?  Fortunately for us, there is Tee-Object.
We have a couple of ways that we can implement Tee-Object.  Let’s look at it from a reusability perspective.  Here is our starting code:

Get-Process |
    Select-Object -first 3 -Property Name, VM, PM

And here is the output:
Name            VM        PM
----            --        --
AcroRd32 472272896 216985600
AcroRd32 132603904  10866688
ApMsgFwd  96550912   1626112

We will now add Tee-Object and send the data to another cmdlet.

Get-Process |
    Select-Object -first 3 -Property Name, VM, PM |
    Tee-Object -FilePath C:\PS\Data1.txt |
    Sort-Object -Property VM

The object was successfully piped to Sort-Object and processed.  We can also read the text file that was created.

PS C:\ps> Get-Content data1.txt

Name            VM        PM
----            --        --
AcroRd32 472272896 216985600
AcroRd32 132603904  10866688
ApMsgFwd  96550912   1626112

The reusability question now comes into play.  What if I needed to use the data that was sent to the text file in a later operation?  Well, it is now text.  That is not information that we could easily reincorporate into a PowerShell object.  Utilizing ConvertTo-CSV and ConvertFrom-CSV we can.  By placing ConvertTo-CSV before Tee-Object and ConvertFrom-CSV after Tee-Object was are able to send a comma separated file to disk.

Get-Process |
    Select-Object -first 3 -Property Name, VM, PM |
    ConvertTo-CSV |
    Tee-Object -FilePath C:\PS\Data1.csv |
    ConvertFrom-CSV|
    Sort-Object -Property VM

Name     VM        PM      
----     --        --      
AcroRd32 132603904 10866688
AcroRd32 472272896 216985600
ApMsgFwd 96550912  1626112

The screen out shows the Sort-Object successfully worked with the object in the PowerShell pipeline.  Import-CSV is also able to successfully read the object back into member from disk.

PS C:\ps> Import-Csv -Path Data1.csv

Name     VM        PM      
----     --        --      
AcroRd32 472272896 216985600
AcroRd32 132603904 10866688
ApMsgFwd 96550912  1626112  


Let’s take a look at using the –Variable parameter of Tee-Object.

Get-Process |
    Select-Object -first 3 -Property Name, VM, PM |
    Tee-Object -Variable Data |
    Sort-Object -Property VM

Here we removed the –FilePath parameter and added the –Variable parameter.  This is a formal usage of a variable so we omit the $ in front of the variable name. Here is the contents of the variable
.
PS C:\ps> $Data

Name            VM        PM
----            --        --
AcroRd32 472272896 216985600
AcroRd32 132603904  10866688
ApMsgFwd  96550912   1626112

More importantly, here is the object:

PS C:\ps> $Data | Get-Member


   TypeName: Selected.System.Diagnostics.Process

Name        MemberType   Definition                   
----        ----------   ----------                   
Equals      Method       bool Equals(System.Object obj)
GetHashCode Method       int GetHashCode()            
GetType     Method       type GetType()               
ToString    Method       string ToString()            
Name        NoteProperty string Name=AcroRd32         
PM          NoteProperty long PM=216985600            
VM          NoteProperty long VM=472272896  

The information stored in $data is retained as an object.


So the question now comes up as to when to use the –FilePath parameter and when to use the –Variable parameter. If you will consume the data that was removed from the pipeline within the same scope of memory, use the –Variable parameter.  If you will consume the data in another scope of memory, such as another cmdlet or a different script, the use the –FilePath parameter

Comments

Popular posts from this blog

How to list all the AD LDS instances on a server

AD LDS allows you to provide directory services to applications that are free of the confines of Active Directory.  To list all the AD LDS instances on a server, follow this procedure: Log into the server in question Open a command prompt. Type dsdbutil and press Enter Type List Instances and press Enter . You will receive a list of the instance name, both the LDAP and SSL port numbers, the location of the database, and its status.

How to run GPResult on a remote client with PowerShell

In the past, to run the GPResult command, you would need to either physically visit this client, have the user do it, or use and RDP connection.  In all cases, this will disrupt the user.  First, you need PowerShell remoting enabled on the target machine.  You can do this via Group Policy . Open PowerShell and type this command. Invoke-Command –ScriptBlock {GPResult /r} –ComputerName <ComputerName> Replace <ComputerName> with the name of the target.  Remember, the target needs to be online and accessible to you.

Error icon when creating a GPO Preference drive map

You may not have an error at all.  Take a look at the drive mapping below. The red triangle is what threw us off.  It is not an error.  It is simply a color representation of the Replace option of the Action field in the properties of the drive mappings. Create action This give you a green triangle. The Create action creates a new mapped drive for users. Replace Action The Replace action gives you a red triangle.  This action will delete and recreate mapped drives for users. The net result of the Replace action is to overwrite all existing settings associated with the mapped drive. If the drive mapping does not exist, then the Replace action creates a new drive mapping. Update Action The Update action will have a yellow triangle. Update will modify settings of an existing mapped drive for users. This action differs from Replace in that it only updates settings defined within the preference item. All other settings remain as configured on the mapped drive. If the