-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScriptStarter1.ps1
80 lines (63 loc) · 2.61 KB
/
ScriptStarter1.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<#
.SYNOPSIS
.DESCRIPTION
This script will
It is used by _ in the process of/for _
.TESTING
.NOTES
Author : KyleGW
Version : 0.1
.MODIFICATIONS
2000-00-00 KyleGW Created script
.EXAMPLE
#>
#Load logging Function
. .\Write-Log.ps1
$scriptShortName = "Script Short Name"
$verbose = $true
$ErrorActionPreference = "Stop"
$logFilePath = drive:\ScriptLogs
$logFileName = "$((Get-Date).ToString("yyyy-MM-dd HHmmss")) - $env:COMPUTERNAME - $scriptShortName.log"
$LogFile = Join-Path $logFilePath $logFileName
$TranscriptFileName = "$((Get-Date).ToString("yyyy-MM-dd HHmmss")) - $env:COMPUTERNAME - $scriptShortName Transcript.log"
if(Test-Path $MyInvocation.MyCommand.Definition)
{
$0 = split-path -parent $MyInvocation.MyCommand.Definition
}
#Transcripting this session to catch system output that isn't explicitly logged with Write-Log
$TranscriptFile = $logFilePath+$TranscriptFileName
Start-Transcript -Path $TranscriptFile -Append -Force
Write-Host "Logged output can be found in $LogFile"
Write-Log "---------------------------------------------------------------------------------------------"
Write-Log "Script instance Started"
Write-Log "Running under context: $env:USERDOMAIN\$env:USERNAME"
Write-Log "Currently running from $0"
Write-Log "---------------------------------------------------------------------------------------------"
# Must run as administrator
if (-not ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] 'Administrator'))
{
Write-Log "FAILURE - Script not running as administrator. Please re-run script in an administrative context."
Write-Host "Launch script or command window as administrator" -ForegroundColor Red
throw "Failed - Script not running as administrator. Please re-run script in an administrative context."
}
#Test any paths or other pre-req we need
$preReqPath = "C:\doesntexist"
if (-not (test-path $preReqPath)){Write-Log "Necessary filepath does not exist [$preReqPath]"; throw "Necessary filepath does not exist [$preReqPath]"}
# Test - does script need to run at all?
if((gci c:\).count -gt 0)
{
Write-Log "Found condition, running script on some stuff"
#set initial validation condition
if(setsuccessconditions)
{
$success = $true
}
}
else
{
Write-Log "Nothing found to do. Exiting."
}
Stop-Transcript
#fix linefeeds in transcript lof (replace LF with CRLF)
Start-sleep -Seconds 5 #give stop-transcript time to flush buffer and write to disk and close file
(Get-Content $TranscriptFile) -replace "`r","`n" | out-file $TranscriptFile