Average Rating:

4.50 by 3 users.

39 Downloads, 227 Views

1 Comment(s)

Out-TempFile.ps1

By: Robert van den Nieuwendijk 21 Apr 2011 09:51 PM UTC in the category: Advanced Event 10

Description:

Submission for the 2011 Scripting Games Advanced Event 10.

Sends output to a temporary text file. The temporary file gets a temporary name, and it resides in the temporary folder.
Function Out-TempFile {
  <#
    .SYNOPSIS
      Sends output to a temporary text file.

    .DESCRIPTION
      Sends output to a temporary text file. The temporary file gets a temporary name, and it resides in the temporary folder.
	
    .PARAMETER InputObject
      Specifies the objects to be written to the file. Enter a variable that contains the objects or type a command or expression that gets the objects.
  
    .PARAMETER Encoding
      Specifies the type of character encoding used in the file. Valid values are "ASCII" and "Unicode". "Unicode" is the default. 

    .PARAMETER Notepad
      Allows the cmdlet to open the temporary file in Notepad after it is written.

    .EXAMPLE
      C:\PS>Get-Process | Out-Tempfile
	  
      Description
      -----------
      This command sends a list of processes on the computer to the temporary file.

    .EXAMPLE
      C:\PS>$a = Get-Process
      C:\PS>Out-TempFile -InputObject $a -Encoding ASCII -Notepad

      Description
      -----------
      This command also sends a list of processes to the temporary file. The file will be encoded in "ASCII". And the file will be opened in Notepad.

    .INPUTS
      System.Management.Automation.PSObject
        You can pipe any object to Out-TempFile.
		
    .OUTPUTS
      System.String
        Out-TempFile returns the path to the temporary file.
  #>

  #Parameter definitions

  [CmdletBinding()]
  param
  (
    [Parameter(Position=0)]
    [ValidateSet('unicode','ascii')]
    [ValidateNotNullOrEmpty()]
    [System.String]
    $Encoding="unicode",
	
    [Parameter(ValueFromPipeline=$true)]
    [System.Management.Automation.PSObject]
    $InputObject,
	
    [switch]
    $Notepad,
	
    [switch]
    $WhatIf
  )
 
  begin
  {
    # Construct the name of the temporary file.
    $FileName = "$env:TEMP\Temp-$(Get-Random).txt"
	
    # The $Objects variable is used to collect all the input objects.
    $Objects = @()	
  }
  
  process
  {
    # Add the input object to $Objects.
    $Objects += $InputObject
  }
  
  end
  {
    # Write the temporary file.
    Try {
      if ($WhatIf) {
        Write-Output "What if: Performing operation ""Output to temporary File"" on Target ""$FileName""."
      }
      else {
        Write-Verbose "Performing operation ""Output to temporary File"" on Target ""$FileName""."
        Write-Debug "Performing operation ""Output to temporary File"" on Target ""$FileName""."
        Out-File -FilePath $FileName -Encoding $Encoding -InputObject $Objects
      }
    }
    Catch {
      Write-Error "Unable to create and write to the temporary file $FileName."
      return
    }
    	
    # Open the temporary file in Notepad if requested.
    Try {
      if ($Notepad) {
        if ($WhatIf) {
          Write-Output "What if: Performing operation ""Opening temporary File $FileName in Notepad""."
        }
        else {
          Write-Verbose "Performing operation ""Opening temporary File $FileName in Notepad""."
          Write-Debug "Performing operation ""Opening temporary File $FileName in Notepad""."
          Notepad $FileName
        }
      }
    }
    Catch {
      Write-Error "Unable to open the temporary file in Notepad."
      return
    }
	
    # Return the name of the temporary file.
    Write-Output $FileName
  }
}
Top

Comments:

4/22/2011 12:25 AM
excellent job.