Lerch Web Wiki

Random, erratic, no responsibility is taken for the correctness of this information

User Tools

Site Tools


powershell:basics:templates

Templates for scripts

ScriptTemplate.ps1
<#
=============================================================================
THIS CODE-SAMPLE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
 
This sample is not supported under any standard support program or service. 
The code sample is provided AS IS without warranty of any kind.
The author further disclaims all implied warranties including, without
limitation, any implied warranties of merchantability or of fitness for a
particular purpose. The entire risk arising out of the use or performance of
the sample and documentation remains with you. In no event shall its author, 
or anyone else involved in the creation, production, or delivery 
of the script be liable for any damages whatsoever (including, without 
limitation, damages for loss of business profits, business interruption, loss
of business information, or other pecuniary loss) arising out of  the use of
or inability to use the sample or documentation, even if the author has been 
advised of the possibility of such damages.
=============================================================================
#>
#Requires –Version 3
 
#region Parameter
[cmdletbinding()]
param(
        [string]$parameterforthisscript
)
 
#endregion
[String]$ScriptVersion = "1.0"
 
#region Functions
function Log2File{
	param(
			[string]$log,
			[string]$text
	)
	"$(Get-Date -Format "yyyyMMdd-HH:mm:ss"):`t$text" | Out-File -FilePath $log -Append
    Write-Verbose "$(Get-Date -Format "yyyyMMdd-HH:mm:ss"):`t$text" 
}
#endregion
 
#region Initialize
$rundatestring = ("{0:yyyyMMdd}" -f (get-date))
$ScriptPath = $MyInvocation.MyCommand.Path | Split-Path
$LogPath = "$ScriptPath\$rundatestring"
if (-not (Test-Path $LogPath -PathType Container)){ $null = mkdir $LogPath }
$LogFile = "$LogPath\$rundatestring-RuntimeLog.log"
[String]$Spacer = "=" * 80
#endregion
 
Log2File -log $LogFile -text $Spacer
Log2File -log $LogFile -text "Starting"
 
#region Do something
 
#endregion
 
Log2File -log $LogFile -text "Ended"
Log2File -log $LogFile -text $Spacer
powershell/basics/templates.txt · Last modified: 2017/02/23 14:39 by marcus