Skip to content

Instantly share code, notes, and snippets.

@tiagoduarte
Created September 24, 2016 13:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tiagoduarte/e1624ef6a6037bfd4099c2fe3501c1e1 to your computer and use it in GitHub Desktop.
Save tiagoduarte/e1624ef6a6037bfd4099c2fe3501c1e1 to your computer and use it in GitHub Desktop.
powershell starter script
#[summary goes here]
#by [author goes here] @ [date goes here]
#[author URL / email]
clear-host
###################################### FUNCTIONS ##############################
#if the context is not administrator, stop the script and start a new shell
function EnsureAdmin($invocation)
{
If (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))
{
$arguments = "& '" + $invocation.mycommand.definition + "'"
Start-Process powershell -Verb runAs -ArgumentList $arguments
throw "running an elevated window!"
}
}
#temporarily go to the folder where the script running is located
function GoToCurrentFolder($invocation)
{
$currentFolder = Get-Location -PSProvider FileSystem
$invokedFolder = [System.Text.RegularExpressions.Regex]::Replace($invocation.MyCommand.Definition, $invocation.MyCommand.Name, "")
if($invokedFolder -ne $currentFolder){ cd $invokedFolder }
}
#add the sharepoint powershell snapin if not added already
function AddSPSnapin()
{
If ((Get-PsSnapin |?{$_.Name -eq "Microsoft.SharePoint.PowerShell"})-eq $null)
{
Write-Host
Write-Host -ForegroundColor White " - Loading SharePoint Powershell Snapin"
$PSSnapin = Add-PsSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue | Out-Null
Write-Host
}
}
function EnsureListExists($web, $listName)
{
$list = $web.Lists.TryGetList($listName)
if($list -eq $null)
{
throw [System.IO.FileNotFoundException] "List $listName not found!"
}
}
function EnableLogging()
{
$format = '{0:yyyyMMdd.hhmmss}' -f (Get-Date)
Start-Transcript -path scriptExecutionOutput.$format.txt -append
}
function DisableLogging()
{
try
{
stop-transcript|out-null
}
catch [System.InvalidOperationException]{}
}
###################################### FUNCTIONS ##############################
try
{
EnsureAdmin $MyInvocation
$nl = [Environment]::NewLine
write-host ("Script execution started!" + $nl)
pushd
GoToCurrentFolder $MyInvocation
#EnableLogging
AddSPSnapin
###context
#$webUrl = "http://testsite"
#$sourceListName = "Tasks"
#$destListName = "TasksArchive"
###ensure objects
#$web = Get-SPWeb $webUrl
#work
#DoStuff $web $sourceListName $destListName
}
catch
{
#handle exceptions
write-warning "Program Crashed!"
write-host ("Stack: " + $_.Exception)
write-host ("Line: " + $_.InvocationInfo.ScriptLineNumber)
write-host ("Column: " + $_.InvocationInfo.OffsetInLine)
}
finally
{
###disposal
#$web.Dispose()
popd
write-host ("Script execution ended!" + $nl)
#DisableLogging
#Read-Host
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment