Average Rating:

4.00 by 2 users.

45 Downloads, 199 Views

Advanced_10.ps1

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

Description:

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{
	<#
	.SYNOPSIS
		Create a New Temporary file with provided data.
	.DESCRIPTION
		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 )
	.PARAMETER Data
		Specifies data to be placed in temporary file
	.PARAMETER fileName
		Specifies a name for the temporary file if the default is not desired.
	.PARAMETER fileDir
		Specifies a directory for the temporary file if $env:FILEDIR is not desired
	.PARAMETER Notepad
		Spawns a Notepad.exe process with either the temporary file or logfile (if ViewLog switch used).
	.PARAMETER Ascii
		Specifies that Ascii encoding should be used in temp file creation
	.PARAMETER ViewLog
		Spawns the script logfile in Notepad.exe.
	.PARAMETER EraseTemps
		Erases temporary files created by this script. Confirmation is required for deletion actions.
	.EXAMPLE
		"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.
	.EXAMPLE
		New-TempFile -LogFile
		
		This will open the logfile in Notepad.exe
	.EXAMPLE
		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
		
	#>
	param(
	#data being passed into Temp file
	[Parameter(Mandatory=$false,ValueFromPipeline=$true,ParameterSetName='LogData')]
	$data
	,
	#used to specify a temp file name
	[Parameter(Mandatory=$false)]
	[string[]]
	[Alias('TempFile','File','Target')]
	$fileName
	,
	#used to specify a folder location
	[Parameter(Mandatory=$false)]
	[string[]]
	[Alias('TempDir','Directory','Location')]
	$fileDir = $env:TEMP
	,
	#if this switch is used a copy of notepad will be spawned
	[Parameter(Mandatory=$false)]
	[switch]
	[Alias('Open')]
	$notepad = $false
	,
	#used to specify ASCII encoding for log file
	[Parameter(Mandatory=$false)]
	[switch]
	[Alias('AsciiEncode')]
	$Ascii = $false
	,
	#if this switch is used the logfile will be opened
	[Parameter(Mandatory=$false,ParameterSetName='Log')]
	[switch]
	[Alias('History','ShowLog')]
	$ViewLog = $false
	,
	#this switch will erase old log files
	[Parameter(Mandatory=$false,ParameterSetName='EraseTemp')]
	[switch]
	[Alias('CleanUp','RemoveTempFiles')]
	$EraseTemps = $false
	)
	begin{
		$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
		}
	}
	process{
		switch ($PSCmdlet.ParameterSetName){
			'LogData'{
				if(!$fileName){
					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($data){
					if(Test-Path "$filedir\$filename" ){Write-Warning "$filedir\$filename will be overwritten"}
					Write-Verbose "Writing data to $filedir\$filename"
					if($Ascii){
						Write-Verbose "Using ASCII encoding"
						$encode = "ASCII"
					}
					else{
						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"
				}
			}
			'Log'{
				if(!$notepad){$notepad = $true}
				$openfile = "$fileDir\NewTempFile.log"
			}
			'EraseTemp'{
				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
				}
			}
		}
	}
	end{
		if($data){Write-Host "The following file has been created $filedir\$filename"}
		if($notepad){
			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"
		}
	}
}
Top

Comments: