-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGlobalFunctions.ps1
More file actions
306 lines (282 loc) · 8.75 KB
/
GlobalFunctions.ps1
File metadata and controls
306 lines (282 loc) · 8.75 KB
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
############################################
## Functions
############################################
Function Get-Now
{
[CmdletBinding()]
param
(
[Parameter(Mandatory=$true)]
[System.String]
$Format
)
$Now = Get-Date -Format $Format;
Return $Now;
}
Function Write-Log
{
[CmdletBinding()]
param
(
[Parameter(Mandatory=$true)]
[System.String]
$File,
[parameter(Mandatory=$true)]
[System.String]
$Text,
[parameter(Mandatory=$true)]
[ValidateSet("Information", "Error", "Warning")]
[System.String]
$Status
)
$Output = ("[" + (((Get-Date).ToShortDateString()) + "][" + (Get-Date).ToLongTimeString()) + "][" + $Status + "] " + $Text);
$Output | Out-File -Encoding UTF8 -Force -FilePath $File -Append;
Return Write-Output $Output;
}
##Author: Nicola Suter, Kudos to Tobias Renström for Get-ADGroupMembership, Test-ADGroupMemberShip and Test-RunningAsSystem
Function Get-ADGroupMembership {
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[System.String]
$UserPrincipalName
)
process {
try {
if ([System.String]::IsNullOrEmpty($env:USERDNSDOMAIN) -and [System.String]::IsNullOrEmpty($searchRoot)) {
Write-Error "Security group filtering won't work because `$env:USERDNSDOMAIN is not available!"
Write-Warning "You can override your AD Domain in the `$overrideUserDnsDomain variable"
Write-Output "Security group filtering won't work because `$env:USERDNSDOMAIN is not available!"
exit 1
}
else {
# if no domain specified fallback to PowerShell environment variable
if ([System.String]::IsNullOrEmpty($searchRoot)) {
$searchRoot = $env:USERDNSDOMAIN
}
$searcher = New-Object -TypeName System.DirectoryServices.DirectorySearcher
$searcher.Filter = "(&(userprincipalname=$UserPrincipalName))"
$searcher.SearchRoot = "LDAP://$searchRoot"
$distinguishedName = $searcher.FindOne().Properties.distinguishedname
$searcher.Filter = "(member:1.2.840.113556.1.4.1941:=$distinguishedName)"
[void]$searcher.PropertiesToLoad.Add("name")
$list = [System.Collections.Generic.List[String]]@()
$results = $searcher.FindAll()
foreach ($result in $results) {
$resultItem = $result.Properties
[void]$List.add($resultItem.name)
}
$list
}
}
catch {
#Nothing we can do
Write-Warning $_.Exception.Message
}
}
}
#Import JSON file contents into array for processing.
Function Get-JSON {
[CmdletBinding()]
param
(
[Parameter(Mandatory=$true)]
[System.String]
$File
)
$ImportJson = Get-Content $File | Out-String -ErrorAction SilentlyContinue
$processJSON = $ImportJson | ConvertFrom-Json -ErrorAction Stop
$processJSON = foreach ($u in $processJSON) {
[PSCustomObject]@{
id = $($u.id)
email = $($u.email)
firstName = $($u.firstName)
lastName = $($u.lastName)
userType = $($u.userType)
}
}
}
Function Test-Elevation {
Write-Host "Checking for elevation"
If (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole(`
[Security.Principal.WindowsBuiltInRole] "Administrator"))
{
Write-Warning "Sorry, you need to run this script from an elevated PowerShell prompt!`nPlease start the PowerShell prompt as an Administrator and re-run the script."
Write-Warning "Aborting script..."
Break
}
Write-Host "PowerShell runs elevated, OK, continuing...`n" -ForegroundColor Green
}
#check if running as system
Function Test-RunningAsSystem {
process {
return [bool]($(whoami -user) -match "S-1-5-18")
}
}
## Testing if groupmembership is given for user
Function Test-GroupMembership {
[CmdletBinding()]
param (
[Parameter(Mandatory=$true)]
[System.String]
$driveMappingConfig,
[Parameter(Mandatory=$true)]
[System.String]
$groupMemberships
)
try {
$obj = foreach ($d in $driveMappingConfig) {
if (-not ([string]::IsNullOrEmpty($($d.GroupFilter)))) {
foreach ($filter in $($d.GroupFilter)) {
if ($groupMemberships -contains $filter) {
$d
}
else {
#no match for group
}
}
}
else {
$d
}
}
$obj
}
catch {
Write-Error "Unknown error testing group memberships: $($_.Exception.Message)"
}
}
Function Test-ScheduleTask
{
[CmdletBinding()]
param
(
[Parameter(Mandatory=$true)]
[System.String]
$Name
)
#Create a new schedule object.
$Schedule = New-Object -com Schedule.Service;
#Connect to the store.
$Schedule.Connect();
#Get schedule tak folders.
$Task = $Schedule.GetFolder("\").GetTasks(0) | Where-Object {$_.Name -eq $Name -and $_.Enabled -eq $true};
#If the task exists and is enabled.
If($Task)
{
Return $true;
}
Else
{
Return $false;
}
}
Function Test-Module {
Param(
[String]$Name,
[String]$Scope
)
try {
Write-Output "Module:`t $Name `tSTATUS=SEARCHING"
if ($null -eq (Get-InstalledModule -Name $Name -ErrorAction Stop -Verbose:$false)) {
Write-Output "Module:`t $Name `tSTATUS=VERSIONCHECK"
$LatestModuleVersion = (Find-Module -Name $Name -ErrorAction Stop -Verbose:$false).Version
if ($LatestModuleVersion -gt $Name.Version) {
Write-Output "Module:`t $Name `tSTATUS=UPDATING `tVERSION=$($LatestModuleVersion.ToString())"
#$UpdateModuleInvocation = Update-Module -Name $Name -Scope $Scope -Force -ErrorAction Stop -Confirm:$false -Verbose:$false
Update-Module -Name $Name -Scope $Scope -Force -ErrorAction Stop -Confirm:$false -Verbose:$false
}
if ($LatestModuleVersion -eq $Name.Version) {
Write-Output "Module:`t $Name `tSTATUS=PASSED"
}
} else {
Write-Output "Module:`t $Name `tSTATUS=INSTALLED"
}
}
catch [System.Exception] {
Write-Output "Module:`t $Name `tSTATUS=MISSING"
try {
# Install NuGet package provider
$PackageProvider = Install-PackageProvider -Name NuGet -Scope $Scope -Force -Verbose:$false
# Check if PSGallery is a trusted source, and if not, add it
$PSGallery = Get-PSRepository -Name PSGallery -ErrorAction Stop -Verbose:$false
if ($PSGallery.InstallationPolicy -ne "Trusted") {
Write-Output "Adding PSGallery as trusted source"
Set-PSRepository -Name PSGallery -InstallationPolicy Trusted
}
# Install module
Install-Module -Name $Name -Scope $Scope -Force -ErrorAction Stop -Confirm:$false -Verbose:$false
Write-Output "Module:`t $Name `tSTATUS=INSTALLING"
}
catch [System.Exception] {
Write-Output "Module:`t $Name `tSTATUS=FAILED"
Write-Output "Error installing module: $Name, code $($_.Exception.Message)"
Break
}
finally {
Write-Output "Module:`t $Name `tSTATUS=INSTALLED"
}
}
}
# Deprecated, replace with Invoke-LoginMgGraph
Function Invoke-LoginMSOnline {
<#
Try {
Get-MsolDomain -ErrorAction Stop > $null
}
Catch {
Write-Output "Connecting to Office 365..."
Connect-MsolService
}
Finally {
Write-Output "Connected to MsolService"
}
#>
Write-Output "This cmdlet is deprecated, please use Invoke-LoginMgGraph instead."
}
# Deprecation on March 30, 2024, replace with Invoke-LoginMgGraph
Function Invoke-Login {
Try {
Get-AzureADDomain -ErrorAction Stop > $null
}
Catch {
Write-Output "Connecting ..."
Connect-AzureAD
}
Finally {
Write-Output "Connected to Azure Active Directory PowerShell for Graph"
}
#>
Write-Output "This cmdlet is scheduled for deprecation on March 30, 2024. Please use Invoke-LoginMgGraph instead."
}
# Replace with Invoke-LoginMgGraph
Function Invoke-LoginMSGraph {
Write-Output "This cmdlet should be replaced, use Invoke-LoginMgGraph instead."
Try {
Get-Organization -ErrorAction Stop > $null
}
Catch {
Write-Output "Connecting ..."
Connect-MSGraph -ForceInteractive
Update-MSGraphEnvironment -SchemaVersion beta
}
Finally {
Write-Output "Connected to Microsoft Graph PowerShell"
}
}
# Current authentication method.
Function Invoke-LoginMgGraph {
Param(
[String]$Scopes
)
Try {
Get-Organization -ErrorAction Stop > $null
}
Catch {
Write-Output "Connecting ..."
Connect-MgGraph -Scopes $Scopes -NoWelcome
}
Finally {
Write-Output "Connected to Microsoft Graph PowerShell"
}
}