Average Rating:

4.50 by 3 users.

25 Downloads, 68 Views

2 Comment(s)

New-TempFile

By: Peter Rossi 20 Apr 2011 07:50 AM UTC in the category: Advanced Event 10

Description:

Advanced function to create temp files from data piplined or inputted into it.
Option to open the file in notepad
Option to specify the encoding type
Function New-TempFile()
{
[CmdletBinding(SupportsShouldProcess = $true)]
Param(
	[Parameter(Mandatory=$False, Position=0, ValueFromPipeline=$True)]
	[string[]]$Content,
	[Parameter(Mandatory=$False, Position=1, ValueFromPipeline=$False)]
 	[switch]$OpenFile=$False,
	[Parameter(Mandatory=$False, Position=1, ValueFromPipeline=$False)]
	[ValidateSet("ASCII","BIGENDIANUNICODE","BYTE","STRING","UNICODE","UNKNOWN","UTF7","UTF8")]
 	[String]$Encoding="ASCII"
)

Begin
{
	
	$TempFileName = "$($ENV:TEMP)\$($Env:USERNAME)_$((Get-Date).Ticks).txt"
	Write-Verbose "Temp file name defined $TempFileName"
}
Process
{
	ForEach($Line in $Content)
	{
		If(-not (Test-Path $TempFileName))
		{
			if($PSCmdLet.ShouldProcess("Creating Temp File With First Line"))
			{
				Write-Verbose "Creating $TempFileName and adding content $Line"
				try
				{
					$Line | Set-Content $TempFileName -Encoding $Encoding 
				}
				Catch
				{
					Write-Debug "Problem creating $TempFileName"
				}
			}
		}
		Else
		{
			if($PSCmdLet.ShouldProcess("Adding Content To Temp File"))
			{
				Write-Verbose "Adding $Line to $TempFileName"
				try
				{
					$Line | Add-Content $TempFileName -Encoding $Encoding 
				}
				Catch
				{
					Write-Debug "Problem creating $TempFileName"
				}
			}
		}
	}
	
	
}
End
{
If($OpenFile)
	{
		if($PSCmdLet.ShouldProcess("Opening Notepad With Temp File"))
		{
			Write-Verbose "Open $TempFileName with notepad.exe"
			Invoke-Expression "notepad.exe $TempFileName"
		}
	}
return $TempFileName
}


<#
.Synopsis
SG2011 Advanced Event 10 - Use Windows PowerShell to create an advanced function to create temporary files.
.Description
Creates a temp file which can have data pipelined into. Various options for encoding and opening the file.
.Parameter Content
The data you wish to write to the temp file
.Parameter OpenFile
A switch to specify if you want to open the temp file in notepad after you have created it.
.Parameter Encoding
A input for the type of file encoding you wish to choose. The options are:
ASCII, BIGENDIANUNICODE, BYTE, STRING, UNICODE, UNKNOWN, UTF7, UTF8

.Example
"MyData" | New-TempFile

This will create a new temp file with the text "MyData" in it and return the new file path to the pipeline
.Example
"MyData" | New-TempFile -OpenFile

This will create a new temp file with the text "MyData" in it and return the new file path to the pipeline and open the file in notepad once written to.
.Example
"MyData" | New-TempFile -Encoding UTF8

This will create a new temp file with UTF8 encoding with the text "MyData" in it and return the new file path to the pipeline

.Notes
Name: New-UserLogFile
Author: Peter Rossi
Last Edited: 20th April 2011
#>

}
Top

Comments:

4/20/2011 7:51 AM
The position for Encoding parameter is wrong :-(

[Parameter(Mandatory=$False, Position=1, ValueFromPipeline=$False)] [ValidateSet("ASCII","BIGENDIANUNICODE","BYTE","STRING","UNICODE","UNKNOWN","UTF7","UTF8")] [String]$Encoding="ASCII"

Should have been

[Parameter(Mandatory=$False, Position=2, ValueFromPipeline=$False)] [ValidateSet("ASCII","BIGENDIANUNICODE","BYTE","STRING","UNICODE","UNKNOWN","UTF7","UTF8")] [String]$Encoding="ASCII"

Hope I don't get marked down too badly for that.
4/24/2011 8:16 PM
Good solid script...Creative to use ticks in temp file names...