Average Rating:

5.00 by 2 users.

28 Downloads, 79 Views

1 Comment(s)

Set-TempFile

By: Alex McFarland 20 Apr 2011 07:27 PM UTC in the category: Advanced Event 10

Description:

Writes data to an automaticly generated temporary file, in either ASCII or Unicode format. Data may come from the pipeline or a command line parameter.

Grading Criteria:
-Advanced function, complete with comment-based Help.
-Accepts piped content.
-Returns the path to the temporary file.
-The temporary file has a temporary name, and resides in the temporary folder.
-Function is appropriately named.
-Has a switch that will display the content of the temporary file in Notepad.
-Supports an encoding parameter and support ASCII and Unicode as encoding types.
-Supports the whatif, debug, and verbose parameters.
<#
	.SYNOPSIS
		Writes data to a temporary file.
	
	.DESCRIPTION
		Writes data to an automaticly generated temporary file, in either ASCII 
		or Unicode format. Data may come from the pipeline or a command line parameter.
	
	.PARAMETER  TempData
		Data to be written to the temporary file.
	
	.PARAMETER  Encoding
		Specifies either "ASCII" or "Unicode" encoding.  Defaults to ASCII

	.PARAMETER  Notepad
		If supplied, will open the temporary file in notepad
	
	.EXAMPLE
		$TempFile = dir -filter *.doc | Set-TempFile
		or
		$MyDir = dir -filter *.doc
		$TempFile = Set-TempFile -TempData $MyDir
		
		Gets a list of all .doc files, writes them to a temporary file,
		and returns the sets $TempFile to the path of the temporary file.
	
	.EXAMPLE
		Get-Verb | Set-TempFile -Notepad
		
		Gets a list of all valid PowerShell verbs, writes it to a temporary
		file, and opens the file in notepad.

	.NOTES
		2011 Scripting Games, Advanced Event 10.
		
		-Advanced function, complete with comment-based Help.
		-Accepts piped content.
		-Returns the path to the temporary file.
	        -The temporary file has a temporary name, and resides in the temporary folder.
		-Function is appropriately named.
		-Has a switch that will display the content of the temporary file in Notepad.
		-Supports an encoding parameter and support ASCII and Unicode as encoding types.
		-Supports the whatif, debug, and verbose parameters.
#>
function Set-TempFile 
{
	[CmdletBinding(SupportsShouldProcess=$true)]
	param(
		[Parameter(
			ValueFromPipeline=$true,
			ValueFromPipelineByPropertyName=$true,
			Position = 0,
			Mandatory = $false,
			HelpMessage = "Data to set in the temporary file")]
		[String[]]$TempData,
		
		[Parameter(
			Mandatory = $false,
			HelpMessage = "Character Encoding used in the temporary file")]
		[ValidateSet("ASCII","Unicode")]
		[String]$Encoding = "ASCII",
		
		[Parameter(
			Mandatory = $false,
			HelpMessage="Option to open the temporary file in notepad")]
		[Switch]$Notepad
	)

	Begin 
	{
		# set temporary file
		Write-Verbose "getting temporary filename"
		Write-Debug "Calling [System.IO.Path] to get a tempoary filename"
		$TempFile = [System.IO.Path]::GetTempFileName()
		
	}	# end begin

	Process
	{
		if ($PSCmdlet.ShouldProcess($TempFile))
		{
			# foreach because an array of strings may have been specified
			# on the command line.
			foreach ($Data in $TempData) {
				try	
				{
					# write data to the temporary file
					Write-Verbose "Writing data to $TempFile using $Encoding encoding."
					Write-Debug "Writing the following $Encoding encoded data to $TempFile `n $Data"
					$Data | Out-File -Append -Encoding $Encoding -ErrorAction Stop -FilePath $TempFile
				} 	# end try
				catch 
				{
					# return error
					throw "Error writing $Encoding encoded data to $Tempfile"
				} 	# end catch
			} 	# end foreach
		} 	#end ShouldProcess
	}	# end process

	End
	{
		# return filepath
		Write-Verbose "returning path to temporary file"
		Write-Debug "returning path to temporary file"
		Write-Output $TempFile
		
		# if requested, spawn notepad
		if ($Notepad) 
		{
			Write-Verbose "opening $TempFile in notepad"
			Write-Debug "opening $TempFile in notepad"
			notepad.exe $TempFile
		}	# end if
	}	# end end
}	# end function Set-TempFile
Top

Comments:

4/22/2011 6:32 PM
good script - I like the try/catch and write-verbose/debug