-
Notifications
You must be signed in to change notification settings - Fork 0
/
PSScriptHelper.psm1
198 lines (169 loc) · 6.73 KB
/
PSScriptHelper.psm1
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#
# Module 'PSScriptHelper'
#
# Generated by: Matticusau
#
# Generated on: 29/08/2017
#
# setup the global variable for the scripts configuration if it doesn't exist
if ($null -eq (Get-Variable -Name 'PSScriptHelperValues' -Scope Global -ErrorAction SilentlyContinue))
{
[PSCustomObject]$Global:PSScriptHelperValues = @{
ScriptName = '';
ScriptPath = '';
}
}
##########################################################################################
# Function: New-FolderPath
# Checks if the supplied path is either a fully qualified path or a sub-directory
# where in that case the supplied base folder or the script location is added
# also includes the option to create the folder if it doesn't exist
##########################################################################################
function New-FolderPath
{
[CmdLetBinding(SupportsShouldProcess=$true)]
[OutputType([String])]
Param(
[Parameter(Mandatory=$true, HelpMessage='The folder path to validate/create')]
[string]$FolderPath
,
[Parameter(Mandatory=$false, HelpMessage='The base folder to create the folder at. Defaults to current script path.')]
[string]$BaseFolder = ""
,
[Parameter(Mandatory=$false, HelpMessage='If supplied the path will be created if it doesnt exist')]
[switch]$CreateFolder
)
try
{
[string]$newFolderPath = "";
#Write-Host "`$BaseFolder = $BaseFolder";
#set the base path depending on if we were provided one or to use the default
if ($BaseFolder.Length -eq 0) {$BaseFolder = $ScriptServerPath;} # Split-Path (Get-Variable MyInvocation -Scope 0).Value.MyCommand.Path
#Write-Host "`$BaseFolder = $BaseFolder";
#Write-Host "`$FolderPath = $FolderPath";
#check if we were given a path
if ($FolderPath.Length -gt 0)
{
#check if we were given a UNC path or qualified path
if ($FolderPath.Substring(1,2) -eq ":\" -or $FolderPath.Substring(0,2) -eq "\\")
{
$newFolderPath = $FolderPath;
}
else
{
#we were given a sub folder
$newFolderPath = Join-Path -Path "$($BaseFolder)" -ChildPath "$($FolderPath)";
}
#Write-Host "`$newFolderPath = $newFolderPath";
#make sure this exists otherwise return nothing
if (!(Test-Path -Path $newFolderPath))
{
if ($CreateFolder = $true)
{
if ($pscmdlet.ShouldProcess("$($newFolderPath)", "Create the folder path"))
{
New-Item $newFolderPath -ItemType Directory | Out-Null;
#make sure we created it
if (!(Test-Path -Path $newFolderPath)) {$newFolderPath = "";}
}
}
else
{
$newFolderPath = "";
}
}
}
else
{
$newFolderPath = "";
}
}
catch
{
#Tell the user something went wrong, we cannot log this to a file because we could be creating the log directory at the time
Write-Error -Message "Could not validate the path $($newFolderPath)." -Category ObjectNotFound;
$newFolderPath = "";
}
finally
{
}
#lastly return the data
return $newFolderPath;
} # fn New-FolderPath
##########################################################################################
# Function: Get-ScriptConfig
# Gets the configuration for the current script
##########################################################################################
function Get-ScriptConfig
{
[CmdLetBinding(SupportsShouldProcess=$true)]
Param(
[Parameter(Mandatory=$false, HelpMessage='The path to the script file to find a config for')]
[string]$ScriptPath=""
,
[Parameter(Mandatory=$false, HelpMessage='The name of the script to retrieve config for')]
[string]$ScriptName=""
,
[Parameter(Mandatory=$true, HelpMessage='The version of the script to validate the config with')]
[double]$ScriptVersion
,
[Parameter(Mandatory=$false, HelpMessage='If supplied the config file will be created')]
[switch]$CreateFile
)
try
{
#set the variables if they were not supplied
if ($ScriptPath.Length -eq 0)
{
$ScriptPath = Split-Path (Get-Variable MyInvocation -Scope 0).Value.MyCommand.Path;
}
if ($ScriptName.Length -eq 0)
{
$ScriptName = $(Split-Path (Get-Variable MyInvocation -Scope 0).Value.MyCommand.Path -Leaf).Replace(".ps1","");
}
#the full path to script config file
[string]$configFilePath = "$($ScriptPath)\$($ScriptName).config"
#check that the file exists
if ((Test-Path -Path $configFilePath) -eq $false)
{
#file not found, should we create it
if ($CreateFile -eq $true)
{
if ($pscmdlet.ShouldProcess("$($configFilePath)", "Create the config file as it does not currently exist"))
{
[xml]$TmpConfigFile = "<Settings ScriptName=`"`" Version=`"0`"><General></General></Settings>";
$TmpConfigFile.Settings.ScriptName = "$($ScriptName)";
$TmpConfigFile.Settings.Version = "$($ScriptVersion)";
$TmpConfigFile.Save($configFilePath);
}
}
else
{
Write-Error -Message "The Script's Config file could not be found" -Category ObjectNotFound;
#Write-Host "Script is terminating";
Break;
}
}
#Create the object to hold the script file
[xml]$Script:Config = Get-Content "$($configFilePath)";
#check that this script file is valid
if ($Script:Config.Settings.ScriptName -ne $ScriptName)
{
Write-Error -Message "The Script's config file is invalid. Incorrect script name, expecting '$($ScriptName)'." -Category InvalidData;
Break;
}
if ($Script:Config.Settings.Version -lt $ScriptVersion)
{
Write-Error -Message "The Script's config file is invalid. Config file is for an older version of the script." -Category InvalidData;
Break;
}
}
catch
{
#Tell the user something went wrong
Write-Error -Message "Could not process the config file, exception was: $($_.Exception.Message)";
}
finally
{
}
} # fn Get-ScriptConfig