Average Rating:

2.00 by 2 users.

27 Downloads, 68 Views

1 Comment(s)

sendto-tempfile.ps1

By: Scott A. Alvarino 20 Apr 2011 07:26 PM UTC in the category: Advanced Event 10

Tags:

Description:

This function saves the pipeline to a temp file and lets you immediately view if a value of notepad is supplied for -value
#----------------------------------------------------------------------------------
# Date: 04/20/2011 10:58:00
# Comments: Use PowerShell to get pipeline output and save to temp file
# Created By: Scott Alvarino >>  "Your Miami Tech Guy"
# www.techjunkie.tv
# www.scottalvarino.com
# Advanced Event 10: Use PowerShell to pipeoutput to a temp file
#-----------------------------------------------------------------------------------





function sendto-tempfile{
<#
     
.SYNOPSIS

This script sends your output to a temp file
 
The script then passes the original objects that you kept along the
pipeline.
 
.EXAMPLE
 
Get-Process | sendto-tempfile -encoding UNICODE -value notepad
Gets all of the processes running on the system, and displays them to you in notepad from the temp file.

.EXAMPLE

Get-process | sendto-tempfile
Gets all the info but only saves it to the temp file

.DESCRIPTION
    
This function saves the pipeline to a temp file      
#>



[CmdletBinding(supportsshouldprocess=$true)]
param (
        [parameter(Valuefrompipeline=$true)] $file,
        [string]$encoding = "DEFAULT",
        [string]$value
        
       ) 
 begin { 
        $filename = [System.IO.Path]::GetTempFileName()
        }
 
 process 
        { 
        ForEach-Object {Out-File -Encoding $encoding -FilePath $filename -Append -InputObject $_ } |
                }
 
 

  end {  
        switch ($Value) {
       "notepad" { notepad $filename ;$filename | Format-Table }
       default {$filename | Format-Table }
       }
              }     
}
Top

Comments:

4/20/2011 7:28 PM
Please visit site and support my sponsors
http://scottalvarino.com
http://techjunkie.tv
Scott