-
Notifications
You must be signed in to change notification settings - Fork 0
/
UMN-Common.psm1
846 lines (713 loc) · 23.1 KB
/
UMN-Common.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
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
####### UMN-Common Module ####
###
# Copyright 2017 University of Minnesota, Office of Information Technology
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with Foobar. If not, see <http://www.gnu.org/licenses/>.
###
function Convert-ColumnIndexToA1Notation {
<#
.SYNOPSIS
Short description
.DESCRIPTION
Long description
.EXAMPLE
Example
.NOTES
General notes
#>
[CmdletBinding()]
param (
[Parameter(Mandatory=$true)]
[int]$ColumnIndex
)
process {
while ($ColumnIndex -gt 0) {
$Temp = ($ColumnIndex -1) % 26
#$Letter =
}
}
} #END Convert-ColumnIndexToA1Notation
<#
.Synopsis
convert text or byte array to URL friendly BAse64
.DESCRIPTION
convert text or byte array to URL friendly BAse64
.EXAMPLE
ConvertTo-Base64URL -text $headerJSON
.EXAMPLE
ConvertTo-Base64URL -Bytes $rsa.SignData($toSign,"SHA256")
#>
function ConvertTo-Base64URL
{
param
(
[Parameter(ParameterSetName='String')]
[string]$text,
[Parameter(ParameterSetName='Bytes')]
[System.Byte[]]$Bytes
)
if($Bytes){$base = $Bytes}
else{$base = [System.Text.Encoding]::UTF8.GetBytes($text)}
$base64Url = [System.Convert]::ToBase64String($base)
$base64Url = $base64Url.Split('=')[0]
$base64Url = $base64Url.Replace('+', '-')
$base64Url = $base64Url.Replace('/', '_')
$base64Url
}
function ConvertTo-OrderedDictionary {
<#
.SYNOPSIS
Converts a hashtable or array to an ordered dictionary.
.DESCRIPTION
Takes in a hashtable or array and then returns an ordered dictionary.
.PARAMETER Object
Object to convert to an ordered dictionary
.NOTES
Name: ConvertTo-OrderedDictionary
Author: Jeff Bolduan
LASTEDIT: 3/11/2016
.EXAMPLE
@{"Item1" = "Value1"; "Item2" = "Value2"; "Item3" = "Value3"; "Item4" = "Value4"} | ConvertTo-OrderedDictionary
Will return the following:
Name Value
---- -----
Item1 Value1
Item2 Value2
Item3 Value3
Item4 Value4
.EXAMPLE
ConvertTo-OrderedDictionary -Object @{"Item1" = "Value1"; "Item2" = "Value2"; "Item3" = "Value3"; "Item4" = "Value4"}
Will return the following:
Name Value
---- -----
Item1 Value1
Item2 Value2
Item3 Value3
Item4 Value4
#>
[CmdletBinding()]
param(
[Parameter(Mandatory=$true, ValueFromPipeline=$true)]
[ValidateNotNullOrEmpty()]
$Object
)
Begin {
$Dictionary = [ordered]@{}
}
Process {
if($Object -is [System.Collections.Hashtable]) {
foreach($Key in ($Object.Keys | sort)) {
$Dictionary.Add($Key, $Object[$Key])
}
} elseif($Object -is [System.Array]) {
for($i = 0; $ -lt $Object.Count; $i++) {
$Dictionary.Add($i, $Object[$i])
}
} else {
throw [System.IO.InvalidDataException]
}
}
End {
return $Dictionary
}
} #END ConvertTo-OrderedDictionary
<#
.Synopsis
zip up module for DSC pull Server - can not use 7zip
.DESCRIPTION
zip up module for DSC pull Server - can not use 7zip
.EXAMPLE
CreateZipFromPSModulePath -ListModuleNames cChoco -Destination $dest
#>
function CreateZipFromPSModulePath
{
param(
[Parameter(Mandatory)]
[string[]]$ListModuleNames,
[Parameter(Mandatory)]
[string]$Destination
)
foreach ($module in $ListModuleNames)
{
$allVersions = Get-Module -Name $module -ListAvailable -Verbose
# Package all versions of the module
foreach ($moduleVersion in $allVersions)
{
$name = $moduleVersion.Name
$source = "$Destination\$name"
# Create package zip
$path = $moduleVersion.ModuleBase
$version = $moduleVersion.Version.ToString()
Compress-Archive -Path "$path\*" -DestinationPath "$source.zip" -Verbose -Force
$newName = "$Destination\$name" + "_" + "$version" + ".zip"
# Rename the module folder to contain the version info.
if (Test-Path $newName)
{
Remove-Item $newName -Recurse -Force
}
Rename-Item -Path "$source.zip" -NewName $newName -Force
}
}
}
function Get-ARP {
<#
.SYNOPSIS
This function is designed to return all ARP entries
.DESCRIPTION
This function returns an object containing all arp entries and details for each sub item property. On 64-bit
powershell sessions there's dynamic paramters to specify the the 32-bit registry or 64-bit registry only
.NOTES
Name: Get-ARP
Author: Aaron Miller
LASTEDIT: 05/08/2013
.EXAMPLE
$ARP = Get-ARP
This returns all arp entries into a variable for processing later.
#>
[CmdletBinding(DefaultParameterSetName='none')]
Param ()
DynamicParam {
if ([IntPtr]::size -eq 8) {
$att1 = new-object -Type System.Management.Automation.ParameterAttribute -Property @{ParameterSetName="x64ARP"}
$attC1 = new-object -Type System.Collections.ObjectModel.Collection[System.Attribute]
$attC1.Add($att1)
$dynParam1 = new-object -Type System.Management.Automation.RuntimeDefinedParameter("x64ARP", [switch], $attC1)
$att2 = new-object -Type System.Management.Automation.ParameterAttribute -Property @{ParameterSetName="x86ARP"}
$attC2 = new-object -Type System.Collections.ObjectModel.Collection[System.Attribute]
$attC2.Add($att2)
$dynParam2 = new-object -Type System.Management.Automation.RuntimeDefinedParameter("x86ARP", [switch], $attC2)
$paramDictionary = new-object -Type System.Management.Automation.RuntimeDefinedParameterDictionary
$paramDictionary.Add("x64ARP", $dynParam1)
$paramDictionary.Add("x86ARP", $dynParam2)
return $paramDictionary
}
}
Begin {
$Primary = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"
$Wow = "HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall"
$toProcess = @()
switch ($PsCmdlet.ParameterSetName) {
"x64ARP" {$toProcess+=$Primary}
"x86ARP" {$toProcess+=$Wow}
default {$toProcess+=$Primary;if ([IntPtr]::size -eq 8) {$toProcess+=$Wow}}
}
}
End {Return [array]($toProcess | ForEach-Object {Get-ChildItem $_} | ForEach-Object {Get-ItemProperty $_.pspath})}
} #END Get-ARP
function Get-ExceptionsList {
<#
.SYNOPSIS
Get's all exceptions available on current machine.
.DESCRIPTION
Goes through all the assemblies on the current computer and gets every exception then outputs them to the console.
.NOTES
Name: Get-ExceptionsList
Author: Jeff Bolduan
LASTEDIT: 3/11/2016
#>
[CmdletBinding()]
param()
# Get all current assemblies
$CurrentDomainAssemblies = [appdomain]::CurrentDomain.GetAssemblies()
# Loop through assemblies and output any members which contain exception in the name
foreach($Assembly in $CurrentDomainAssemblies) {
try {
$Assembly.GetExportedTypes() | Where-Object {
$_.Fullname -match 'Exception'
}
} catch {
}
}
} #END Get-ExceptionsList
function Get-RandomString {
<#
.SYNOPSIS
Returns a random string of a given length.
.DESCRIPTION
Takes in a minimum and maximum lenth and then builds a string of that size.
.PARAMETER LengthMin
Integer for the minimum length of the string
.PARAMETER LengthMax
Integer for the maximum length of the string
.NOTES
Name: Get-RandomString
Author: Jeff Bolduan
LASTEDIT: 3/11/2016
.EXAMPLE
Get-RandomString -LengthMin 5 -LengthMax 10
Will return a random string composed of [a-z][A-Z][0-9] and dash, underscore and period. It's length will be between 5 and 10.
#>
[CmdletBinding()]
param(
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[string]$LengthMin,
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[string]$LengthMax,
[Parameter(Mandatory=$false)]
[ValidateNotNullOrEmpty()]
[string]$ValidCharacters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_."
)
$PossibleCharacters = $ValidCharacters.ToCharArray()
$Result = ""
if($LengthMin -eq $LengthMax) {
$Length = $LengthMin
} else {
$Length = Get-Random -Minimum $LengthMin -Maximum $LengthMax
}
#Write-Verbose -Message "Length: $Length"
for($i = 0; $i -lt $Length; $i++) {
$Result += $PossibleCharacters | Get-Random
}
return $Result
} #END Get-RandomString
#region Get-UsersIDM
function Get-UsersIDM
{
<#
.Synopsis
Fetch list of users from IDM
.DESCRIPTION
Fetch list of users from IDM
.PARAMETER ldapServer
Name of LDAP server to connect to
.PARAMETER ldapSearchString
LDAP query to execute
.PARAMETER searchDN
DN to execute search against
.PARAMETER TimeoutMinutes
Timeout for the query, in minutes, defaults to 30. If the query exceeds this time it will throw an exception
.EXAMPLE
$users = Get-UsersIDM -ldapCredential $ldapCredential -ldapServer $ldapServer -ldapSearchString "(Role=*.cur*)" -TimeoutMinutes 60
.EXAMPLE
$users = Get-UsersIDM -ldapCredential $ldapCredential -ldapServer $ldapServer -ldapSearchString "(&(Role=*.staff.*)(cn=mrEd))"
#>
[CmdletBinding()]
Param
(
[System.Net.NetworkCredential]$ldapCredential,
[Parameter(Mandatory)]
[string]$ldapServer,
[Parameter(Mandatory)]
[string]$ldapSearchString,
[string]$searchDN,
[int]$TimeoutMinutes = 30
)
#Load the assemblies needed for ldap lookups
$null = [System.Reflection.Assembly]::LoadWithPartialName("System.DirectoryServices.Protocols")
$null = [System.Reflection.Assembly]::LoadWithPartialName("System.Net")
#setup the ldap connection
$ldapConnection = New-Object System.DirectoryServices.Protocols.LdapConnection((New-Object System.DirectoryServices.Protocols.LdapDirectoryIdentifier($ldapServer,636)), $ldapCredential)
$ldapConnection.AuthType = [System.DirectoryServices.Protocols.AuthType]::Basic
$ldapConnection.SessionOptions.ProtocolVersion = 3
#cert validation fails, so this will never validate the cert and just connect things
$ldapConnection.SessionOptions.VerifyServerCertificate = { return $true; }
$ldapConnection.SessionOptions.SecureSocketLayer = $true
$ldapConnection.Bind()
#build the ldap query
$ldapSearch = New-Object System.DirectoryServices.Protocols.SearchRequest
$ldapSearch.Filter = $ldapSearchString
$ldapSearch.Scope = "Subtree"
$ldapSearch.DistinguishedName = $searchDN
#execute query for Students...default 30 minute timeout...generally takes about 12 minutes
$ldapResponse = $ldapConnection.SendRequest($ldapSearch, (New-Object System.TimeSpan(0,$TimeoutMinutes,0))) -as [System.DirectoryServices.Protocols.SearchResponse]
$null = $ldapConnection.Dispose()
return ($ldapResponse)
}
#endregion
#region Get-WebReqErrorDetails
function Get-WebReqErrorDetails {
<#
.SYNOPSIS
Returns JSON Responsbody data from an Error thrown by Invoke-Webrequest or Invoke-RestMethod
.DESCRIPTION
Returns JSON Responsbody data from an Error thrown by Invoke-Webrequest or Invoke-RestMethod
.PARAMETER err
Error thrown by Invoke-Webrequest or Invoke-RestMethod
.NOTES
Author: Travis Sobeck
#>
[CmdletBinding()]
param
(
[Parameter(Mandatory)]
[System.Management.Automation.ErrorRecord]$err
)
$reader = New-Object System.IO.StreamReader($err.Exception.Response.GetResponseStream())
$reader.BaseStream.Position = 0
$reader.DiscardBufferedData()
return ($reader.ReadToEnd() | ConvertFrom-Json)
}
#endregion
function Out-RecursiveHash {
<#
.SYNOPSIS
Outputs a hashtable recursively
.DESCRIPTION
Takes in a hashtable and then writes the values stored within to output.
.PARAMETER hash
Hashtable to be outputted recursively
.NOTES
Name: Out-RecursiveHash
Author: Jeff Bolduan
LASTEDIT: 3/11/2016
.EXAMPLE
$hashtable = @{ "Item1" = @{ "SubItem1" = "Value" }; "Item2" = "Value2" }
Out-RecursiveHash -Hash $hashtable
This will output:
SubItem1 : Value
Item2 : Value2
#>
[CmdletBinding()]
param(
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[hashtable]$Hash
)
$Return = ""
# Loop through each of the hashtable keys and output the key pair unless it's a hashtable then recursive call
foreach($key in $hash.keys) {
if($hash[$key] -is [HashTable]) {
$Return += (Out-RecursiveHash $hash[$key])
} else {
$Return += "$key : $($hash[$key])`n"
}
}
return $Return
} #END Out-RecursiveHash
#region Send-SplunkHEC
function Send-SplunkHEC
{
<#
.Synopsis
Send event to Splunk HTTP Event Collector
.DESCRIPTION
Send event to Splunk HTTP Event Collector
.PARAMETER uri
URI for HEC endpoint
.PARAMETER header
contains auth token
.PARAMETER host
Part of Splunk Metadata for event. Device data being sent from
.PARAMETER source
Part of Splunk Metadata for event. Source
.PARAMETER sourceType
Part of Splunk Metadata for event. SourceType
.PARAMETER Retries
Set how many retries will be attempted if invoking fails
.PARAMETER SecondsDelay
Set how many seconds to wait between retries
.PARAMETER metadata
Part of Splunk Metadata for event. Combination of host,source,sourcetype in performatted hashtable, will be comverted to JSON
.PARAMETER eventData
Event Data in hastable or pscustomeobject, will be comverted to JSON
.PARAMETER JsonDepth
Optional, specifies the Depth parameter to pass to ConvertTo-JSON, defaults to 100
#>
[CmdletBinding()]
Param
(
# Param1 help description
[Parameter(Mandatory)]
[string]$uri,
[Parameter(Mandatory)]
[Collections.Hashtable]$header,
[Parameter(Mandatory,ParameterSetName='Components')]
[String]$source,
[Parameter(Mandatory,ParameterSetName='Components')]
[String]$sourceType,
[Alias("Host")]
[Parameter(Mandatory,ParameterSetName='Components')]
[String]$EventHost,
[Parameter(Mandatory,ParameterSetName='hashtable')]
[Collections.Hashtable]$metadata,
# This can be [Management.Automation.PSCustomObject] or [Collections.Hashtable]
[Parameter(Mandatory)]
$eventData,
[int]$Retries = 5,
[int]$SecondsDelay = 10,
[int]$JsonDepth = 100
)
Begin{
$retryCount = 0
$completed = $false
$response = $null
}
Process
{
if ($metadata){$bodySplunk = $metadata.Clone()}
else {$bodySplunk = @{'host' = $EventHost;'source' = $source;'sourcetype' = $sourcetype}}
#Splunk takes time in Unix Epoch format, so first get the current date,
#convert it to UTC (what Epoch is based on) then format it to seconds since January 1 1970.
#Without converting it to UTC the date would be offset by a number of hours equal to your timezone's offset from UTC
$bodySplunk['time'] = (Get-Date).toUniversalTime() | Get-Date -UFormat %s
$internalEventData = $eventData | ConvertTo-Json | ConvertFrom-Json
Add-Member -InputObject $internalEventData -Name "SplunkHECRetry" -Value $retryCount -MemberType NoteProperty
$bodySplunk['event'] = $internalEventData
while (-not $completed) {
try {
$response = Invoke-RestMethod -Uri $uri -Headers $header -UseBasicParsing -Body ($bodySplunk | ConvertTo-Json -Depth $JsonDepth) -Method Post
if ($response.text -ne 'Success' -or $response.code -ne 0){throw "Failed to submit to Splunk HEC $($response)"}
$completed = $true
}
catch {
if ($retrycount -ge $Retries) {
throw
}
else {
Start-Sleep $SecondsDelay
$retrycount++
$bodySplunk.event.SplunkHECRetry = $retryCount
}
}
}
}
End{return $true}
}
#endregion
#region Get-CurrentEpochTime
function Get-CurrentEpochTime
{
<#
.Synopsis
Get current Epoch Time (seconds from 00:00:00 1 January 1970 UTC) as string with 1/100,000 of a second precision
.DESCRIPTION
Get current Epoch Time (seconds from 00:00:00 1 January 1970 UTC) as string with 1/100,000 of a second precision
#>
[OutputType([string])]
[CmdletBinding()]
Param
(
)
Begin{}
Process
{
(Get-Date).toUniversalTime() | Get-Date -UFormat %s
}
End{}
}
#endregion
function Set-ModuleLatestVersion
{
<#
.Synopsis
installs latest version of a module and deletes the old one
.DESCRIPTION
The problem with Update-module is it leave the old one behind, this cleans that up
.EXAMPLE
Set-ModuleLatestVersion -module xPSDesiredStateConfiguration
#>
[CmdletBinding()]
Param
(
# Param1 help description
[Parameter(Mandatory=$true,
ValueFromPipelineByPropertyName=$true,
Position=0)]
[string]$module
)
Begin
{
}
Process
{
$currentMod = get-module -ListAvailable $module
if ($currentMod.count -gt 1){throw "Multiple version of module installed, clear out old $($currentMod.Version)"}
$currentVersion = $currentMod.Version.ToString()
Update-Module $module -Force
if((get-module -ListAvailable $module).count -gt 1){Uninstall-Module -Name $module -RequiredVersion $currentVersion;get-module -ListAvailable $module}
else {Write-Warning "Current version was latest version"}
}
End
{
}
}
function Test-RegistryValue {
<#
.SYNOPSIS
This function takes in a registry path, a name and then determines whether the registry value exists.
.NOTES
Name: Test-RegistryValue
Author: Jeff Bolduan
LASTEDIT: 09/01/2016
.EXAMPLE
Test-RegistryValue -Path HKLM:\Foo\Bar -Value FooBar
#>
[CmdletBinding()]
param(
[Parameter(Mandatory=$true)]
[string]$Path,
[Parameter(Mandatory=$true)]
[string]$Name,
[Parameter(Mandatory=$false)]
[switch]$PassThru
)
if(Test-Path -LiteralPath $Path) {
$Key = Get-Item -LiteralPath $Path
if($Key.GetValue($Value, $null) -ne $null) {
if($PassThru) {
Get-ItemProperty -LiteralPath $Path -Name $Name
} else {
$true
}
} else {
$false
}
} else {
return $false
}
} #END Test-RegistryValue
function Write-Log {
<#
.SYNOPSIS
This function is used to pass messages to a ScriptLog. It can also be leveraged for other purposes if more complex logging is required.
.DESCRIPTION
Write-Log function is setup to write to a log file in a format that can easily be read using CMTrace.exe. Variables are setup to adjust the output.
.PARAMETER Message
The message you want to pass to the log.
.PARAMETER Path
The full path to the script log that you want to write to.
.PARAMETER Severity
Manual indicator (highlighting) that the message being written to the log is of concern. 1 - No Concern (Default), 2 - Warning (yellow), 3 - Error (red).
.PARAMETER Component
Provide a non null string to explain what is being worked on.
.PARAMETER Context
Provide a non null string to explain why.
.PARAMETER Thread
Provide a optional thread number.
.PARAMETER Source
What was the root cause or action.
.PARAMETER Console
Adjusts whether output is also directed to the console window.
.NOTES
Name: Write-Log
Author: Aaron Miller
LASTEDIT: 01/23/2013 10:09:00
.EXAMPLE
Write-Log -Message $exceptionMsg -Path $ScriptLog -Severity 3
Writes the content of $exceptionMsg to the file at $ScriptLog and marks it as an error highlighted in red
#>
PARAM(
[Parameter(Mandatory=$true)][string]$Message,
[Parameter(Mandatory=$false)][string]$Path = "$env:TEMP\CMTrace.Log",
[Parameter(Mandatory=$false)][int]$Severity = 1,
[Parameter(Mandatory=$false)][string]$Component = " ",
[Parameter(Mandatory=$false)][string]$Context = " ",
[Parameter(Mandatory=$false)][string]$Thread = "1",
[Parameter(Mandatory=$false)][string]$Source = "",
[Parameter(Mandatory=$false)][switch]$Console
)
# Setup the log message
$time = Get-Date -Format "HH:mm:ss.fff"
$date = Get-Date -Format "MM-dd-yyyy"
$LogMsg = '<![LOG['+$Message+']LOG]!><time="'+$time+'+000" date="'+$date+'" component="'+$Component+'" context="'+$Context+'" type="'+$Severity+'" thread="'+$Thread+'" file="'+$Source+'">'
# Write out the log file using the ComObject Scripting.FilesystemObject
$ForAppending = 8
$oFSO = New-Object -ComObject scripting.filesystemobject
$oFile = $oFSO.OpenTextFile($Path, $ForAppending, $true)
$oFile.WriteLine($LogMsg)
$oFile.Close()
Remove-Variable oFSO
Remove-Variable oFile
# Write to the console if $Console is set to True
if ($Console -eq $true) {Write-Host $Message}
} #END Write-Log
function Get-RandomCharacter{
<#
.SYNOPSIS
This function is to randomize a list of characters for password generation.
.PARAMETER length
How many of each character set you want.
.PARAMETER characters
A set list of characters to be selected from.
.EXAMPLE
Get-RandomCharacters -length 2 -characters '1234567890'
42
.NOTES
Taken from all over the web. Notable = https://activedirectoryfaq.com/2017/08/creating-individual-random-passwords/
#>
param(
[Parameter(Mandatory=$true)]$length,
[Parameter(Mandatory=$true)]$characters
)
Begin{}
Process{
$random = 1..$length | ForEach-Object {Get-Random -Maximum $characters.length}
}
End{
return $characters[$random] -join ''
}
}
function Scramble-String{
<#
.SYNOPSIS
This function takes a list of characters and randomizes them.
.PARAMETER inputString
A string of characters to scramble.
.EXAMPLE
Scramble-String -inputString '12345'
51432
.NOTES
Taken from all over the web. Notable = https://activedirectoryfaq.com/2017/08/creating-individual-random-passwords/
#>
param(
[Parameter(Mandatory=$true)]
[string]$inputString
)
Begin{$characterArray = $inputString.ToCharArray()}
Process{
$scrambledStringArray = $characterArray | Get-Random -Count $characterArray.Length
$outputString = $scrambledStringArray -join ''
}
End{return $outputString}
}
function New-Password{
<#
.SYNOPSIS
This function just generates a password based on predetermined character list.
.PARAMETER passwordLength
The length of the password
.PARAMETER special
Boolean for ignoring special characters. Default is True
.EXAMPLE
New-Password -passwordLength 15
42
.NOTES
Taken from all over the web. Notable = https://activedirectoryfaq.com/2017/08/creating-individual-random-passwords/
#>
param(
[int]$passwordLength = 40,
[boolean]$special = $True
)
Begin{
[int]$length = [Math]::Truncate($passwordLength / 4)
switch ($passwordLength % 4) {
0 { $lengths = @($length, $length, $length, $length) }
1 { $lengths = @($length, $length, $length, ($length + 1)) }
2 { $lengths = @($length, $length, ($length + 1), ($length + 1)) }
3 { $lengths = @($length, ($length + 1), ($length + 1), ($length + 1)) }
}
}
Process{
$password = Get-RandomCharacter -length $lengths[0] -characters 'abcdefghiklmnoprstuvwxyz'
$password += Get-RandomCharacter -length $lengths[1] -characters 'ABCDEFGHKLMNOPRSTUVWXYZ'
$password += Get-RandomCharacter -length $lengths[2] -characters '1234567890'
If($special -eq $True){
$password += Get-RandomCharacter -length $lengths[3] -characters '!$%&()=}][{#+'
}
Else{
$password += Get-RandomCharacter -length $lengths[3] -characters '150AHKbrp2z3'
}
$password = Scramble-String $password
}
End{return $password}
}