Average Rating:

4.00 by 2 users.

45 Downloads, 199 Views


By: Justin Stokes 21 Apr 2011 08:54 PM UTC in the category: Advanced Event 10


This script contains the New-TempFile function, which is a solution to Advanced event 10. The event called for a function, so this script needs to be dot-sourced to run.
# Script: 	Advanced_10.ps1
# Author: 	Justin Stokes
# Date:		04/15/11

function New-TempFile{
		Create a New Temporary file with provided data.
		The New-TempFile function creates a new Temporary file with supplied data. The directory and filname can be set by the user,
		but by default files are created in $env:TEMP and conform a Temp-?????.tmp naming format (? represent 1-5 random letters).
		All actions are logged in $env:TEMP\NewTempFile.log ($env:TEMP is the users local temp directory )
		Specifies data to be placed in temporary file
	.PARAMETER fileName
		Specifies a name for the temporary file if the default is not desired.
		Specifies a directory for the temporary file if $env:FILEDIR is not desired
		Spawns a Notepad.exe process with either the temporary file or logfile (if ViewLog switch used).
		Specifies that Ascii encoding should be used in temp file creation
		Spawns the script logfile in Notepad.exe.
	.PARAMETER EraseTemps
		Erases temporary files created by this script. Confirmation is required for deletion actions.
		"Testing 123" | New-TempFile -Notepad
		This will create a new temo file in $env:TEMP with the ocntents "Testing 123". It will then open that file in Notepad.
		New-TempFile -LogFile
		This will open the logfile in Notepad.exe
		New-TempFile -Data "Testing 123" -Ascii -Verbose
		This will create a tempfile with Ascii encoding at $env:TEMP and outline the process as the scipt runs
	#data being passed into Temp file
	#used to specify a temp file name
	#used to specify a folder location
	$fileDir = $env:TEMP
	#if this switch is used a copy of notepad will be spawned
	$notepad = $false
	#used to specify ASCII encoding for log file
	$Ascii = $false
	#if this switch is used the logfile will be opened
	$ViewLog = $false
	#this switch will erase old log files
	$EraseTemps = $false
		$rand = New-Object System.Random
		if(!(Test-Path "$fileDir\NewTempFile.log")){
			Write-Verbose "Log file does not exist. Creating logfile $fileDir\NewTempFile.log"
			New-Item "$fileDir\NewTempFile.log" -type File
		switch ($PSCmdlet.ParameterSetName){
					Write-Verbose "Generating random filename"
					$tempName = $null
					1..5 |foreach {
						$randL = $rand.next(65,122) 
						if($randL -le 90 -or $randL -ge 97){$tempName += [char]$randL}
					$filename = "Temp" + "-" + $tempName + ".tmp"
					Write-Verbose "$filename will be used for temp file"
					if(Test-Path "$filedir\$filename" ){Write-Warning "$filedir\$filename will be overwritten"}
					Write-Verbose "Writing data to $filedir\$filename"
						Write-Verbose "Using ASCII encoding"
						$encode = "ASCII"
						Write-Verbose "Using Unicode (Default) encoding"
						$encode = "Default"
					"Temp file created by New-TempFile by $($env:USERNAME) at $(get-date)" | Out-File -Encoding $encode -FilePath "$filedir\$filename"
					"-------------------------------------------------------------------" | Out-File -Encoding $encode -FilePath "$filedir\$filename" -Append
					$data | Out-File -Encoding $encode -FilePath "$filedir\$filename" -Append
					Write-Verbose "making log entry"
					"$filedir\$filename written by $($env:USERNAME) at $(get-date)" |Out-File -FilePath "$fileDir\NewTempFile.log" -Append
					$openfile = "$filedir\$filename"
				if(!$notepad){$notepad = $true}
				$openfile = "$fileDir\NewTempFile.log"
				write-verbose "Finding Temp Files"
				$tempfiles = ls "$fileDir\TEMP-*.tmp"
				$tempfiles |foreach {
					Remove-Item $_ -Confirm
					Write-Verbose "making log entry"
					"$($_.FullName) deleted at $(get-date)" |Out-File -FilePath "$fileDir\NewTempFile.log" -Append
		if($data){Write-Host "The following file has been created $filedir\$filename"}
			Write-Verbose "Launching Notepad"
			#using Diagnostics.Process instead of start-process so that external process is spawned
			$NotepadProcess = [Diagnostics.Process]::Start('c:\Windows\notepad.exe'," $openfile") 
			Write-Verbose "Spawned $NotepadProcess"