Learn about Windows PowerShell
Microsoft Scripting Guy Ed Wilson here, it’s a good thing I enjoy getting up early on weekends. To me, because I get up early during the week, it makes sense to get up early on the weekend and get busy doing things that I want to do. One of the things I like to do early on weekend mornings is hang out on Facebook and Twitter for a little while as I wait for my English Breakfast tea to steep, and as I wait for the Irish steel cut oats to cook.
One of the things I wanted to look at this morning arose because of a presentation I made to the Philadelphia Area Exchange Users Group this week. I showed them a script I had written several years ago that creates a temporary text file, writes process information to the temporary text file, displays the information in Notepad, and then deletes the temporary text file when you close Notepad. It is a pretty cool script, and I posted it to Scripting Guys Script Repository that night after the user group meeting. I thought the script would be more useful if I modified it to accept piped input. I got a little carried away, and turned it into an advanced function. The Out-TempFile.ps1 script contains the Out-TempFile advanced function. I added help tags so the function interacts with the Get-Help function. For more information about adding help to a Windows PowerShell script, refer to the articles from Help week.
After creating the help tags, I use the CmdletBinding attribute. This causes the function to bind parameters in the same way that a Windows PowerShell cmdlet binds the parameters. I then create the input parameter. To enable piped input to the function, it is necessary to specify the valueFromPipeLine parameter. The other parameters make the –in parameter mandatory and set it in the first position. The [psobject] type constraint makes the input an object, and not merely a string. The parameter section and CmdletBinding attribute are shown here:
The first thing I want to do when the function is called is create the temporary file name. To do this, I use the GetTempFileName static method from the .NET Framework io.path class. I really do not need to know the temporary file name because I store it in the $tempFile variable. This variable will be used when creating and deleting the file. The Begin section of the advanced function defines actions that are performed once. This is shown here:
The Process section is run for each item that comes through the pipeline. The Out-File cmdlet is used to create the temporary text file and to append data to the file. I set the width to 300 characters and the encoding to Unicode. The Process block is shown here:
There are two things that remain to be accomplished. The first is to display the temporary file in Notepad; this is very convenient because the user’s temporary folder is cumbersome to navigate to. In addition, many programs have a nasty habit of leaving their temp files in the temp folder, and therefore spotting my specific temporary file gets to be difficult. It is much faster to have Notepad display it for me. After I am finished reading the file, I like to remove the temporary file. To do this, I pipe the Notepad command to the Out-Null cmdlet. This pauses execution of the script. When Notepad is closed, execution resumes and the Remove-Item cmdlet is used to delete the temporary file. The end section of the Out-TempFile function is shown here:
When the function is called, after you have run the script to load the function (in either the ISE or by dot-sourcing the script into the Windows PowerShell console), the function pauses execution while the temporary text file is displayed, as shown in the following image.
Join us tomorrow when we will begin looking at the Active Directory cmdlets that ship with Windows Server 2008 R2.
We would love for you to follow us on Twitter or Facebook. If you have any questions, send email to us at email@example.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
Ed Wilson and Craig Liebendorfer, Scripting Guys