Average Rating:

5.00 by 2 users.

30 Downloads, 65 Views

2 Comment(s)

New-TempTextFile.psm1

By: Kerry Boomsliter 21 Apr 2011 06:04 AM UTC in the category: Advanced Event 10

Description:

Creates a temp file on the pipeline and returns the temp file path name.
Challenge from the 2011 Scripting Games, the Advanced Event number 10.
Will create an empty file if no input is provided. I believe I've hit all of the extra point requirements!
Kerry Boomsliter
function New-TempTextFile {
<#
 .SYNOPSIS
  Accepts piped content to create a temporary text file.
  Returns path to the temp file that holds the content.
 .DESCRIPTION
  Creates a temp file on the pipeline and returns the temp file path name.
  Challenge from the 2011 Scripting Games, the Advanced Event number 10.
  Will create an empty file if no input is provided.
 .PARAMETER Content
  The content to be placed into our temp file, also taken
  from the pipeline.
 .PARAMETER Notepad
  Switch to enable running Notepad to display the new temp file.
 .PARAMETER Encode
  Select "ASCII" or "Unicode" temp file encoding.  
  Unicode is default.
 .EXAMPLE
  "Hello everyone" | New-TempTextFile
 .INPUTS
  [string] Text to be saved in a temporary file
 .OUTPUTS
  [string] Temporary file path name
 .NOTES
  Most full featured advanced function I've written in PS, even though it 
  does very little!
 .LINK
  http://blogs.technet.com/b/heyscriptingguy/archive/2011/04/15/the-2011-scripting-games-advanced-event-10-use-powershell-to-create-a-function-to-create-temp-files.aspx
  
#>

    [CmdletBinding(
        SupportsShouldProcess=$true
    )]
    
    param(
        [Parameter(Mandatory=$true,
            ValueFromPipeline=$true,
            HelpMessage="Enter a text string or pipe text to this function"
        )]
        [AllowNull()]
        [String]
        $content,
        [Parameter(
            HelpMessage="Enables displaying content in Notepad."
         )]
        [Switch]
        $notepad,
        [Parameter(Mandatory=$true,
            HelpMessage="Select ASCII or Unicode (default) encoding for the file."
        )]
        [ValidateSet("ASCII","Unicode")]
        [String]
        $encode="Unicode"
    )
    
    Process {
        $filename = $ENV:TEMP + "\$(Get-Date -format 'yyyy_MM_dd_hh_mm_ss').txt"
        # Handle -confirm, -whatif and -verbose
        if ($pscmdlet.ShouldProcess($filename, "Create temp file with pipeline input"))
        {
            $content | Out-File -FilePath $filename -Encoding $encode
            if ($notepad.isPresent) {
                notepad $filename
            }
            
            return $(Get-ChildItem $filename).FullName
        }
    }

}
Top

Comments:

4/21/2011 10:35 PM
Kerry -
Excellent script. I love your comment based help, and your use of parameters. Fine job. I generally like to use join-path to create paths to files instead of concatenation. I find it easier to read, and more robust. In addition i like to use -f and parameter substitution to put pieces together (like date, and file extension etc ... but this is a strong script. I love your use of ispresent ...
4/25/2011 7:39 PM
Thanks for the review and very helpful comments, Ed. I had not known about join-path and hardly used -f previously, but I can certainly see how nicely they fit. I actually hadn't known about isPresent until working on this script and found that property.