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

Sticky Key problem between Windows Server 2012 and LogMeIn

This week I instructed my first class using Windows Server 2012 accessed via LogMeIn and discovered a Sticky Key problem every time you press the Shift key. Here is my solution to resolve this.  First off, in the Preferences of LogMeIn for the connection to the Windows Server, click General . Change the Keyboard and mouse priority to Host side user and click Apply at the bottom. On the Windows 2012 server, open the Control Panel – Ease of Access – Change how your keyboard works . Uncheck Turn on Sticky Keys . Click Set up Sticky Keys . Uncheck Turn on Sticky Keys when SHIFT is pressed five times . Click OK twice. If you are using Windows Server 2012 as a Hyper-V host, you will need to redo the Easy of Use settings on each guest operating system in order to avoid the Sticky Key Problem. Updated Information: March 20, 2013 If you continue to have problems, Uncheck Turn on Filter Keys .

With the AD Recycle Bin Turned on, What Happens when you Create a User Account with a Password that does not meet the Password Policy?

This was an interesting observation from one of my Windows Server 2012 classes.  While working with the AD Recycle bin in a lab, one of my students discovered some interesting accounts that were created. When he created user accounts that did not meet password complexity requirements, an account is temporarily made and then deleted.  When a new password is provided that meets the password requirements, then a new account is made. We discovered this in two places.  First off in the Active Directory Administrative Center.  This is what caused the initial confusion.  Take a look.  This is in the Deleted Objects OU. You can see multiple deleted accounts for Test2 and one for Test3.  Test3 is a valid, functioning user account.  Using the PowerShell command Get-ADObject –IncludeDeletedObjects –Filter * –Properties ObjectSID we can see that indeed, two accounts were created, with one of them deleted. Notice the RID portion of the SID is different. ...

Where did a User’s Account Get Locked Out?

Updated: May 15, 2015 When this article was originally published, two extra carriage returns were add causing the code to malfunction.  The code below is correct.   My client for this week’s PowerShell class had a really interesting question. They needed to know where an account is being locked out at. OK, interesting. Apparently users hop around clients and forget to log off, leading to eventual lock out of their accounts. The accounts can be unlocked, but are then relocked after Active Directory replication. This problem is solved in two parts. The first one is to modify the event auditing on the network. The second part is resolved with PowerShell. The first part involves creating a group policy that will encompass your Domain Controllers. In this GPO, make these changes. Expand Computer Configuration \ Policies \ Windows Settings \ Security Settings \ Advanced Audit Policy Configuration \ Audit Policies \ Account Management Double click User Account Management C...