forked from rgel/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMS-Module.psm1
More file actions
1087 lines (1001 loc) · 34.2 KB
/
Copy pathMS-Module.psm1
File metadata and controls
1087 lines (1001 loc) · 34.2 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
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
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
Filter Get-OUPath {
<#
.SYNOPSIS
Convert AD object's 'DistinguishedName' property to path-like format.
.DESCRIPTION
This filter convert Active Directory object's 'DistinguishedName' property to path-like format.
Active Directory hierarchy view like this: 'Domainname\TopLevelOU\North\HR' or without domain name 'TopLevelOU\North\HR'.
.PARAMETER IncludeDomainName
If ommited doesn't include Domain Name to the path.
Useful in multi domain forests.
.PARAMETER ExcludeObjectName
If ommited include object name in the path.
.PARAMETER UCaseDomainName
Convert Domain Name to UPPERCASE, otherwise only the capital letter is uppercased.
contoso -> CONTOSO -> Contoso.
Does nothing if 'IncludeDomainName' ommited.
.EXAMPLE
PS C:\> Get-ADUser user1 |Get-OUPath
.EXAMPLE
PS C:\> Get-ADUser -Filter {SamAccountName -like 'user*'} |select Name,@{N='OUPath';E={$_.DistinguishedName |Get-OUPath}}
Add calculated property 'OUPath' to existing objects.
This technique will work with all types of objects (users/computers/groups/OU etc).
.EXAMPLE
PS C:\> Get-ADGroup -Filter {SamAccountName -like 'hr*'} |select Name,@{N='OUPath';E={$_.DistinguishedName |Get-OUPath -IncludeDomainName}} |ft -au
.EXAMPLE
PS C:\> Get-ADGroupMember HR |select Name,@{N='OUPath';E={$_.DistinguishedName |Get-OUPath}} |sort OUPath,Name |ft -au
.EXAMPLE
PS C:\> Get-ADOrganizationalUnit -Filter {Name -like 'North*'} |select @{N='DN';E={$_}},@{N='OUPath';E={$_ |Get-OUPath -IncludeDomainName}} |sort DN
.EXAMPLE
PS C:\> $DNs = @()
PS C:\> $DNs += 'CN=User1,OU=HR,OU=Northwest,OU=North,DC=contoso,DC=co,DC=il'
PS C:\> $DNs += 'CN=User2,CN=Users,DC=contoso,DC=co,DC=il'
PS C:\> $DNs += 'CN=Server1,CN=Computers,DC=contoso,DC=co,DC=il'
PS C:\> $DNs += 'OU=Northwest,OU=north,DC=contoso,DC=co,DC=il'
PS C:\> $DNs += 'OU=TopLevelOU,DC=contoso,DC=co,DC=il'
PS C:\> $DNs |select @{N='DN';E={$_}},@{N='OUPath';E={$_ |Get-OUPath -IncludeDomainName}}
These DNs for the different AD object types: User, User in the default 'Users' container, Computer, OU and top level OU.
.EXAMPLE
PS C:\> Get-ADDomainController -Filter * |Get-OUPath -IncludeDomainName
.INPUTS
[Microsoft.ActiveDirectory.Management.ADUser[]] Active Directory user objects, returned by Get-ADUser cmdlet.
[Microsoft.ActiveDirectory.Management.ADGroup[]] Active Directory group objects, returned by Get-ADGroup cmdlet.
[Microsoft.ActiveDirectory.Management.ADPrincipal[]] Active Directory objects, returned by Get-ADGroupMember cmdlet.
[Microsoft.ActiveDirectory.Management.ADComputer[]] Active Directory computer objects, returned by Get-ADComputer cmdlet.
[Microsoft.ActiveDirectory.Management.ADDomainController[]] Active Directory DC objects, returned by Get-ADDomainController cmdlet.
[Microsoft.ActiveDirectory.Management.ADObject[]] Active Directory objects, returned by Get-ADObject cmdlet.
[Microsoft.ActiveDirectory.Management.ADOrganizationalUnit[]] Active Directory OU objects, returned by Get-ADOrganizationalUnit cmdlet.
[System.String[]] Strings that represent any object's 'DistinguishedName' property.
Or any object that have 'DistinguishedName' property.
.OUTPUTS
[System.String[]]
If you use '-ExcludeObjectName' switch without '-IncludeDomainName'
both the object itself and a domain name are not included in the returned string
and you will get EMPTY path for TOP LEVEL OU containers.
.NOTES
Author :: Roman Gelman @rgelman75
Version 1.0 :: 18-May-2016 :: [Release] :: This function was fully rewrited from the original 'Get-OUTree'
.LINK
https://ps1code.com/category/powershell/ms-module/
#>
Param ([switch]$IncludeDomainName,
[switch]$ExcludeObjectName,
[switch]$UCaseDomainName)
If ($_.GetType().Name -eq 'string') { $DN = $_ }
ElseIf ($_.GetType().Name -eq 'ADDomainController') { $DN = $_.ComputerObjectDN }
Else { $DN = $_.DistinguishedName }
If ($IncludeDomainName)
{
If ($ExcludeObjectName)
{
### Top level OU ###
If (($DN -split ',')[1].ToLower().StartsWith('dc=')) { $rgxDN2OU = '(?i)^(cn|ou)=.+?,(?<OUPath>dc=.+?),' }
### Non top level OU ###
Else { $rgxDN2OU = '(?i)^(cn|ou)=.+?,(?<OUPath>(ou=.+?|cn=.+?),dc=.+?),' }
}
Else
{
$rgxDN2OU = '(?i)^(?<OUPath>(ou=.+?|cn=.+?),dc=.+?),'
}
}
Else
{
If ($ExcludeObjectName) { $rgxDN2OU = '(?i)^(cn|ou)=.+?,(?<OUPath>ou=.+?|cn=.+?),dc=' }
Else { $rgxDN2OU = '(?i)^(?<OUPath>ou=.+?|cn=.+?),dc=' }
}
Try
{
$arrOU = [regex]::Match($DN, $rgxDN2OU).Groups['OUPath'].Value -replace ('ou=|cn=|dc=', $null) -split (',')
[array]::Reverse($arrOU)
If ($IncludeDomainName)
{
If ($UCaseDomainName) { $Domain = $arrOU[0].ToUpper() }
Else { $Domain = (Get-Culture).TextInfo.ToTitleCase($arrOU[0]) }
If ($arrOU.Length -gt 1) { return $Domain + '\' + ($arrOU[1 .. ($arrOU.Length - 1)] -join ('\')) }
Else { return $Domain }
}
Else { return $arrOU -join ('\') }
}
Catch
{ return $null }
} #EndFilter Get-OUPath
Function Write-Menu
{
<#
.SYNOPSIS
Display custom menu in the PowerShell console.
.DESCRIPTION
The Write-Menu cmdlet creates numbered and colored menues
in the PS console window and returns the choiced entry.
.PARAMETER Menu
Menu entries.
.PARAMETER PropertyToShow
If your menu entries are objects and not the strings
this is property to show as entry.
.PARAMETER Prompt
User prompt at the end of the menu.
.PARAMETER Header
Menu title (optional).
.PARAMETER Shift
Quantity of <TAB> keys to shift the menu items right.
.PARAMETER TextColor
Menu text color.
.PARAMETER HeaderColor
Menu title color.
.PARAMETER AddExit
Add 'Exit' as very last entry.
.EXAMPLE
PS C:\> Write-Menu -Menu "Open","Close","Save" -AddExit -Shift 1
Simple manual menu with 'Exit' entry and 'one-tab' shift.
.EXAMPLE
PS C:\> Write-Menu -Menu (Get-ChildItem 'C:\Windows\') -Header "`t`t-- File list --`n" -Prompt 'Select any file'
Folder content dynamic menu with the header and custom prompt.
.EXAMPLE
PS C:\> Write-Menu -Menu (Get-Service) -Header ":: Services list ::`n" -Prompt 'Select any service' -PropertyToShow DisplayName
Display local services menu with custom property 'DisplayName'.
.EXAMPLE
PS C:\> Write-Menu -Menu (Get-Process |select *) -PropertyToShow ProcessName |fl
Display full info about choicen process.
.INPUTS
Any type of data (object(s), string(s), number(s), etc).
.OUTPUTS
[The same type as input object] Single menu item.
.NOTES
Author :: Roman Gelman @rgelman75
Version 1.0 :: 21-Apr-2016 :: [Release] :: Publicly available
Version 1.1 :: 03-Nov-2016 :: [Change] :: Supports a single item as menu entry
Version 1.2 :: 22-Jun-2017 :: [Change] :: Throws an error if property, specified by -PropertyToShow does not exist. Code optimization
Version 1.3 :: 27-Sep-2017 :: [Bugfix] :: Fixed throwing an error while menu entries are numeric values
.LINK
https://ps1code.com/2016/04/21/write-menu-powershell
#>
[CmdletBinding()]
[Alias("menu")]
Param (
[Parameter(Mandatory, Position = 0)]
[Alias("MenuEntry", "List")]
$Menu
,
[Parameter(Mandatory = $false, Position = 1)]
[string]$PropertyToShow = 'Name'
,
[Parameter(Mandatory = $false, Position = 2)]
[ValidateNotNullorEmpty()]
[string]$Prompt = 'Pick a choice'
,
[Parameter(Mandatory = $false, Position = 3)]
[Alias("Title")]
[string]$Header = ''
,
[Parameter(Mandatory = $false, Position = 4)]
[ValidateRange(0, 5)]
[Alias("Tab", "MenuShift")]
[int]$Shift = 0
,
[Parameter(Mandatory = $false, Position = 5)]
[Alias("Color", "MenuColor")]
[System.ConsoleColor]$TextColor = 'White'
,
[Parameter(Mandatory = $false, Position = 6)]
[System.ConsoleColor]$HeaderColor = 'Yellow'
,
[Parameter(Mandatory = $false)]
[ValidateNotNullorEmpty()]
[Alias("Exit", "AllowExit")]
[switch]$AddExit
)
Begin
{
$ErrorActionPreference = 'Stop'
if ($Menu -isnot [array]) { $Menu = @($Menu) }
if ($Menu[0] -is [psobject] -and $Menu[0] -isnot [string])
{
if (!($Menu | Get-Member -MemberType Property, NoteProperty -Name $PropertyToShow)) { Throw "Property [$PropertyToShow] does not exist" }
}
$MaxLength = if ($AddExit) { 8 }
else { 9 }
$AddZero = if ($Menu.Length -gt $MaxLength) { $true }
else { $false }
[hashtable]$htMenu = @{ }
}
Process
{
### Write menu header ###
if ($Header -ne '') { Write-Host $Header -ForegroundColor $HeaderColor }
### Create shift prefix ###
if ($Shift -gt 0) { $Prefix = [string]"`t" * $Shift }
### Build menu hash table ###
for ($i = 1; $i -le $Menu.Length; $i++)
{
$Key = if ($AddZero)
{
$lz = if ($AddExit) { ([string]($Menu.Length + 1)).Length - ([string]$i).Length }
else { ([string]$Menu.Length).Length - ([string]$i).Length }
"0" * $lz + "$i"
}
else
{
"$i"
}
$htMenu.Add($Key, $Menu[$i - 1])
if ($Menu[$i] -isnot 'string' -and ($Menu[$i - 1].$PropertyToShow))
{
Write-Host "$Prefix[$Key] $($Menu[$i - 1].$PropertyToShow)" -ForegroundColor $TextColor
}
else
{
Write-Host "$Prefix[$Key] $($Menu[$i - 1])" -ForegroundColor $TextColor
}
}
### Add 'Exit' row ###
if ($AddExit)
{
[string]$Key = $Menu.Length + 1
$htMenu.Add($Key, "Exit")
Write-Host "$Prefix[$Key] Exit" -ForegroundColor $TextColor
}
### Pick a choice ###
Do
{
$Choice = Read-Host -Prompt $Prompt
$KeyChoice = if ($AddZero)
{
$lz = if ($AddExit) { ([string]($Menu.Length + 1)).Length - $Choice.Length }
else { ([string]$Menu.Length).Length - $Choice.Length }
if ($lz -gt 0) { "0" * $lz + "$Choice" }
else { $Choice }
}
else
{
$Choice
}
}
Until ($htMenu.ContainsKey($KeyChoice))
}
End
{
return $htMenu.get_Item($KeyChoice)
}
} #EndFunction Write-Menu
Function New-PercentageBar
{
<#
.SYNOPSIS
Create percentage bar.
.DESCRIPTION
This cmdlet creates percentage bar.
.PARAMETER Percent
Value in percents (%).
.PARAMETER Value
Value in arbitrary units.
.PARAMETER MaxValue
100% value.
.PARAMETER BarLength
Bar length in chars.
.PARAMETER BarView
Different char sets to build the bar.
.PARAMETER GreenBorder
Percent value to change bar color from green to yellow (relevant with -DrawBar parameter only).
.PARAMETER YellowBorder
Percent value to change bar color from yellow to red (relevant with -DrawBar parameter only).
.PARAMETER NoPercent
Exclude percentage number from the bar.
.PARAMETER DrawBar
Directly draw the colored bar onto the PowerShell console (unsuitable for calculated properties).
.EXAMPLE
PS C:\> New-PercentageBar -Percent 90 -DrawBar
Draw single bar with all default settings.
.EXAMPLE
PS C:\> New-PercentageBar -Percent 95 -DrawBar -GreenBorder 70 -YellowBorder 90
Draw the bar and move the both color change borders.
.EXAMPLE
PS C:\> 85 |New-PercentageBar -DrawBar -NoPercent
Pipeline the percent value to the function and exclude percent number from the bar.
.EXAMPLE
PS C:\> For ($i=0; $i -le 100; $i+=10) {New-PercentageBar -Percent $i -DrawBar -Length 100 -BarView AdvancedThin2; "`r"}
Demonstrates advanced bar view with custom bar length and different percent values.
.EXAMPLE
PS C:\> $Folder = 'C:\reports\'
PS C:\> $FolderSize = (Get-ChildItem -Path $Folder |measure -Property Length -Sum).Sum
PS C:\> Get-ChildItem -Path $Folder -File |sort Length -Descending |select -First 10 |select Name,Length,@{N='SizeBar';E={New-PercentageBar -Value $_.Length -MaxValue $FolderSize}} |ft -au
Get file size report and add calculated property 'SizeBar' that contains the percent of each file size from the folder size.
.EXAMPLE
PS C:\> $VolumeC = gwmi Win32_LogicalDisk |? {$_.DeviceID -eq 'c:'}
PS C:\> Write-Host -NoNewline "Volume C Usage:" -ForegroundColor Yellow; `
PS C:\> New-PercentageBar -Value ($VolumeC.Size-$VolumeC.Freespace) -MaxValue $VolumeC.Size -DrawBar; "`r"
Get system volume usage report.
.NOTES
Author :: Roman Gelman @rgelman75
Version 1.0 :: 04-Jul-2016 :: [Release] :: Publicly available
.LINK
https://ps1code.com/2016/07/16/percentage-bar-powershell
#>
[CmdletBinding(DefaultParameterSetName = 'PERCENT')]
Param (
[Parameter(Mandatory, Position = 1, ValueFromPipeline, ParameterSetName = 'PERCENT')]
[ValidateRange(0, 100)]
[int]$Percent
,
[Parameter(Mandatory, Position = 1, ValueFromPipeline, ParameterSetName = 'VALUE')]
[ValidateRange(0, [double]::MaxValue)]
[double]$Value
,
[Parameter(Mandatory, Position = 2, ParameterSetName = 'VALUE')]
[ValidateRange(1, [double]::MaxValue)]
[double]$MaxValue
,
[Parameter(Mandatory = $false, Position = 3)]
[Alias("BarSize", "Length")]
[ValidateRange(10, 100)]
[int]$BarLength = 20
,
[Parameter(Mandatory = $false, Position = 4)]
[ValidateSet("SimpleThin", "SimpleThick1", "SimpleThick2", "AdvancedThin1", "AdvancedThin2", "AdvancedThick")]
[string]$BarView = "SimpleThin"
,
[Parameter(Mandatory = $false, Position = 5)]
[ValidateRange(50, 80)]
[int]$GreenBorder = 60
,
[Parameter(Mandatory = $false, Position = 6)]
[ValidateRange(80, 90)]
[int]$YellowBorder = 80
,
[Parameter(Mandatory = $false)]
[switch]$NoPercent
,
[Parameter(Mandatory = $false)]
[switch]$DrawBar
)
Begin
{
If ($PSBoundParameters.ContainsKey('VALUE'))
{
If ($Value -gt $MaxValue)
{
Throw "The [-Value] parameter cannot be greater than [-MaxValue]!"
}
Else
{
$Percent = $Value/$MaxValue * 100 -as [int]
}
}
If ($YellowBorder -le $GreenBorder) { Throw "The [-YellowBorder] value must be greater than [-GreenBorder]!" }
Function Set-BarView ($View)
{
Switch -exact ($View)
{
"SimpleThin" { $GreenChar = [char]9632; $YellowChar = [char]9632; $RedChar = [char]9632; $EmptyChar = "-"; Break }
"SimpleThick1" { $GreenChar = [char]9608; $YellowChar = [char]9608; $RedChar = [char]9608; $EmptyChar = "-"; Break }
"SimpleThick2" { $GreenChar = [char]9612; $YellowChar = [char]9612; $RedChar = [char]9612; $EmptyChar = "-"; Break }
"AdvancedThin1" { $GreenChar = [char]9632; $YellowChar = [char]9632; $RedChar = [char]9632; $EmptyChar = [char]9476; Break }
"AdvancedThin2" { $GreenChar = [char]9642; $YellowChar = [char]9642; $RedChar = [char]9642; $EmptyChar = [char]9643; Break }
"AdvancedThick" { $GreenChar = [char]9617; $YellowChar = [char]9618; $RedChar = [char]9619; $EmptyChar = [char]9482; Break }
}
$Properties = [ordered]@{
Char1 = $GreenChar
Char2 = $YellowChar
Char3 = $RedChar
Char4 = $EmptyChar
}
$Object = New-Object PSObject -Property $Properties
$Object
} #End Function Set-BarView
$BarChars = Set-BarView -View $BarView
$Bar = $null
Function Draw-Bar
{
Param (
[Parameter(Mandatory)]
[string]$Char
,
[Parameter(Mandatory = $false)]
[string]$Color = 'White'
,
[Parameter(Mandatory = $false)]
[boolean]$Draw
)
If ($Draw)
{
Write-Host -NoNewline -ForegroundColor ([System.ConsoleColor]$Color) $Char
}
Else
{
return $Char
}
} #End Function Draw-Bar
} #End Begin
Process
{
If ($NoPercent)
{
$Bar += Draw-Bar -Char "[ " -Draw $DrawBar
}
Else
{
If ($Percent -eq 100) { $Bar += Draw-Bar -Char "$Percent% [ " -Draw $DrawBar }
ElseIf ($Percent -ge 10) { $Bar += Draw-Bar -Char " $Percent% [ " -Draw $DrawBar }
Else { $Bar += Draw-Bar -Char " $Percent% [ " -Draw $DrawBar }
}
For ($i = 1; $i -le ($BarValue = ([Math]::Round($Percent * $BarLength / 100))); $i++)
{
If ($i -le ($GreenBorder * $BarLength / 100)) { $Bar += Draw-Bar -Char ($BarChars.Char1) -Color 'DarkGreen' -Draw $DrawBar }
ElseIf ($i -le ($YellowBorder * $BarLength / 100)) { $Bar += Draw-Bar -Char ($BarChars.Char2) -Color 'Yellow' -Draw $DrawBar }
Else { $Bar += Draw-Bar -Char ($BarChars.Char3) -Color 'Red' -Draw $DrawBar }
}
For ($i = 1; $i -le ($EmptyValue = $BarLength - $BarValue); $i++) { $Bar += Draw-Bar -Char ($BarChars.Char4) -Draw $DrawBar }
$Bar += Draw-Bar -Char " ]" -Draw $DrawBar
} #End Process
End
{
If (!$DrawBar) { return $Bar }
} #End End
} #EndFunction New-PercentageBar
Function New-RandomPassword
{
<#
.SYNOPSIS
Generate a random password.
.DESCRIPTION
This cmdlet generates a random password with different complexity levels.
.PARAMETER Letters
Use 'a-z' letters.
.PARAMETER CapitalLetters
Use 'A-Z' letters.
.PARAMETER Digits
Use '0-9' digits.
.PARAMETER SpecialCharacters
Use special characters.
.PARAMETER Complex
Use all possible characters (letters, capital letters, digits and special characters).
.PARAMETER PasswordLength
Password length.
.EXAMPLE
PS C:\> New-RandomPassword -Complex
Generate complex password with default length.
.EXAMPLE
PS C:\> New-RandomPassword -Letters -CapitalLetters -Digits -PasswordLength 8
Generate 8-character password without special characters.
.EXAMPLE
PS C:\> (1..10) |% {New-RandomPassword -Digits}
Generate ten 8-digit numbers.
.OUTPUTS
[System.String] Password.
.NOTES
Author :: Roman Gelman @rgelman75
Version 1.0 :: 01-Jun-2016 :: [Release] :: Publicly available
.LINK
https://github.com/rgel/PowerShell
#>
[CmdletBinding(DefaultParameterSetName = 'Chars')]
Param (
[Parameter(Mandatory = $false, Position = 1, ParameterSetName = 'Chars')]
[switch]$Letters
,
[Parameter(Mandatory = $false, Position = 2, ParameterSetName = 'Chars')]
[switch]$CapitalLetters
,
[Parameter(Mandatory = $false, Position = 3, ParameterSetName = 'Chars')]
[switch]$Digits
,
[Parameter(Mandatory = $false, Position = 4, ParameterSetName = 'Chars')]
[switch]$SpecialCharacters
,
[Parameter(Mandatory, Position = 1, ParameterSetName = 'Complex')]
[switch]$Complex
,
[Parameter(Mandatory = $false, Position = 5)]
[ValidateRange(6, 24)]
[uint16]$PasswordLength = 8
)
Begin
{
$loweraz = -join ((97 .. 122) | %{ [char][byte]$_ })
$UPPERAZ = -join ((65 .. 90) | %{ [char][byte]$_ })
$digit09 = -join ((48 .. 57) | %{ [char][byte]$_ })
$special = -join ((33 .. 47) | %{ [char][byte]$_ }) + (-join ((58 .. 64) | %{ [char][byte]$_ })) + (-join ((91 .. 95) | %{ [char][byte]$_ }))
$CharSet = ''
$i = 0
### How many character groups choicen ###
If ($PSCmdlet.ParameterSetName -eq 'Chars')
{
If ($PSBoundParameters.ContainsKey('CapitalLetters')) { $i++ }
If ($PSBoundParameters.ContainsKey('Letters')) { $i++ }
If ($PSBoundParameters.ContainsKey('SpecialCharacters')) { $i++ }
If ($PSBoundParameters.ContainsKey('Digits')) { $i++ }
}
ElseIf ($PSCmdlet.ParameterSetName -eq 'Complex') { $i = 4 }
If (!$i) { Throw "You have to choice the password complexity!" }
}
Process
{
### How many characters in the each group ###
$CharCount = [math]::Truncate($PasswordLength/$i)
If ($PSCmdlet.ParameterSetName -eq 'Chars')
{
If ($PSBoundParameters.ContainsKey('CapitalLetters')) { $CharSet += -join ($UPPERAZ.ToCharArray() | Get-Random -Count $CharCount) }
If ($PSBoundParameters.ContainsKey('Letters')) { $CharSet += -join ($loweraz.ToCharArray() | Get-Random -Count $CharCount) }
If ($PSBoundParameters.ContainsKey('SpecialCharacters')) { $CharSet += -join ($special.ToCharArray() | Get-Random -Count $CharCount) }
If ($PSBoundParameters.ContainsKey('Digits')) { $CharSet += -join ($digit09.ToCharArray() | Get-Random -Count $CharCount) }
}
ElseIf ($PSCmdlet.ParameterSetName -eq 'Complex')
{
$CharSet = -join ($UPPERAZ.ToCharArray() | Get-Random -Count $CharCount) + `
(-join ($loweraz.ToCharArray() | Get-Random -Count $CharCount)) + `
(-join ($special.ToCharArray() | Get-Random -Count $CharCount)) + `
(-join ($digit09.ToCharArray() | Get-Random -Count $CharCount))
}
### Additional characters if not divided evenly between all groups ###
If ($PasswordLength -gt $CharCount * $i)
{
If ($PSCmdlet.ParameterSetName -eq 'Chars')
{
If ($PSBoundParameters.ContainsKey('CapitalLetters')) { $CharSet += -join ($UPPERAZ.ToCharArray() | Get-Random -Count ($PasswordLength - $CharCount * $i)) }
ElseIf ($PSBoundParameters.ContainsKey('Letters')) { $CharSet += -join ($loweraz.ToCharArray() | Get-Random -Count ($PasswordLength - $CharCount * $i)) }
ElseIf ($PSBoundParameters.ContainsKey('SpecialCharacters')) { $CharSet += -join ($special.ToCharArray() | Get-Random -Count ($PasswordLength - $CharCount * $i)) }
ElseIf ($PSBoundParameters.ContainsKey('Digits')) { $CharSet += -join ($digit09.ToCharArray() | Get-Random -Count ($PasswordLength - $CharCount * $i)) }
}
ElseIf ($PSCmdlet.ParameterSetName -eq 'Complex') { $CharSet += -join ($loweraz.ToCharArray() | Get-Random -Count ($PasswordLength - $CharCount * $i)) }
}
}
End
{
### Shuffle resultant character set ###
return -join ($CharSet.ToCharArray() | sort { Get-Random })
}
} #EndFunction New-RandomPassword
Function Start-SleepProgress
{
<#
.SYNOPSIS
Put a script to sleep with progress bar.
.DESCRIPTION
This function puts a script or cmdlet to sleep for specified interval
of either seconds/minutes/hours or until specified timestamp.
.PARAMETER Second
Specifies Seconds to sleep.
.PARAMETER Minute
Specifies Minutes to sleep.
.PARAMETER Hour
Specifies Hours to sleep.
.PARAMETER Until
Specifies date/time to sleep until it.
.PARAMETER Force
If desired timestamp specified by -Until parameter
earlier than current time, then assume it will be tomorrow.
.PARAMETER ScriptBlock
Specifies code to execute after the sleep is finished.
Must be enclosed in the curly braces {}.
.EXAMPLE
C:\PS> Start-SleepProgress -Second 20
.EXAMPLE
C:\PS> Start-SleepProgress 10
The default are seconds.
.EXAMPLE
C:\PS> Start-SleepProgress -Minutes 1.5
Sleep ninety seconds.
.EXAMPLE
C:\PS> Start-SleepProgress -Hour 1.25
Sleep one hour and fifteen minutes.
.EXAMPLE
C:\PS> Start-SleepProgress -Until (Get-Date -Hour 0 -Minute 0 -Second 0).AddDays(1) -ScriptBlock {(Get-Service).Where{$_.Status -eq 'Running'} > '.\services.txt'}
Take snapshot of all running services and export the list to a text file at midnight.
.EXAMPLE
C:\PS> for ($i=0; $i -lt 10; $i++) {Start-SleepProgress -s 5 -ScriptBlock {(dir "$env:windir\Temp\" |sort LastWriteTime -Descending).Where({$_.Name -like '*.tmp'},'First')}}
Every five seconds get the newest ".TMP" file from Windows temp directory. Do it ten times.
.EXAMPLE
C:\PS> Start-SleepProgress -Until 08:45
Sleep until today 8:45 AM.
.EXAMPLE
C:\PS> Start-SleepProgress -Until 08:45 -Force
Sleep until 8:45 AM. Maybe either today or tomorrow, it depends on the current time.
.EXAMPLE
C:\PS> Start-SleepProgress -Until 1:45PM
Sleep until 13:45.
.EXAMPLE
C:\PS> Start-SleepProgress -Until (Get-Date -Hour 2 -Minute 0 -Second 0).AddDays(1)
Sleep until tomorrow 2:00 AM.
.NOTES
Author :: Roman Gelman @rgelman75
Requirement :: PowerShell 3.0
Dependency :: The maximum sleep interval is 24 hours
Version 1.0 :: 20-Nov-2016 :: [Release] :: Publicly available
Version 1.1 :: 25-Jun-2017 :: [Change] :: Minor code optimization, alias added
.LINK
https://ps1code.com/2016/11/20/sleep-powershell-scripts-progress-bar
#>
[CmdletBinding(DefaultParameterSetName = 'SEC')]
[Alias("slp")]
Param (
[Parameter(Mandatory, Position = 0, ParameterSetName = 'SEC')]
[ValidateRange(1, 86400)]
[Alias("Seconds", "s")]
[uint32]$Second
,
[Parameter(Mandatory, ParameterSetName = 'MIN')]
[ValidateRange(1, 1440)]
[Alias("Minutes", "m")]
[decimal]$Minute
,
[Parameter(Mandatory, ParameterSetName = 'HOUR')]
[ValidateRange(1, 24)]
[Alias("Hours", "h")]
[decimal]$Hour
,
[Parameter(Mandatory, ParameterSetName = 'TIME')]
[datetime]$Until
,
[Parameter(Mandatory = $false, ParameterSetName = 'TIME')]
[switch]$Force
,
[Parameter(Mandatory = $false)]
[Alias("RunAfter")]
[scriptblock]$ScriptBlock
)
Begin
{
switch -exact ($PSCmdlet.ParameterSetName)
{
'SEC'
{
$TimeSpan = New-TimeSpan -Start (Get-Date) -End (Get-Date).AddSeconds($Second)
Break
}
'MIN'
{
$Second = $Minute * 60 -as [uint32]
$TimeSpan = New-TimeSpan -Start (Get-Date) -End (Get-Date).AddSeconds($Second)
Break
}
'HOUR'
{
$Second = $Hour * 3600 -as [uint32]
$TimeSpan = New-TimeSpan -Start (Get-Date) -End (Get-Date).AddSeconds($Second)
Break
}
'TIME'
{
$TimeSpan = New-TimeSpan -Start ([datetime]::Now) -End $Until
$TotalSecond = $TimeSpan.TotalSeconds
if ($TotalSecond -le 0)
{
if ($Force) { Start-SleepProgress -Until $Until.AddDays(1) }
else { Throw "The timestamp [ $($Until.ToString()) ] is in the past!`nUse [-Force] parameter to shift the timestamp to tomorrow [ $($Until.AddDays(1)) ]." }
}
else
{
$Second = $TotalSecond -as [uint32]
}
}
}
$h = 'hour'
$m = 'minute'
$s = 'second'
$h += if ($TimeSpan.Hours -ne 1) { 's' }
$m += if ($TimeSpan.Minutes -ne 1) { 's' }
$s += if ($TimeSpan.Seconds -ne 1) { 's' }
Function Add-LeadingZero
{
Param ([Parameter(Mandatory, Position = 0)]
[int]$Digit)
$str = $Digit.ToString()
If ($str.Length -eq 1) { $str = '0' + $str }
return $str
} #EndFunction Add-LeadingZero
}
Process
{
if ($PSCmdlet.ParameterSetName -eq 'SEC')
{
for ($i = 1; $i -le $Second; $i++)
{
Write-Progress -Activity "Waiting $($TimeSpan.Hours) $h $($TimeSpan.Minutes) $m and $($TimeSpan.Seconds) $s ..." `
-CurrentOperation "Left time: $([int]($Second - $i)) seconds" `
-Status "Elapsed time: $i seconds" -PercentComplete (100/$Second * $i)
Start-Sleep -Milliseconds 980
}
}
else
{
for ($i = 1; $i -le $Second; $i++)
{
$Now = Get-Date
$TimeElapsed = New-TimeSpan -Start $Now -End $Now.AddSeconds($i)
$TimeLeft = New-TimeSpan -Start $Now -End $Now.AddSeconds([int]($Second - $i))
Write-Progress -Activity "Waiting $($TimeSpan.Hours) $h $($TimeSpan.Minutes) $m and $($TimeSpan.Seconds) $s ..." `
-CurrentOperation "Left time: $(Add-LeadingZero $TimeLeft.Hours):$(Add-LeadingZero $TimeLeft.Minutes):$(Add-LeadingZero $TimeLeft.Seconds)" `
-Status "Elapsed time: $(Add-LeadingZero $TimeElapsed.Hours):$(Add-LeadingZero $TimeElapsed.Minutes):$(Add-LeadingZero $TimeElapsed.Seconds)" `
-PercentComplete (100/$Second * $i)
Start-Sleep -Milliseconds 980
}
}
Write-Progress -Activity "Completed" -Completed
}
End
{
If ($PSBoundParameters.ContainsKey('ScriptBlock')) { &$ScriptBlock }
}
} #EndFunction Start-SleepProgress
Function Invoke-SortIpAddress
{
<#
.SYNOPSIS
Sort IP addresses pool.
.DESCRIPTION
This simple and short function intellectually sorts IP addresses.
.PARAMETER IpPool
Specifies IP addresses to sort.
.PARAMETER ZA
If specified, sort descending.
.EXAMPLE
PS C:\> '172.31.97.14', '172.31.97.20', '172.31.97.4' | Sort-IpAddress
Try the same with the Sort-Object cmdlet instead of the Sort-IpAddress and compare the results :-)
.EXAMPLE
PS C:\> 1..254 |% {"192.168.10.$_"} | Sort-IpAddress -ZA
Sort descending a Class C subnet.
.NOTES
Author :: Roman Gelman @rgelman75
Version 1.0 :: 24-Oct-2017 :: [Release] :: Publicly available
.LINK
https://ps1code.com/2017/10/26/sort-ip-address-powershell
#>
[CmdletBinding()]
[Alias("Sort-IpPool", "Sort-IpAddress")]
Param (
[Parameter(Mandatory, ValueFromPipeline)]
[ipaddress[]]$IpPool
,
[Parameter(Mandatory = $false)]
[switch]$ZA
)
Begin
{ }
Process
{
$IpPool2 += $IpPool
}
End
{
$IpPoolObj = foreach ($Ip in $IpPool2)
{
$IpBytes = $Ip.GetAddressBytes()
[pscustomobject] @{
IP = $Ip.IPAddressToString
Octat1 = $IpBytes[0]
Octat2 = $IpBytes[1]
Octat3 = $IpBytes[2]
Octat4 = $IpBytes[3]
}
}
if ($ZA) { ($IpPoolObj | Sort-Object -Descending Octat1, Octat2, Octat3, Octat4).IP }
else { ($IpPoolObj | Sort-Object Octat1, Octat2, Octat3, Octat4).IP }
}
} #EndFunction Invoke-SortIpAddress
Function Write-HostHighlight
{
<#
.SYNOPSIS
Write input string to the console while highlighting specified string.
.DESCRIPTION
This function writes input string to the PowerShell console
while highlighting specified string by color.
.PARAMETER InputObject
Specifies input string.
.PARAMETER Highlight
Specifies string to highlight within input object.
.PARAMETER BackgroundColor
Specifies background color for highlighted string.
.PARAMETER ForegroundColor
Specifies foreground color for highlighted string.
.PARAMETER HideNotMatch
If specified, does not display not matched lines.
.PARAMETER LineNum
If specified, add line numbers.
.PARAMETER CaseSensitive
If specified, the search is case sensitive.
.EXAMPLE
PS C:\> 'Errorzzz)(xxxERROR_yyyerrOr' | Write-HostHighlight -Highlight error -BackgroundColor Yellow -ForegroundColor Red
Highlight with custom colors.
.EXAMPLE
PS C:\> Get-Content $env:windir\WindowsUpdate.log | select -Last 50 | Write-HostHighlight FATAL -HideNotMatch -CaseSensitive
Make case sensitive highlight in the last lines of the log file.
.EXAMPLE
PS C:\> Get-Content $env:windir\WindowsUpdate.log -Wait | Write-HostHighlight fatal -LineNum
Add line numbers and highlight while live file monitoring (gc -Wait = tail -f).
.EXAMPLE
PS C:\> Import-Csv .\Inputfile.csv | Write-HostHighlight string
.NOTES
Author :: Roman Gelman @rgelman75
Shell :: Tested on PowerShell 5.0
Version 1.0 :: 22-Jan-2019 :: [Release] :: Publicly available
.LINK
https://ps1code.com/2019/01/22/highlight-powershell
#>
Param (
[Parameter(Mandatory, ValueFromPipeline)]
[string]$InputObject
,
[Parameter(Mandatory, Position = 0)]
[ValidateNotNull()]
[string]$Highlight
,
[Parameter(Position = 1)]
[ValidateNotNullOrEmpty()]
[Alias('bgc')]
[System.ConsoleColor]$BackgroundColor = 'Red'
,
[Parameter(Position = 2)]
[Alias('fgc')]
[System.ConsoleColor]$ForegroundColor = $Host.UI.RawUI.ForegroundColor
,
[switch]$HideNotMatch
,
[switch]$LineNum
,
[switch]$CaseSensitive
)
Begin
{
$l = 0
$fgPipe = 'Yellow'
$bgLine = $Host.UI.RawUI.BackgroundColor
$Length = $Highlight.Length
$Regex = if ($CaseSensitive)
{
New-Object System.Text.RegularExpressions.Regex([regex]::Escape($Highlight),
[System.Text.RegularExpressions.RegexOptions]::None)
}
else
{
New-Object System.Text.RegularExpressions.Regex([regex]::Escape($Highlight),
[System.Text.RegularExpressions.RegexOptions]::IgnoreCase)
}
Write-Debug "-Highlight : [$Highlight] :: HighlightEscape : [$([regex]::Escape($Highlight))]"
}
Process
{
$l++
$StartHighlight = @(($Matches = $Regex.Matches($InputObject)).Index)
if ($Matches.Success -eq $true)
{
Write-Debug "$InputObject :: [$($StartHighlight)]"
### Write line number ###
if ($LineNum)
{
$Host.UI.Write('Yellow', $bgLine, "$l ")
$Host.UI.Write($fgPipe, $bgLine, "|")
}
### Start write till first highlight ###
if ($StartHighlight[0] -ne 0)
{
$Host.UI.Write($InputObject.Substring(0, $StartHighlight[0]))
}
### Highlight & write excluding last highlight ###
for ($i = 0; $i -lt $StartHighlight.Length - 1; $i++)
{
$Host.UI.Write($ForegroundColor, $BackgroundColor, $InputObject.Substring($StartHighlight[$i], $Length))
$Host.UI.Write($InputObject.Substring($StartHighlight[$i] + $Length, $StartHighlight[$i + 1] - ($StartHighlight[$i] + $Length)))
}
### Highlight the last one ###
if (($StartHighlight[-1] + $Length) -eq $InputObject.Length)
{
$Host.UI.WriteLine($ForegroundColor, $BackgroundColor, $InputObject.Substring($StartHighlight[-1], $Length))
}
else
{
$Host.UI.Write($ForegroundColor, $BackgroundColor, $InputObject.Substring($StartHighlight[-1], $Length))
$Host.UI.WriteLine($InputObject.Substring($StartHighlight[-1] + $Length))
}
}
else
{
if ($HideNotMatch)
{
Write-Debug $InputObject
}
else
{
### Write line number ###
if ($LineNum)
{
$Host.UI.Write('DarkCyan', $bgLine, "$l ")
$Host.UI.Write($fgPipe, $bgLine, "|")
}
### Write line ###
$Host.UI.WriteLine($InputObject)
}
}
}
End { }
} #EndFunction Write-HostHighlight
Function Write-Ascii
{
<#
.SYNOPSIS
Write output by using ASCII art fonts.
.DESCRIPTION
This function draws custom text in the PowerShell
console using ASCII art fonts.