-
Notifications
You must be signed in to change notification settings - Fork 0
/
Library.ps1
177 lines (149 loc) · 3.12 KB
/
Library.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
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
<#
.SYNOPSIS
Copies with Robocopy
.EXAMPLE
Copy-Robocopy -Source $RemoteVMs -Destination $LocalVMs -Progress
.PARAMETER Source
Source
.PARAMETER Destination
Destination
.PARAMETER Progress
If set, show Robocopy output
#>
Function Copy-Robocopy($Source, $Destination, [switch]$Progress)
{
If($Progress)
{
Start-Process -FilePath "Robocopy.exe" -ArgumentList "`"$Source`" `"$Destination`" /MIR" -NoNewWindow -Wait
}
Else
{
Start-Process -FilePath "Robocopy.exe" -ArgumentList "`"$Source`" `"$Destination`" /MIR" -WindowStyle hidden -Wait
}
}
<#
.SYNOPSIS
Pause the script (was not included prior to Powershell 3.0)
.EXAMPLE
Pause-Script
.EXAMPLE
Pause-Script -PauseMessage "This script is done"
.PARAMETER PauseMessage
Message to the user (only shown if version is lower than 3)
.PARAMETER Exit
If set, exits after the Pause
#>
Function Pause-Script($PauseMessage = "Press any key to continue ...",[switch]$Exit)
{
# This does not work on PS1 as reported on StackOverflow; however this is not an issue since we are using V2 upwards
If($PSVersionTable.PSVersion.Major -lt 3)
{
Write-Host $PauseMessage
$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
}
Else
{
Pause
}
If($Exit)
{
Exit
}
}
<#
.SYNOPSIS
Test if a directory exists, if not, create it
.EXAMPLE
$DistributionBundles = "$Bundle\core\distribution\bundles"
TestCreate-Directory -Dir $DistributionBundles
.PARAMETER Dir
Directory to test
.PARAMETER Message
Show a message to the user if needed or not
#>
Function TestCreate-Directory($Dir,[switch]$Message)
{
If(-not(Test-Path $Dir))
{
If($Message)
{
Write-Host "$Dir not found, creating..."
}
New-Item -Path $Dir -ItemType directory | Out-Null
}
Else
{
If($Message)
{
Write-Host "$Dir found"
}
}
}
<#
.SYNOPSIS
Test if a file or directory exists. If not, show a message, pause and exit
.EXAMPLE
$SourceDir="..\..\..\source\windows"
TestMessage-Path -Path $SourceDir -Type "Source directory"
.PARAMETER Path
File or directory to test
.PARAMETER Type
Type of item (needed for message)
.PARAMETER Recovery
What to do, if it is not found (optional parameter)
.PARAMETER Message
Show a message, if it is found
#>
Function TestMessage-Path($Path, $Type, $Recovery, [switch]$Message)
{
If(!(Test-Path $Path))
{
Write-Host "$Type $Path not found" -ForegroundColor Red
Write-Host $Recovery
Pause-Script -Exit
}
Else
{
If($Message)
{
Write-Host "$Type $Path was found"
}
}
}
<#
.SYNOPSIS
Show the current step in yellow
.EXAMPLE
Write-Step -Step 1 -Message "Checking requirements"
.PARAMETER Step
Number of step
.PARAMETER Message
Message to show
#>
Function Write-Step($Step, $Message)
{
Write-Host "`nStep ${step}: $Message`n" -ForegroundColor Yellow
}
<#
.SYNOPSIS
Show success in green
If message is given, write it
Else, just write a generic success message
.EXAMPLE
Write-Step -Step 1 -Message "Checking requirements"
.PARAMETER Message
Message to show, if needed
#>
Function Write-Success($Message)
{
$SuccessMessage = "Success"
If($Message)
{
$SuccessMessage += ": $Message"
}
Else
{
$SuccessMessage += "ful"
}
Write-Host "$SuccessMessage" -ForegroundColor Green
}