Average Rating:

5.00 by 2 users.

54 Downloads, 199 Views

1 Comment(s)

Out-Temp

By: Miklos Karasz 21 Apr 2011 09:59 PM UTC in the category: Advanced Event 10

Tags:

Description:

The Out-Temp function sends output to a temporary file. Displays the content in notepad if -Notepad specified. Character encoding can be altered from the default ASCII. The temporary file will not be deleted if -Keepfile is specified.
You can pipe string or objects also.
#Requires -Version 2.0

<#
	Script:  	Out-Temp.ps1
	Author:  	Miklos Karasz
	Date:		2011-04-21
	Keywords:	PowerShell, 2011 Scripting Games, Advanced10
#>

function Out-Temp {

	[CmdletBinding(SupportsShouldProcess=$true, ConfirmImpact='Medium')]
	param (
		
		# Specifies the objects to be written to the file.
		# The value can be an object, such as a quoted string, a variable that contains objects, or a command or expression that gets objects.
		# You can also pipe objects to the Out-Temp function.
		[Parameter(Mandatory=$true, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
		[System.Management.Automation.PSObject]
		$InputObject,
		
		# Specifies the type of character encoding used in the file.
		# Valid values are "Unicode", "UTF7", "UTF8", "UTF32", "ASCII", "BigEndianUnicode", "Default", and "OEM". "ASCII" is the default.
		# "Default" uses the encoding of the system's current ANSI code page.
		# "OEM" uses the current original equipment manufacturer code page identifier for the operating system.
		[ValidateSet('Unicode','UTF7','UTF8','ASCII','UTF32','BigEndianUnicode','Default','OEM')]
		[System.String]
		$Encoding = 'ASCII',
		
		# Displays the content in notepad.exe.
		[Alias('Notepad')]
		[switch]$DisplayInNotepad,
		
		# Temporary file will not be deleted if Keepfile is specified.
		[Alias('NoDelete')]
		[switch]$KeepFile
	)

	begin
	{	
		# The following is required since functions does not always pass thru Verbose and Debug preferences to the called cmdlet scope.
		$VerboseDebug = @{Verbose=$VerbosePreference.value__; Debug=$DebugPreference.value__}
		$tmp_fso = New-Item -ItemType:File -Path:$env:TEMP -Name:"$([System.IO.Path]::GetRandomFileName())" -Force @VerboseDebug
		Write-Verbose "Performing operation `"Output to File`" on Target `"$tmp_fso`"." @VerboseDebug
	}
	process
	{
		if ( $tmp_fso )
		{
			$InputObject | Out-File -FilePath:$tmp_fso.FullName -Encoding:$Encoding -Append
			# @VerboseDebug is not specified because $InputObject is wrapped per line when object is coming from pipe,
			# and verbose and debug output per line could be confusing.
		}
	}
	end
	{
		if ( $tmp_fso )
		{
			# return the name of the temp file:
			$tmp_fso.FullName
			if ( $DisplayInNotepad )
			{
				Start-Process -FilePath:notepad.exe -ArgumentList:$tmp_fso.FullName -Wait @VerboseDebug
			}
			if ( -not $KeepFile )
			{
				Remove-Item -Force -Path:$tmp_fso.FullName @VerboseDebug
			}
		}
	}
	
	
	<#
	
	.SYNOPSIS
	Sends output to a temporary file.

	.DESCRIPTION
	The Out-Temp function sends output to a temporary file.
	You can use this function instead of the redirection operator (>) when you need to use its parameters.
		
	.INPUTS
	[System.Management.Automation.PSObject]
	You can pipe any object to Out-Temp.

	.OUTPUTS
	[System.String]
	The full path of the temporary file.

	.EXAMPLE
	Get-Content $env:WINDIR\WindowsUpdate.log | Select -Last:10 | Out-Temp -Notepad -Verbose
	VERBOSE: Performing operation "Create File" on Target "Destination: C:\Users\User01\AppData\Local\Temp\rhgp4vqf.ivz".
	VERBOSE: Performing operation "Output to File" on Target "C:\Users\User01\AppData\Local\Temp\rhgp4vqf.ivz".
	C:\Users\User01\AppData\Local\Temp\rhgp4vqf.ivz
	VERBOSE: Performing operation "Remove File" on Target "C:\Users\User01\AppData\Local\Temp\rhgp4vqf.ivz".
	
	Displays the last 10 lines in notepad from WindowsUpdate.log and deletes the temporary file.
	
	.EXAMPLE	
	$tmpWUlogTail10 = Get-Content $env:WINDIR\WindowsUpdate.log | Select -Last:10 | Out-Temp -Notepad -Keep
	Move-Item $tmpWUlogTail10 "\\LogServer\WuLogs\Last.log"
	Displays the last 10 lines from WindowsUpdate.log and stores it on a logcollector server.

	.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
		
	.LINK
	New-Item
	Out-File
	Start-Process
	Remove_item
		
	.NOTES
	Author:  	Miklos Karasz
	Date:		2011-04-21
	Keywords:	PowerShell, 2011 Scripting Games, Advanced10

	#>
}

# Displays the help of the function in notepad. Returns the fullpath of the temp file, and removes it.
help Out-Temp -Full | Out-Temp -DisplayInNotepad -Verbose
Top

Comments:

4/21/2011 10:09 PM
very nice script. I like your use of shouldprocess to do the whatif parameter.