Average Rating:

3.00 by 2 users.

42 Downloads, 201 Views

2011psa10.ps1

By: Polin Yan 22 Apr 2011 01:49 AM UTC in the category: Advanced Event 10

Description:

2011 Scripting Games. Advanced Event 10.
#-------------------------------------------------------------------------
# 2011 Scripting Games. Advanced Event 10
# NAME: 2011PSA10.ps1
# AUTHOR: polin yan
#-------------------------------------------------------------------------

function Out-TmpFile {
 
    param(  
        [Parameter( Position=0, Mandatory=$true, ValueFromPipeline=$true)]
        [string[]]$data,  
        
        [ValidateSet("unicode","ASCII")]
        [String]$Encoding="unicode",        
        
        [Switch]$ShowContent=$false
    )
    
    begin {
        $outfolder = $env:tmp # user's temp folder
        $outfile = "~{0:yyyyMMdd_HHmmss}.tmp" -f (Get-Date) # temp file name based on date and time
        $outfilepath = $outFolder + "\" + $outfile
        Out-File -FilePath $outfilepath -Encoding $encoding
    } 
    
    process {
        Out-File -FilePath $outfilepath -Append -Encoding $encoding -InputObject $data
    } 
               
    end {
        if ($ShowContent) { notepad.exe $outfilepath }
        return $outfilepath
    }
    
    <#
    .Synopsis
        Output data to a temporary text file.
    .Description
        This function saves the content to a temporary text file. It supports an encoding paramter and also provides a switch to view the saved content in Notepad. The function returns the path to the temporary file.
    .Parameter Data
        Specifies the input data.
    .Parameter Encoding
        Specifies the outfile encoding type. Accepted values are ASCII and unicode. The default encoding value is unicode.
    .Parameter ShowContent
        A switch that specifies to open the output text file.
    .Example
        Out-TmpFile -Data (Get-Dat)
        Find out the current date and time info and save the result to a text file with default unicode encoding type.
    .Example
        Get-Content $env:windir\windowsupdate.log -TotalCount 50 | Out-TmpFile -Encoding ASCII -ShowContent
        Retrieve 50 lines from windows update log, save them to a text file with ASCII encoding type, and open the file.
    #requires -version 2.0
    #>
    
}
Top

Comments: