Average Rating:

5.00 by 2 users.

46 Downloads, 201 Views

2 Comment(s)

adv10.ps1

By: Francis D 22 Apr 2011 03:49 AM UTC in the category: Advanced Event 10

Description:

Output to a temporary text file in the temp folder
function Out-TempFile
{

<#
.SYNOPSIS
2011 Scripting Games - Advanced Event 10

.DESCRIPTION
Output to a temporary text file in the temp folder

.PARAMETER InputObject
Specifies the objects to be written to the temporary file. Can accept input from the pipeline.

.PARAMETER Encoding
Specifies the type of character encoding used in the temporary file. Unicode is the default.

.PARAMETER Extension
Specifies the extension of the temporary file.

.PARAMETER Notepad
Open the temporary file in notepad.

.PARAMETER Force
Run function even when -Confirm is present

.INPUTS
PsObject. Objects to write to the temporary file

.OUTPUTS
String. Full path of the temporary file

.EXAMPLE
$temp = (Get-Process s*), (Get-Process w*) | Out-TempFile
Get process starting with s and w,
write output to a temporary file, and then
store the full path of the temporary file to variable $temp

.LINK
Out-File

.NOTES
Author: Francis de la Cerna
Created: 2011-04-16
#Requires –Version 2.0
#>

    [CmdletBinding(SupportsShouldProcess = $true)]

    Param(
        [psobject]
        [parameter(mandatory = $false, ValueFromPipeline = $true)]
        $InputObject,

        [parameter(mandatory = $false)]
        [String]
        $Encoding = "Unicode",

        [parameter(mandatory = $false)]
        [String]
        $Extension,
        
        [parameter(mandatory = $false)]
        [switch]
        $Notepad,
        
        [parameter(mandatory = $false)]
        [switch]
        $Force
    )

    Begin
    {
        $tempFolder = [system.environment]::ExpandEnvironmentVariables("%temp%");
        $shouldProcess = $Force -or $psCmdlet.ShouldProcess("$tempFolder");
        
        if (-not $shouldProcess) { return; }
        
        $temp = [io.path]::GetTempFileName();
        try
        {
            if ($Extension) {
                $filename = [io.path]::ChangeExtension($temp, $Extension);
                $params = @{Confirm=$false; Verbose=$false;};
                Move-Item -Path $temp -Destination $filename -Force @params;
                $temp = $filename;
            }
        }
        catch
        {
            Remove-Item $temp -Confirm:$false -Verbose:$false -Force;
            throw $Error[0];
        }
        
        $a = @();        
    }
    
    Process
    {
        if (-not $shouldProcess) { return; }
        $a += $InputObject;
    }
    
    End
    {
        if (-not $shouldProcess) { return; }
        
        try
        {
            $params = @{Confirm=$false; Verbose=$VerbosePreference;};
            $a | Out-File -FilePath $temp -Encoding $Encoding -Force @params;
        }
        catch
        {
            Remove-Item $temp -Confirm:$false -Verbose:$false -Force;
            throw $Error[0];
        }
        
        if ($notepad) {
           notepad $temp;
        }

        return $temp;
    }
}
Top

Comments:

4/22/2011 4:04 AM
nice script that does what it is supposed to. Good use of parameters, and comments. I am glad you clean up after the script. good job.
4/23/2011 6:51 PM
While testing, I opened my temp folder and saw a bunch of temp files being created when I didn't want to, so I cleaned them up. Thank you for the comment.