<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>template | Memos for Admins</title><link>https://www.memosforadmins.com/tags/template/</link><atom:link href="https://www.memosforadmins.com/tags/template/index.xml" rel="self" type="application/rss+xml"/><description>template</description><generator>Source Themes Academic (https://sourcethemes.com/academic/)</generator><language>en-us</language><copyright>© MemosForAdmins 2024</copyright><lastBuildDate>Thu, 29 Aug 2019 21:18:25 +0200</lastBuildDate><image><url>https://www.memosforadmins.com/img/icon-192.png</url><title>template</title><link>https://www.memosforadmins.com/tags/template/</link></image><item><title>Powershell Template</title><link>https://www.memosforadmins.com/post/powershelltemplate/</link><pubDate>Thu, 29 Aug 2019 21:18:25 +0200</pubDate><guid>https://www.memosforadmins.com/post/powershelltemplate/</guid><description>&lt;p&gt;There are a lot of best practices when it comes to PowerShell scripting. This is the template I like to use.&lt;/p&gt;
&lt;p&gt;Some of the features of the template are:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Command based help. The synopsis header helps us and others to make sense of the script&amp;rsquo;s purpose. The help can be displayed by using &lt;code&gt;get-help&lt;/code&gt;, described &lt;a href=&#34;https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/get-help?view=powershell-6&#34; target=&#34;_blank&#34;&gt;here&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;A &lt;code&gt;requires&lt;/code&gt; statement, to make the script work for PowerShell versions later than &lt;code&gt;v3.0&lt;/code&gt;. We can also easily require admin access or a specific module to be available.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Set-StrictMode&lt;/code&gt; to &lt;a href=&#34;https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/set-strictmode?view=powershell-6&#34; target=&#34;_blank&#34;&gt;enforce coding rules&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Set the error action preference to stop. When something doesn&amp;rsquo;t behave properly we want the script to fail fast, with a lot of noise.&lt;/li&gt;
&lt;li&gt;A transcript in the temp folder with a name based on the script name and the DateTime when it was run.&lt;/li&gt;
&lt;li&gt;A stopwatch to record the time taken.&lt;/li&gt;
&lt;/ul&gt;
&lt;pre&gt;&lt;code class=&#34;language-powershell&#34;&gt;&amp;lt;#
.SYNOPSIS
Script to &amp;lt;what will the script do&amp;gt;
.DESCRIPTION
This script will &amp;lt;Elaborate on what the script does&amp;gt;
.PARAMETER Param1
Specifies &amp;lt;What? Is the parameter required?&amp;gt;
.INPUTS
&amp;lt;Does the script accept an input&amp;gt;
.OUTPUTS
A log file in the temp directory of the user running the script
.NOTES
Version: 0.1
Author: Sven de Windt
Creation Date: &amp;lt;Date&amp;gt;
Purpose/Change: Initial script development
.EXAMPLE
&amp;lt;Give multiple examples of the script if possible&amp;gt;
#&amp;gt;
#requires -version 3.0
#-----------------------------------------------------------[Parameters]-----------------------------------------------------------
param(
[CmdletBinding()]
[parameter(mandatory = $false)][String]$Param1
)
#---------------------------------------------------------[Initialisations]--------------------------------------------------------
Set-StrictMode -Version Latest
# Set Error Action to Silently Continue
$ErrorActionPreference = &amp;quot;Stop&amp;quot;
# Dot Source required Function Libraries
#. &amp;quot;C:\Scripts\Functions\Logging_Functions.ps1&amp;quot;
#----------------------------------------------------------[Declarations]----------------------------------------------------------
$LogNumber = Get-Date -UFormat &amp;quot;%Y-%m-%d@%H-%M-%S&amp;quot;
$Log = &amp;quot;$($env:TEMP)\$($MyInvocation.MyCommand.Name) $($LogNumber).log&amp;quot;
$ScriptVersion = &amp;quot;0.1&amp;quot;
#-----------------------------------------------------------[Functions]------------------------------------------------------------
#-----------------------------------------------------------[Execution]------------------------------------------------------------
Start-Transcript -Path $Log -NoClobber
$StopWatch = New-Object System.Diagnostics.Stopwatch
$StopWatch.Start()
Write-Output &amp;quot;Start script - version $($ScriptVersion)&amp;quot;
#-----------------------------------------------------------[Finish up]------------------------------------------------------------
Write-Output $StopWatch.Elapsed
$StopWatch.Stop()
Write-Output &amp;quot;Finished script - $($MyInvocation.MyCommand.Name)&amp;quot;
Stop-Transcript
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Enjoy!&lt;/p&gt;</description></item></channel></rss>