Average Rating:

5.00 by 3 users.

56 Downloads, 306 Views

2 Comment(s)

New-TempFileContent

By: Rich Kusak 22 Apr 2011 04:58 AM UTC in the category: Advanced Event 10

Description:

The New-TempFileContent function creates a temporary file in the Windows temp location and saves content in the file.
Optionally, the file contents can be shown by use if the -Show parameter.
function New-TempFileContent {
<#
	.SYNOPSIS
		Creates a new temporary file to store content.

	.DESCRIPTION
		The New-TempFileContent function creates a temporary file in the Windows temp location and saves content in the file.
		Optionally, the file contents can be shown by use if the -Show parameter.

	.PARAMETER Content
		Specifies the content to be saved to the temporary file.

	.PARAMETER Encoding
		Specifies the type of character encoding used in the file. Valid values are "Unicode" and "ASCII". "Unicode" is the default.

	.PARAMETER Show
		Shows the file content by opening it with the specified file reader.

	.PARAMETER TempFile
		The location of the temp file. The default is automatically generated in the Windows temp location.

	.PARAMETER FileReader
		The program used to read the temp file and display its contents. The default is Notepad.

	.EXAMPLE
		New-TempFileContent 'Luke, I am your father.'
		Saves the content 'Luke, I am your father.' to a temp file.

	.EXAMPLE
		$mystuff | New-TempFileContent
		The variable $mystuff contains a large string of data. Piping $mystuff to New-TempFileContent saves the content to a temp file.
		
	.EXAMPLE
		New-TempFileContent 'Resistance is futile.' -Show
		Saves the content 'Resistance is futile.' to a temp file and opens the file showing its content.

	.INPUTS
		System.String

	.OUTPUTS
		PSObject

	.NOTES
		Name:
		Author: Rich Kusak
		Created: 2011-04-19
		LastEdit: 2011-04-21 23:56

	.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(Position=0, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true,
			HelpMessage='Specifies the content to be saved to the temporary file.'
		)]
		[string]$Content,
		
		[Parameter(ValueFromPipelineByPropertyName=$true,
			HelpMessage='Specifies the type of character encoding used in the file.'
		)]
		[ValidateSet('ASCII, Unicode')]
		[string]$Encoding = 'Unicode',
		
		[Parameter(ValueFromPipelineByPropertyName=$true,
			HelpMessage='Shows the file content by opening it with the specified file reader.'
		)]
		[switch]$Show,
		
		[Parameter(ValueFromPipelineByPropertyName=$true,
			HelpMessage='The location of the temp file.'
		)]
		[string]$TempFile = [IO.Path]::GetTempFileName(),
		
		[Parameter(ValueFromPipelineByPropertyName=$true,
			HelpMessage='The program used to read the temp file and display its contents.'
		)]
		[string]$FileReader = "$env:SystemRoot\System32\Notepad.exe"
	)

	process {

		Write-Debug "Performing operation 'Test-Path' on target '$FileReader'."
		# Tests if the file reader program exists in the path specified.
		if (Test-Path $FileReader) {
			Write-Verbose "Successfully found file reader '$FileReader'."
		} else {
			return Write-Error "Failed to find file reader '$FileReader'."
		}
		
		# Implements the -Whatif and -Confirm parameters
		if ($psCmdlet.ShouldProcess($TempFile, 'New-TempFileContent')) {
			try {
				Write-Debug "Performing operation 'Out-File' on target '$TempFile'."
				# Saves input content to a temp file
				$Content | Out-File $TempFile -Encoding $Encoding -Force -ErrorAction Stop
				Write-Verbose "Successfully wrote content to file '$TempFile'."
			} catch {
				return Write-Error $_
			}
			
			if ($Show) {
				Write-Debug "Performing operation call '$FileReader' on target '$TempFile'."
				# Opens the file and shows its contents
				& $FileReader $TempFile
			}

			# Outputs the temp file path as an object
			New-Object PSObject -Property @{
				'TempFilePath' = $TempFile
			}
		} # end if ShouldProcess
	} # end process
} # end function New-TempFileContent
Top

Comments:

4/22/2011 5:59 AM
nice function. I like the ability to choose the reader ... nice touch. I also like that you return an object.
4/22/2011 6:17 AM
Thanks Ed, really appreicate you taking the time to give feedback!