Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
PowerShell: Script Template
#requires -version 2
<#
.SYNOPSIS
<Overview of script>
.DESCRIPTION
<Brief description of script>
.PARAMETER <Parameter_Name>
<Brief description of parameter input required. Repeat this attribute if required>
.INPUTS
<Inputs if any, otherwise state None>
.OUTPUTS
<Outputs if any, otherwise state None - example: Log file stored in C:\Windows\Temp\<name>.log>
.NOTES
Version: 1.0
Author: <Name>
Creation Date: <Date>
Purpose/Change: Initial script development
.EXAMPLE
<Example goes here. Repeat this attribute for more than one example>
#>
#---------------------------------------------------------[Initialisations]--------------------------------------------------------
#Set Error Action to Silently Continue
$ErrorActionPreference = "SilentlyContinue"
#Dot Source required Function Libraries
. "C:\Scripts\Functions\Logging_Functions.ps1"
#----------------------------------------------------------[Declarations]----------------------------------------------------------
#Script Version
$sScriptVersion = "1.0"
#Log File Info
$sLogPath = "C:\Windows\Temp"
$sLogName = "<script_name>.log"
$sLogFile = Join-Path -Path $sLogPath -ChildPath $sLogName
#-----------------------------------------------------------[Functions]------------------------------------------------------------
<#
Function <FunctionName>{
Param()
Begin{
Log-Write -LogPath $sLogFile -LineValue "<description of what is going on>..."
}
Process{
Try{
<code goes here>
}
Catch{
Log-Error -LogPath $sLogFile -ErrorDesc $_.Exception -ExitGracefully $True
Break
}
}
End{
If($?){
Log-Write -LogPath $sLogFile -LineValue "Completed Successfully."
Log-Write -LogPath $sLogFile -LineValue " "
}
}
}
#>
#-----------------------------------------------------------[Execution]------------------------------------------------------------
#Log-Start -LogPath $sLogPath -LogName $sLogName -ScriptVersion $sScriptVersion
#Script Execution goes here
#Log-Finish -LogPath $sLogFile
@ghost

This comment has been minimized.

Copy link

@ghost ghost commented Jan 12, 2015

How would one dump the result of the function to the log on success?

@9to5IT

This comment has been minimized.

Copy link
Owner Author

@9to5IT 9to5IT commented Jan 13, 2015

You can just use the Log-Write function, so for example replace the "Completed Successfully" on line

Log-Write -LogPath $sLogFile -LineValue "Completed Successfully."

with the variable that contains the result of what you are executing. So for example lets say $Result is the variable with the data you want to output then you can simply do this:

Log-Write -LogPath $sLogFile -LineValue $Result

The If($?){...} will only execute when the function was successful.

@dragon788

This comment has been minimized.

Copy link

@dragon788 dragon788 commented Sep 21, 2016

You could also use a "trap" if you want to always get the full output of a command whether it failed or succeeded in case a stack trace doesn't help you any.

@hamsdre

This comment has been minimized.

Copy link

@hamsdre hamsdre commented Oct 13, 2017

I see you use custom functions for logging. Why don't you just use the Write-Output / Write-Progress / Write-Verbose cmdlet?

@NeverEnoughCoffeeMan

This comment has been minimized.

Copy link

@NeverEnoughCoffeeMan NeverEnoughCoffeeMan commented Jul 17, 2018

Why don't you just use the Write-Output / Write-Progress / Write-Verbose cmdlets?

Creating a small custom function would allow you to prepend a time stamp to the log entry.

function Write-Log {
    param (
        [Parameter(Mandatory=$False, Position=0)]
        [String]$Entry
    )

    "$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss.fff') $Entry" | Out-File -FilePath $LogFilePath -Append
}

$ExitCode = 0
$LogFilePath = "C:\Logs\MyScript.log"

Write-Log -Entry "Script MyScript started on $(Get-Date -Format 'dddd, MMMM dd, yyyy')."
Write-Log
Write-Log -Entry "Do stuff here."
Write-Log
Write-Log -Entry "Script MyScript ended ($ExitCode)."

Which results in this:

2018-07-17 00:39:06.719 Script MyScript started on Tuesday, July 17, 2018.
2018-07-17 00:39:06.720 
2018-07-17 00:39:06.725 Do stuff here.
2018-07-17 00:39:06.726 
2018-07-17 00:39:06.727 Script MyScript ended (0).

You'll also find PowerShell custom log functions that create SCCM cmtrace-style logs.

@tresf

This comment has been minimized.

Copy link

@tresf tresf commented Jul 20, 2018

@99to5IT, my Get-Help won't work unless I put a newline between #requires -version 2 and <#. This seems to be consistent with PowerShell on Windows 10 as well as MacOS.

@klborders

This comment has been minimized.

Copy link

@klborders klborders commented Feb 17, 2019

This is a beautiful way of building out a script! TY!

Jsnover would be proud.

@krptodr

This comment has been minimized.

Copy link

@krptodr krptodr commented Jan 10, 2020

@tresf

@99to5IT, my Get-Help won't work unless I put a newline between #requires -version 2 and <#. This seems to be consistent with PowerShell on Windows 10 as well as MacOS.

I can confirm, you are correct.

@dcvlehr

This comment has been minimized.

Copy link

@dcvlehr dcvlehr commented Feb 13, 2020

Is this up to date for version 5? Or are there certain things that can be removed? Or replaced with a new v5 cmdlet or format?

@pvcodes

This comment has been minimized.

Copy link

@pvcodes pvcodes commented Aug 29, 2020

I only want a snippet that do

  1. when i enter "run"
  2. hit tab key, It simply autocomplete the expression to "g++ -g **.cpp -o main.exe && .\main.exe"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment