-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMicrosoft.DHCP.PowerShell.Admin.psm1
2109 lines (2051 loc) · 111 KB
/
Microsoft.DHCP.PowerShell.Admin.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
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
#=============================================================================#
# #
# Microsoft.DHCP.PowerShell.Admin.psm1 #
# Powershell Module for DHCP Administration #
# Author: Jeremy Engel #
# Creation Date: 12.16.2010 #
# Modified Date: 06.22.2011 #
# Version: 1.8.2 #
# #
#=============================================================================#
Add-Type -TypeDefinition @"
public struct DHCPDNSConfiguration {
public string Level;
public bool AllowDynamicUpdate;
public string UpdateTrigger;
public bool DiscardStaleRecords;
public bool AllowLegacyClientUpdate;
public int Value;
public override string ToString() {
switch(Value) {
case -1: return (Level=="Server")?"(Default) Update by Client Request With Cleanup":"Inherited from Parent";
case 0: return "Dynamic Updates Disabled";
case 1: return "Update by Client Request Without Cleanup";
case 2: return "Dynamic Updates Disabled";
case 3: return "Update Legacy Clients and by Client Request Without Cleanup";
case 4: return "Dynamic Updates Disabled";
case 5: return "Update by Client Request With Cleanup";
case 6: return "Dynamic Updates Disabled";
case 7: return "Update Legacy Clients and by Client Request With Cleanup";
case 16: return "Dynamic Updates Disabled";
case 17: return "Always Update Clients Without Cleanup";
case 18: return "Dynamic Updates Disabled";
case 19: return "Always Update [Legacy] Clients Without Cleanup";
case 20: return "Dynamic Updates Disabled";
case 21: return "Always Update Clients With Cleanup";
case 22: return "Dynamic Updates Disabled";
case 23: return "Always Update [Legacy] Clients With Cleanup";
default: return "Invalid Configuration";
}
}
}
public struct DHCPFilter {
public string MACAddress;
public string Description;
public override string ToString() { return MACAddress; }
}
public struct DHCPFilterConfiguration {
public string Server;
public string AllowList;
public string DenyList;
public DHCPFilter[] AllowFilters;
public DHCPFilter[] DenyFilters;
public override string ToString() { return "AllowList - "+AllowList+"\r\nDenyList - "+DenyList; }
}
public struct DHCPIPRange {
public string StartAddress;
public string EndAddress;
public override string ToString() { return StartAddress+" - "+EndAddress; }
}
public struct DHCPOption {
public int OptionID;
public string OptionName;
public string ArrayType;
public string OptionType;
public string[] Values;
public string Level;
public override string ToString() { return OptionName; }
}
public struct DHCPReservation {
public string IPAddress;
public string MACAddress;
public string Scope;
public string Server;
public DHCPOption[] Options;
public DHCPDNSConfiguration DNSConfiguration;
public override string ToString() { return IPAddress; }
}
public struct DHCPScope {
public string Address;
public string SubnetMask;
public string Name;
public string Description;
public string State;
public string Server;
public DHCPIPRange[] IPRanges;
public DHCPIPRange[] ExclusionRanges;
public DHCPReservation[] Reservations;
public int Lease;
public DHCPOption[] Options;
public DHCPDNSConfiguration DNSConfiguration;
public override string ToString() { return Address; }
}
public struct DHCPServer {
public string Name;
public string IPAddress;
public DHCPDNSConfiguration DNSConfiguration;
public int ConflictDetectionAttempts;
public DHCPScope[] Scopes;
public DHCPOption[] Options;
public override string ToString() { return Name; }
}
public struct DHCPScopeStatistics {
public string Scope;
public string Server;
public int TotalAddresses;
public int UsedAddresses;
public int PendingOffers;
public override string ToString() { return Scope; }
}
public struct DHCPServerStatistics {
public string Server;
public int Discovers;
public int Offers;
public int Requests;
public int Acks;
public int Naks;
public int Declines;
public int Releases;
public System.DateTime StartTime;
public int Scopes;
public DHCPScopeStatistics[] ScopeStatistics;
public override string ToString() { return Server; }
}
"@
function Add-DHCPExclusionRange {
<#
.Synopsis
Adds an exclusion range to a DHCP scope.
.Example
Add-DHCPExclusionRange -Server dhcp01.contoso.com -Scope 192.168.1.0 -StartAddress 192.168.1.200 -EndAddress 192.168.1.254
This example adds an exclusion range to the 192.168.1.0 scope on dhcp01.contoso.com that is between 192.168.1.200 and 192.168.1.254.
.Example
$scope | Add-DHCPExclusionRange -StartAddress 192.168.1.200 -EndAddress 192.168.1.254
Given that $scope is the DHCPScope object for subnet 192.168.1.0 on dhcp01.contoso.com, this example accomplishes the same as the one in Example 1.
.Description
The Add-DHCPExclusionRange cmdlet is used to define a new exclusion range within a given DHCP scope. The return value for success is the parent DHCPScope object.
.Parameter Server
The value for this parameter can be a DHCPServer object or the name or FQDN of the DHCP server. The designated server must by a valid DHCP server.
.Parameter Scope
The value for this parameter can be a DHCPScope object or the subnet address of the scope. If entering the subnet address, the Server parameter must be defined and be the host of this scope.
Note: This cmdlet will process significantly faster if the Scope variable supplied is a DHCPScope object.
.Parameter StartAddress
This parameter signifies the beginning of the range of IPs that you want excluded from the addresses available for lease. The value for this parameter must be in an IP address string format (ie: 0.0.0.0).
.Parameter EndAddress
This parameter signifies the end of the range of IPs that you want excluded from the addresses available for lease. The value for this parameter must be in an IP address string format (ie: 0.0.0.0).
.Outputs
DHCPScope
.Notes
Name: Add-DHCPExclusionRange
Module: Microsoft.DHCP.PowerShell.Admin.psm1
Author: Jeremy Engel
Date: 12.16.2010
#>
Param([Parameter(Mandatory=$false)][PSObject]$Server,
[Parameter(Mandatory=$true,ValueFromPipeline=$true)][PSObject]$Scope,
[Parameter(Mandatory=$true)][ValidatePattern("\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b")][string]$StartAddress,
[Parameter(Mandatory=$true)][ValidatePattern("\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b")][string]$EndAddress
)
if($Scope.GetType() -eq [DHCPScope] -and !$Server) { $Server = $Scope.Server }
$text = $(Invoke-Expression "cmd /c netsh dhcp server \\$Server scope $Scope add excluderange $StartAddress $EndAddress")
$result = if($text.GetType() -eq [string]){$text}else{$text[($text.Count-1)]}
if($result.Contains("completed successfully")) {
if($Scope.GetType() -eq [DHCPScope]) {
$range = New-Object DHCPIPRange
$range.StartAddress = $StartAddress
$range.EndAddress = $EndAddress
$Scope.ExclusionRanges += $range
return $Scope
}
else { return Get-DHCPScope -Server $Server -Scope $Scope }
}
elseif($result.Contains("Server may not function properly")) { Write-Host "ERROR: $Server is inaccessible or is not a DHCP server." -ForeGroundColor Red }
elseif($result.Contains("The command needs a valid Scope IP Address")) { Write-Host "ERROR: $Scope is not a valid scope on $Server." -ForeGroundColor Red }
elseif($result.Contains("No items to display")) { Write-Host "ERROR: $Scope must first have a defined IP range!" -ForeGroundColor Red }
else { Write-Host "ERROR: $result" -ForeGroundColor Red }
if($Scope.GetType() -eq [DHCPScope]) { return $Scope }
}
function Add-DHCPFilter {
<#
.Synopsis
Adds a MAC filter to the DHCP server.
.Example
Add-DHCPFilter -Server dhcp01.contoso.com -Deny -MACAddress 00-00-00-00-00 -Description "Example Filter"
Adds a deny filter for the given MAC address on dhcp01.contoso.com.
.Description
The Add-DHCPFilter cmdlet is used to add allow and deny MAC address filters to a given server.
.Parameter Server
The value for this parameter can be a DHCPServer object or the name or FQDN of the DHCP server. The designated server must by a valid DHCP server.
Note: This cmdlet will process significantly faster if the Server variable supplied is a DHCPServer object.
.Parameter Allow
This parameter defines the MAC address filter for the allow list.
.Parameter Deny
This parameter defines the MAC address filter for the deny list.
.Parameter MACAddress
This parameter defines the MAC address for the filter. The value for this parameter must be in one of three standard hardware address string formats:
00:00:00:00:00:00
00-00-00-00-00-00
000000000000
.Parameter Description
This parameter defines the description of the filter.
.Outputs
DHCPFilterConfiguration
.Notes
Name: Add-DHCPFilter
Module: Microsoft.DHCP.PowerShell.Admin.psm1
Author: Jeremy Engel
Date: 06.16.2011
#>
Param([Parameter(Mandatory=$true)][PSObject]$Server,
[Parameter(Mandatory=$true,ParameterSetName="Allow")][switch]$Allow,
[Parameter(Mandatory=$true,ParameterSetName="Deny")][switch]$Deny,
[Parameter(Mandatory=$true)][ValidatePattern("([0-9a-fA-F]{2}[:-]{0,1}){5}[0-9a-fA-F]{2}")][string]$MACAddress,
[Parameter(Mandatory=$true)][string]$Description
)
$MACAddress = $MACAddress.Replace("-","").Replace(":","")
$text = $(Invoke-Expression "cmd /c netsh dhcp server \\$Server v4 add filter $(if($Allow){"allow"}else{"deny"}) $MACAddress `"$Description`"")
$result = if($text.GetType() -eq [string]){$text}else{$text[($text.Count-1)]}
if($result.Contains("completed successfully")) {
if($text.Count -ge 4 -and $text[3].Contains("not a valid DNS Server")) { Write-Host "ERROR: $($text[3])" -ForeGroundColor Red; return }
}
elseif($result.Contains("Server may not function properly")) { Write-Host "ERROR: $server is inaccessible or is not a DHCP server." -ForeGroundColor Red; return }
elseif($result.Contains("Address or Address pattern is already")) { Write-Host "ERROR: There is an existing $($PSCmdlet.ParameterSetName.ToLower()) filter for $MACAddress on $Server." -ForeGroundColor Red; return }
else { Write-Host "ERROR: $result" -ForeGroundColor Red; return }
return Get-DHCPFilterConfiguration -Server $Server
}
function Add-DHCPIPRange {
<#
.Synopsis
Adds a range of IP addresses available for lease to a DHCP scope.
.Example
Add-DHCPIPRange -Server dhcp01.contoso.com -Scope 192.168.1.0 -StartAddress 192.168.1.30 -EndAddress 192.168.1.254
This example adds an ip range to the 192.168.1.0 scope on dhcp01.contoso.com that is between 192.168.1.30 and 192.168.1.254.
.Example
$scope | Add-DHCPIPRange -StartAddress 192.168.1.30 -EndAddress 192.168.1.254
Given that $scope is the DHCPScope object for subnet 192.168.1.0 on dhcp01.contoso.com, this example accomplishes the same as the one in Example 1.
.Description
The Add-DHCPIPRange cmdlet is used to define a new range of IP addresses available for lease within a given DHCP scope. The return value for success is the parent DHCPScope object.
.Parameter Server
The value for this parameter can be a DHCPServer object or the name or FQDN of the DHCP server. The designated server must by a valid DHCP server.
.Parameter Scope
The value for this parameter can be a DHCPScope object or the subnet address of the scope. If entering the subnet address, the Server parameter must be defined and be the host of this scope.
Note: This cmdlet will process significantly faster if the Scope variable supplied is a DHCPScope object.
.Parameter StartAddress
This parameter signifies the beginning of the range of IP addresses available for lease. The value for this parameter must be in an IP address string format (ie: 0.0.0.0).
.Parameter EndAddress
This parameter signifies the end of the range of IP addresses available for lease. The value for this parameter must be in an IP address string format (ie: 0.0.0.0).
.Outputs
DHCPScope
.Notes
Name: Add-DHCPIPRange
Module: Microsoft.DHCP.PowerShell.Admin.psm1
Author: Jeremy Engel
Date: 12.16.2010
#>
Param([Parameter(Mandatory=$false)][PSObject]$Server,
[Parameter(Mandatory=$true,ValueFromPipeline=$true)][PSObject]$Scope,
[Parameter(Mandatory=$true)][ValidatePattern("\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b")][string]$StartAddress,
[Parameter(Mandatory=$true)][ValidatePattern("\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b")][string]$EndAddress
)
if($Scope.GetType() -eq [DHCPScope] -and !$Server) { $Server = $Scope.Server }
$text = $(Invoke-Expression "cmd /c netsh dhcp server \\$Server scope $Scope add iprange $StartAddress $EndAddress")
$result = if($text.GetType() -eq [string]){$text}else{$text[($text.Count-1)]}
if($result.Contains("completed successfully")) {
if($Scope.GetType() -eq [DHCPScope]) {
$range = New-Object DHCPIPRange
$range.StartAddress = $StartAddress
$range.EndAddress = $EndAddress
$Scope.IPRanges += $range
return $Scope
}
else { return Get-DHCPScope -Server $Server -Scope $Scope }
}
elseif($result.Contains("Server may not function properly")) { Write-Host "ERROR: $Server is inaccessible or is not a DHCP server." -ForeGroundColor Red }
elseif($result.Contains("The command needs a valid Scope IP Address")) { Write-Host "ERROR: $Scope is not a valid scope on $Server." -ForeGroundColor Red }
else { Write-Host "ERROR: $result" -ForeGroundColor Red }
if($Scope.GetType() -eq [DHCPScope]) { return $Scope }
}
function Disable-DHCPScope {
<#
.Synopsis
Deactivates a given DHCP scope.
.Example
Disable-DHCPScope -Server dhcp01.contoso.com -Scope 192.168.1.0
This example deactivates the 192.168.1.0 scope on dhcp01.contoso.com.
.Example
$scope | Disable-DHCPScope
Given that $scope is the DHCPScope object for subnet 192.168.1.0 on dhcp01.contoso.com, this example accomplishes the same as the one in Example 1.
.Description
The Disable-DHCPScope cmdlet is used to deactivate scopes. The return value for success is the parent DHCPScope object.
.Parameter Server
The value for this parameter can be a DHCPServer object or the name or FQDN of the DHCP server. The designated server must by a valid DHCP server.
.Parameter Scope
The value for this parameter can be a DHCPScope object or the subnet address of the scope. If entering the subnet address, the Server parameter must be defined and be the host of this scope.
Note: This cmdlet will process significantly faster if the Scope variable supplied is a DHCPScope object.
.Parameter SwitchedNetwork
This is typically used for switched networks, or networks where multiple logical networks are hosted on a single physical network.
.Outputs
DHCPScope
.Notes
Name: Disable-DHCPScope
Module: Microsoft.DHCP.PowerShell.Admin.psm1
Author: Jeremy Engel
Date: 12.16.2010
#>
Param([Parameter(Mandatory=$false)][PSObject]$Server,
[Parameter(Mandatory=$true,ValueFromPipeline=$true)][PSObject]$Scope,
[Parameter(Mandatory=$false)][switch]$SwitchedNetwork
)
$val = if($SwitchedNetwork){2}else{0}
if($Scope.GetType() -eq [DHCPScope] -and !$Server) { $Server = $Scope.Server }
$text = $(Invoke-Expression "cmd /c netsh dhcp server \\$Server scope $Scope set state $val")
$result = if($text.GetType() -eq [string]){$text}else{$text[($text.Count-1)]}
if($result.Contains("completed successfully")) {
if($Scope.GetType() -eq [DHCPScope]) {
$Scope.State = if($SwitchedNetwork){"Disabled(Switched)"}else{"Disabled"}
return $Scope
}
else { return Get-DHCPScope -Server $Server -Scope $Scope }
}
elseif($result.Contains("Server may not function properly")) { Write-Host "ERROR: $Server is inaccessible or is not a DHCP server." -ForeGroundColor Red }
elseif($result.Contains("The command needs a valid Scope IP Address")) { Write-Host "ERROR: $Scope is not a valid scope on $Server." -ForeGroundColor Red }
else { Write-Host "ERROR: $result" -ForeGroundColor Red }
if($Scope.GetType() -eq [DHCPScope]) { return $Scope }
}
function Enable-DHCPScope {
<#
.Synopsis
Activates a given DHCP scope.
.Example
Enable-DHCPScope -Server dhcp01.contoso.com -Scope 192.168.1.0
This example activates the 192.168.1.0 scope on dhcp01.contoso.com.
.Example
$scope | Enable-DHCPScope
Given that $scope is the DHCPScope object for subnet 192.168.1.0 on dhcp01.contoso.com, this example accomplishes the same as the one in Example 1.
.Description
The Enable-DHCPScope cmdlet is used to activate scopes. The return value for success is the parent DHCPScope object.
.Parameter Server
The value for this parameter can be a DHCPServer object or the name or FQDN of the DHCP server. The designated server must by a valid DHCP server.
.Parameter Scope
The value for this parameter can be a DHCPScope object or the subnet address of the scope. If entering the subnet address, the Server parameter must be defined and be the host of this scope.
Note: This cmdlet will process significantly faster if the Scope variable supplied is a DHCPScope object.
.Parameter SwitchedNetwork
This is typically used for switched networks, or networks where multiple logical networks are hosted on a single physical network.
.Outputs
DHCPScope
.Notes
Name: Enable-DHCPScope
Module: Microsoft.DHCP.PowerShell.Admin.psm1
Author: Jeremy Engel
Date: 12.16.2010
#>
Param([Parameter(Mandatory=$false)][PSObject]$Server,
[Parameter(Mandatory=$true,ValueFromPipeline=$true)][PSObject]$Scope,
[Parameter(Mandatory=$false)][switch]$SwitchedNetwork
)
$val = if($SwitchedNetwork){3}else{1}
if($Scope.GetType() -eq [DHCPScope] -and !$Server) { $Server = $Scope.Server }
$text = $(Invoke-Expression "cmd /c netsh dhcp server \\$Server scope $Scope set state $val")
$result = if($text.GetType() -eq [string]){$text}else{$text[($text.Count-1)]}
if($result.Contains("completed successfully")) {
if($Scope.GetType() -eq [DHCPScope]) {
$Scope.State = if($SwitchedNetwork){"Active(Switched)"}else{"Active"}
return $Scope
}
else { return Get-DHCPScope -Server $Server -Scope $Scope }
}
elseif($result.Contains("Server may not function properly")) { Write-Host "ERROR: $Server is inaccessible or is not a DHCP server." -ForeGroundColor Red }
elseif($result.Contains("The command needs a valid Scope IP Address")) { Write-Host "ERROR: $Scope is not a valid scope on $Server." -ForeGroundColor Red }
else { Write-Host "ERROR: $result" -ForeGroundColor Red }
if($Scope.GetType() -eq [DHCPScope]) { return $Scope }
}
function Get-DHCPDNSConfiguration {
<#
.Synopsis
Retrieves the DNS update configuration for a given DHCP object.
.Example
Get-DHCPDNSConfiguration -Owner dhcp01.contoso.com/192.168.1.0/192.168.1.237
This example retrieves the DNS update configuration for the reservation 192.168.1.237 in the scope of 192.168.1.0 on server dhcp01.contoso.com.
.Description
The Get-DHCPDNSConfiguration cmdlet is used to retrieve the DNS update configuration for a given DHCP server, scope, or reservation. If no configuration is defined, then the configuration from its parent is inherited, except at the server level, which uses the default configuration.
.Parameter Owner
The value for this parameter can be a DHCPServer, DHCPScope, or DHCPReservation object. It can also be a string representation of these objects, defined thus:
ServerNameOrFQDN
ServerNameOrFQDN/ScopeAddress
ServerNameOrFQDN/ScopeAddress/ReservationAddress
.Outputs
DHCPDNSConfiguration
.Notes
Name: Get-DHCPDNSConfiguration
Module: Microsoft.DHCP.PowerShell.Admin.psm1
Author: Jeremy Engel
Date: 04.22.2011
#>
Param([Parameter(Mandatory=$true,ValueFromPipeline=$true)][PSObject]$Owner)
if($Owner.GetType() -eq [string]) {
switch($Owner.Split("/").Count) {
1 { $level = "Server" }
2 { $level = "Scope" }
3 { $level = "Reservation" }
default { return }
}
}
else { $level = $Owner.GetType().ToString().Substring(4) }
if(!($option = Get-DHCPOption -Owner $Owner -OptionID 81 -Force)) { return }
$value = [int]$option.Values[0]
$dnsConfig = New-Object DHCPDNSConfiguration
$dnsConfig.Level = $level
$dnsConfig.Value = $value
if($value -eq -1) {
if($level -ne "Server") {
if($Owner.GetType() -eq [string]) {
$parts = $Owner.Split("/")
$Owner = $parts[0..($parts.Count-2)] -Join "/"
}
elseif($Owner.GetType() -eq [DHCPReservation]) { $Owner = "$($Owner.Server)/$($Owner.Scope)" }
elseif($Owner.GetType() -eq [DHCPScope]) { $Owner = $Owner.Server }
else { return }
$dnsConfig = Get-DHCPDNSConfiguration -Owner $Owner
$dnsConfig.Level = $level
return $dnsConfig
}
else { $value = 5 }
}
if($value -ge 16) {
$dnsConfig.UpdateTrigger = "Always"
$value -= 16
}
else { $dnsConfig.UpdateTrigger = "ClientRequest" }
if($value -ge 4) {
$dnsConfig.DiscardStaleRecords = $true
$value -= 4
}
else { $dnsConfig.DiscardStaleRecords = $false }
if($value -ge 2) {
$dnsConfig.AllowLegacyClientUpdate = $true
$value -= 2
}
else { $dnsConfig.AllowLegacyClientUpdate = $false }
$dnsConfig.AllowDynamicUpdate = if($value -eq 1){$true}else{$false}
return $dnsConfig
}
function Get-DHCPFilters {
Param([string[]]$Text,[int]$Start)
$filters = @()
for($i=$Start;$i -lt $Text.Count;$i++) {
$parts = $Text[$i].Split("`t") | %{ $_.Trim() }
if($parts.Count -ne 4) { break }
$filter = New-Object DHCPFilter
$filter.MACAddress = $parts[2]
$filter.Description = $parts[3]
$filters += $filter
}
return $filters
}
function Get-DHCPFilterConfiguration {
<#
.Synopsis
Gets the MAC Filter configuration for a given DHCP server.
.Example
Get-DHCPFilterConfiguration -Server dhcp01.contoso.com
Returns the Filter configuration for dhcp01.contoso.com.
.Description
The Get-DHCPFilterConfiguration cmdlet is used to retrieve the Allow and Deny MAC address filter configuration.
.Parameter Server
The value for this parameter can be a DHCPServer object or the name or FQDN of the DHCP server. The designated server must by a valid DHCP server.
Note: This cmdlet will process significantly faster if the Server variable supplied is a DHCPServer object.
.Outputs
DHCPFilterConfiguration
.Notes
Name: Get-DHCPFilterConfiguration
Module: Microsoft.DHCP.PowerShell.Admin.psm1
Author: Jeremy Engel
Date: 06.16.2011
#>
Param([Parameter(Mandatory=$true)][PSObject]$Server)
$text = $(Invoke-Expression "cmd /c netsh dhcp server \\$Server v4 show filter")
$result = if($text.GetType() -eq [string]){$text}else{$text[($text.Count-1)]}
if($result.Contains("completed successfully")) {
if($text[3].Contains("not a valid DNS Server")) { Write-Host "ERROR: $($text[3])" -ForeGroundColor Red; return }
}
elseif($result.Contains("Server may not function properly")) { Write-Host "ERROR: $server is inaccessible or is not a DHCP server." -ForeGroundColor Red; return }
else { Write-Host "ERROR: $result" -ForeGroundColor Red; return }
$filterConfig = New-Object DHCPFilterConfiguration
$filterConfig.Server = "$Server"
$filterConfig.AllowList = if($($text[9].Split("=") | %{ $_.Trim() })[1] -eq "1"){"Enabled"}else{"Disabled"}
$filterConfig.DenyList = if($($text[10].Split("=") | %{ $_.Trim() })[1] -eq "1"){"Enabled"}else{"Disabled"}
$filterConfig.AllowFilters = Get-DHCPFilters -Text $text -Start 19
for($i=20;$i -lt $text.Count;$i++) { if($text[$i].Contains("Total No. of MAC")) { break } }
$filterConfig.DenyFilters = Get-DHCPFilters -Text $text -Start ($i+7)
return $filterConfig
}
function Get-DHCPIPRanges {
Param([Parameter(Mandatory=$false)][PSObject]$Server,
[Parameter(Mandatory=$true,ValueFromPipeline=$true)][PSObject]$Scope,
[Parameter(Mandatory=$true)][string]$Type
)
$ipranges = @()
if($Scope.GetType() -eq [DHCPScope] -and !$Server) { $Server = $Scope.Server }
$text = $(Invoke-Expression "cmd /c netsh dhcp server \\$Server scope $Scope show $Type")
$result = if($text.GetType() -eq [string]){$text}else{$text[($text.Count-1)]}
if($result.Contains("The command needs a valid Scope IP Address")) { Write-Host "ERROR: $Scope is not a valid scope on $Server." -ForeGroundColor Red; return }
if($result.Contains("Server may not function properly")) { Write-Host "ERROR: $Server is inaccessible or is not a DHCP server." -ForeGroundColor Red; return }
for($i=6;$i -lt $text.Count;$i++) {
if(!$text[$i]) { break }
$parts = $text[$i].Split("-") | %{ $_.Trim() }
$ipRange = New-Object DHCPIPRange
$ipRange.StartAddress = $parts[0]
$ipRange.EndAddress = $parts[1]
$ipRanges += $ipRange
}
return $ipRanges
}
function Get-DHCPOption {
<#
.Synopsis
Retrieves all or specific DHCP options and their values for a given level.
.Example
Get-DHCPOption -Owner dhcp01.contoso.com/192.168.1.0/192.168.1.237
This example retrieves all the options set for the reservation 192.168.1.237 in the scope of 192.168.1.0 on server dhcp01.contoso.com.
.Example
$scope | Get-DHCPOption -OptionID 3
Given that $scope is the DHCPScope object for subnet 192.168.1.0 on dhcp01.contoso.com, this examples retrieves scope option 3, the gateway address.
.Example
Get-DHCPOption -Owner $server -OptionID 81 -Force
Given that $server is the DHCPServer object for dhcp01.contoso.com, this example retrieves server option 81, dynamic dns configuration. For more information on why the -Force parameter was needed, please read its parameter
description under -full.
.Description
The Get-DHCPOption cmdlet is used to retrieve all or specific standard DHCP options and their values for a given server, scope, or reservation. Non-standard classes (eg: BOOTP) are not currently being recorded. The return value for success is one or an array of DHCPOption objects.
.Parameter Owner
The value for this parameter can be a DHCPServer, DHCPScope, or DHCPReservation object. It can also be a string representation of these objects, defined thus:
ServerNameOrFQDN
ServerNameOrFQDN/ScopeAddress
ServerNameOrFQDN/ScopeAddress/ReservationAddress
.Parameter OptionID
The value for this parameter must be the id of a valid DHCP option as listed by Get-DHCPOptionDefinitions.
.Parameter Force
This switch parameter is only needed if you want to retrieve options 51 (lease time in seconds) or 81 (int value for the dynamic dns configuration). These are otherwise not returned as their values are expressed elsewhere.
.Outputs
DHCPOption
.Notes
Name: Get-DHCPOption
Module: Microsoft.DHCP.PowerShell.Admin.psm1
Author: Jeremy Engel
Date: 12.16.2010
#>
Param([Parameter(Mandatory=$true,ValueFromPipeline=$true)][PSObject]$Owner,
[Parameter(Mandatory=$false)][ValidateRange(1,254)][int]$OptionID,
[Parameter(Mandatory=$false)][switch]$Force
)
$server = $null
$scope = $null
$reservation = $null
if($Owner.GetType() -eq [DHCPServer]) { $server = $Owner.Name }
elseif($Owner.GetType() -eq [DHCPScope]) {
$server = $Owner.Server
$scope = $Owner.Address
}
elseif($Owner.GetType() -eq [DHCPReservation]) {
$server = $Owner.Server
$scope = $Owner.Scope
$reservation = $Owner.IPAddress
}
else {
$parts = $Owner.ToString().Split("/")
$server = $parts[0]
if($parts.Count -gt 1) { $scope = $parts[1] }
if($parts.Count -gt 2) { $reservation = $parts[2] }
}
$command = if($scope){"\\$server scope $scope show optionvalue"}else{"\\$server show optionvalue"}
if($reservation) { $command = "\\$server scope $scope show reservedoptionvalue $reservation" }
$text = $(Invoke-Expression "cmd /c netsh dhcp server $command")
$result = if($text.GetType() -eq [string]){$text}else{$text[($text.Count-1)]}
if($result.Contains("Server may not function properly")) { Write-Host "ERROR: $server is inaccessible or is not a DHCP server." -ForeGroundColor Red; return }
if($result.Contains("The command needs a valid Scope IP Address")) { Write-Host "ERROR: $scope is not a valid scope on $server." -ForeGroundColor Red; return }
if($result.Contains("client is not a reserved")) { Write-Host "ERROR: $reservation is not a valid reservation in $scope on $server." -ForeGroundColor Red; return }
$options = @()
$work = @{}
$optiondefs = Get-DHCPOptionDefinitions -Server $server
$option = New-Object PSOBject
$option | Add-Member NoteProperty OptionName("DNS Configuration")
$option | Add-Member NoteProperty ArrayType("UNARY")
$option | Add-Member NoteProperty OptionType("DWORD")
$optiondefs.Add(81,$option)
$id = $null
$block = $false
for($i=0;$i -lt $text.Count;$i++) {
if($text[$i] -eq "Command completed successfully.") {
if(!!$id) {
$option = New-Object DHCPOption
$option.OptionID = $id
$option.OptionName = $optiondefs[$id].OptionName
$option.ArrayType = $optiondefs[$id].ArrayType
$option.OptionType = $optiondefs[$id].OptionType
$option.Values += $work[$optiondefs[$id].OptionName]
$option.Level = if($reservation){"Reservation"}elseif($scope){"Scope"}else{"Server"}
$options += $option
}
}
if($block) {
if($text[$i].Contains("DHCP Standard Options")) { $block = $false }
continue
}
if($text[$i].Contains("For vendor class") -or $text[$i].Contains("For user class")) { $block = $true; continue }
if($text[$i].Contains("OptionId")) {
if(!!$id) {
$option = New-Object DHCPOption
$option.OptionID = $id
$option.OptionName = $optiondefs[$id].OptionName
$option.ArrayType = $optiondefs[$id].ArrayType
$option.OptionType = $optiondefs[$id].OptionType
$option.Values += $work[$optiondefs[$id].OptionName]
$option.Level = if($reservation){"Reservation"}elseif($scope){"Scope"}else{"Server"}
$options += $option
}
$id = [int]($text[$i].Split(":")[1].Trim())
if($OptionID -and $OptionID -ne $id) { $id = $null; continue }
if(!$Force -and ($id -eq 81 -or $id -eq 51)) { $id = $null; continue }
$work.Add($optiondefs[$id].OptionName,$null)
}
elseif(!$id) { continue }
else {
if(!($text[$i].Contains("Option Element Value"))) { continue }
$desc = $optiondefs[$id].OptionName
$type = $optiondefs[$id].ArrayType
$values = $work[$desc]
$val = $text[$i].Split("=")[1].Trim()
if($type-eq"ARRAY") { $values += @($val) }
else { $values = $val }
$work[$desc] = $values
}
}
if($Force -and !($options | Where-Object { $_.OptionID -eq 81 }) -and $OptionID -eq 81) {
$id = 81
$option = New-Object DHCPOption
$option.OptionID = $id
$option.OptionName = $optiondefs[$id].OptionName
$option.ArrayType = $optiondefs[$id].ArrayType
$option.OptionType = $optiondefs[$id].OptionType
$option.Values += -1
$option.Level = if($reservation){"Reservation"}elseif($scope){"Scope"}else{"Server"}
$options += $option
}
return $options
}
function Get-DHCPOptionDefinitions {
<#
.Synopsis
Retrieves all DHCP options defined on a given server.
.Example
Get-DHCPOptionDefinitions -Server dhcp01.contoso.com
This example retrieves all the options defined on server dhcp01.contoso.com.
.Example
$server | Get-DHCPOptionDefinitions
Given that $server is the DHCPServer object for dhcp01.contoso.com, this example accomplishes the same as the one in Example 1.
.Description
The Get-DHCPOptionDefinitions cmdlet is used to retrieve all DHCP options defined on a given server.
.Parameter Server
The value for this parameter can be a DHCPServer object or the name or FQDN of the DHCP server. The designated server must by a valid DHCP server.
.Outputs
HashTable
.Notes
Name: Get-DHCPOptionDefinitions
Module: Microsoft.DHCP.PowerShell.Admin.psm1
Author: Jeremy Engel
Date: 12.16.2010
#>
Param([Parameter(Mandatory=$true,ValueFromPipeline=$true)][PSObject]$Server)
$options = @{}
$text = $(Invoke-Expression "cmd /c netsh dhcp server \\$Server show optiondef")
if($text[2].Contains("Server may not function properly")) { Write-Host "ERROR: $Server is inaccessible or is not a DHCP server." -ForeGroundColor Red; return }
for($i=7;$i -lt $text.Count;$i++) {
if(!$text[$i]) { break }
$parts = $text[$i].Split("-") | %{ $_.Trim() }
if($parts[2] -eq "to") {
$parts[1] = $parts[1]+"-"+$parts[2]+"-"+$parts[3]
$parts[2] = $parts[4]
$parts[3] = $parts[5]
}
$option = New-Object PSOBject
$option | Add-Member NoteProperty OptionName($parts[1])
$option | Add-Member NoteProperty ArrayType($parts[2])
$option | Add-Member NoteProperty OptionType($parts[3])
if($options.Keys -notcontains [int]$parts[0]) { $options.Add([int]$parts[0],$option) } #Needed if clause for some weird funkiness on Windows 2003 servers
}
return $options
}
function Get-DHCPReservation {
<#
.Synopsis
Retieves all or specific DHCP reservations for a given scope.
.Example
Get-DHCPReservation -Server dhcp01.contoso.com -Scope 192.168.1.0
This example retrieves all reservations in the 192.168.1.0 scope on dhcp01.contoso.com.
.Example
$scope | Get-DHCPReservation -IPAddress 192.168.1.237
Given that $scope is the DHCPScope object for subnet 192.168.1.0 on dhcp01.contoso.com, this example retrieves the 192.168.1.237 reservation.
.Description
The Get-DHCPReservation cmdlet is used to retieves all or specific DHCP reservations for a given scope. The return value for success is one or an array of DHCPReservation objects.
.Parameter Server
The value for this parameter can be a DHCPServer object or the name or FQDN of the DHCP server. The designated server must by a valid DHCP server.
.Parameter Scope
The value for this parameter can be a DHCPScope object or the subnet address of the scope. If entering the subnet address, the Server parameter must be defined and be the host of this scope.
.Parameter IPAddress
The value for this parameter must be in an IP address string format (ie: 0.0.0.0).
.Outputs
DHCPReservation
.Notes
Name: Get-DHCPReservation
Module: Microsoft.DHCP.PowerShell.Admin.psm1
Author: Jeremy Engel
Date: 12.16.2010
#>
Param([Parameter(Mandatory=$false)][PSObject]$Server,
[Parameter(Mandatory=$true,ValueFromPipeline=$true)][PSObject]$Scope,
[Parameter(Mandatory=$false)][ValidatePattern("\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b")][string]$IPAddress
)
$reservations = @()
if($Scope.GetType() -eq [DHCPScope] -and !$Server) { $Server = $Scope.Server }
$text = $(Invoke-Expression "cmd /c netsh dhcp server \\$Server scope $Scope show reservedip")
$result = if($text.GetType() -eq [string]){$text}else{$text[($text.Count-1)]}
if($result.Contains("The command needs a valid Scope IP Address")) { Write-Host "ERROR: $Scope is not a valid scope on $Server." -ForeGroundColor Red; return }
if($result.Contains("Server may not function properly")) { Write-Host "ERROR: $Server is inaccessible or is not a DHCP server." -ForeGroundColor Red; return }
for($i=7;$i -lt $text.Count;$i++) {
if(!$text[$i]) { break }
$parts = $text[$i].Split("-") | %{ $_.Trim() }
if($IPAddress -and $parts[0] -ne $IPAddress) { continue }
$reservation = New-Object DHCPReservation
$reservation.IPAddress = $parts[0]
$reservation.MACAddress = [string]::Join("-",$parts[1..6])
$reservation.Scope = $Scope
$reservation.Server = $Server
$reservation.Options = Get-DHCPOption -Owner $reservation
$reservation.DNSConfiguration = Get-DHCPDNSConfiguration -Owner $reservation
$reservations += $reservation
}
return $reservations
}
function Get-DHCPScope {
<#
.Synopsis
Retieves all or specifics scopes for a given server.
.Example
Get-DHCPScope -Server dhcp01.contoso.com
This example retrieves all scopes on dhcp01.contoso.com.
.Example
$server | Get-DHCPScope -Scope 192.168.1.0
Given that $server is the DHCPServer object for dhcp01.contoso.com, this example retrieves the scope 192.168.1.0.
.Description
The Get-DHCPScope cmdlet is used to retieves all or specific DHCP scopes on a given server. The return value for success is one or an array of DHCPScope objects.
.Parameter Server
The value for this parameter can be a DHCPServer object or the name or FQDN of the DHCP server. The designated server must by a valid DHCP server.
.Parameter Scope
The value for this parameter must be the subnet address of the scope.
.Outputs
DHCPScope
.Notes
Name: Get-DHCPScope
Module: Microsoft.DHCP.PowerShell.Admin.psm1
Author: Jeremy Engel
Date: 12.16.2010
#>
Param([Parameter(Mandatory=$true,ValueFromPipeline=$true)][PSObject]$Server,
[Parameter(Mandatory=$false)][string]$Scope
)
$dhcpScopes = @()
$scopeList = @{}
$text = $(Invoke-Expression "cmd /c netsh dhcp server \\$Server show scope")
if($text[2].Contains("Server may not function properly")) { Write-Host "ERROR: $Server is inaccessible or is not a DHCP server." -ForeGroundColor Red; return }
for($i=5;$i -lt $text.Count;$i++) {
if(!$text[$i]) { break }
$parts = $text[$i].Split("-") | %{ $_.Trim() }
$scopeList.Add($parts[0],@($parts[1],$parts[2],$parts[3],$parts[4]))
}
foreach($address in $scopeList.Keys) {
if($Scope -and $address -ne $Scope) { continue }
$dhcpScope = New-Object DHCPScope
$dhcpScope.Address = $address
$dhcpScope.Server = $Server.ToString()
$dhcpScope.SubnetMask = $scopeList[$address][0]
$dhcpScope.State = $scopeList[$address][1]
$dhcpScope.Name = $scopeList[$address][2]
$dhcpScope.Description = $scopeList[$address][3]
$dhcpScope.IPRanges = Get-DHCPIPRanges -Server $Server -Scope $address -Type "ipRange"
$dhcpScope.ExclusionRanges = Get-DHCPIPRanges -Server $Server -Scope $address -Type "excluderange"
$dhcpScope.Reservations = Get-DHCPReservation -Server $Server -Scope $address
$lease = Get-DHCPOption -Owner $dhcpScope -OptionID 51 -Force
$dhcpScope.Lease = if($lease){$lease.Values[0]}else{0}
$dhcpScope.Options = Get-DHCPOption -Owner $dhcpScope
$dhcpScope.DNSConfiguration = Get-DHCPDNSConfiguration -Owner $dhcpScope
$dhcpScopes += $dhcpScope
}
return $dhcpScopes
}
function Get-DHCPServer {
<#
.Synopsis
Retieves a specific DHCP server, or all DHCP servers in the current domain.
.Example
Get-DHCPServer -Server dhcp01.contoso.com
This example retrieves the DHCP server object for dhcp01.contoso.com.
.Description
The Get-DHCPServer cmdlet is used to retieve a specific DHCP server, or all DHCP servers in the current domain. The return value for success is one or an array of DHCPServer objects.
Note: Due to the nature of calling netsh commands from powershell, this cmdlet can take an exceptionally long time to complete.
.Parameter Server
The value for this parameter must be the name or FQDN of the DHCP server.
.Outputs
DHCPServer
.Notes
Name: Get-DHCPServer
Module: Microsoft.DHCP.PowerShell.Admin.psm1
Author: Jeremy Engel
Date: 12.16.2010
#>
Param([Parameter(Mandatory=$true,ValueFromPipeline=$true)][string]$Server)
$dhcpServers = @()
if(!$Server) {
$configDN = ([adsi]"LDAP://RootDSE").configurationNamingContext
$objADSearch = New-Object System.DirectoryServices.DirectorySearcher
$objADSearch.SearchRoot = New-Object System.DirectoryServices.DirectoryEntry("LDAP://CN=NetServices,CN=Services,$configDN")
$objADSearch.PageSize = 1000
$objADSearch.Filter = "(&(objectClass=dhcpClass)(dhcpIdentification=DHCP Server object))"
$objADSearch.SearchScope = "OneLevel"
$serverList = $objADSearch.FindAll() | %{ $_.Properties["cn"][0] }
}
else {
$result = $(Invoke-Expression "cmd /c netsh dhcp server \\$Server")
if($result -is [array] -and $result[0].Contains("Lists all the commands available")) { $serverList = @($Server) }
else { Write-Host "ERROR: $Server is inaccessible or is not a DHCP server." -ForeGroundColor Red }
}
foreach($name in $serverList) {
$dhcpServer = New-Object DHCPServer
$dhcpServer.Name = $name
$text = $(Invoke-Expression "cmd /c netsh dhcp server \\$name show server")
if($text[2].Contains("Server may not function properly")) { Write-Host "ERROR: $Server is inaccessible or is not a DHCP server." -ForeGroundColor Red; continue }
else { $dhcpServer.IPAddress = $text[2].Split("=")[1].Trim() }
$dhcpServer.DNSConfiguration = Get-DHCPDNSConfiguration -Owner $dhcpServer
$text = $(Invoke-Expression "cmd /c netsh dhcp server \\$name show detectconflictretry")
if($text[2].Contains("Server may not function properly")) { Write-Host "ERROR: $Server is inaccessible or is not a DHCP server." -ForeGroundColor Red; continue }
else { $dhcpServer.ConflictDetectionAttempts = [int]($text[1].Split(":")[1].Trim()) }
$dhcpServer.Scopes = Get-DHCPScope -Server $name
$dhcpServer.Options = Get-DHCPOption -Owner $name
$dhcpServers += $dhcpServer
}
return $dhcpServers
}
function Get-DHCPStatistics {
<#
.Synopsis
Retieves the DHCP statistics of a specific server.
.Example
Get-DHCPStatistics -Server dhcp01.contoso.com
This example retrieves the DHCP statistics for dhcp01.contoso.com.
.Example
$server | Get-DHCPStatistics
Given that $server is the DHCPServer object for dhcp01.contoso.com, this example accomplishes the same as the one in Example 1.
.Description
The Get-DHCPStatistics cmdlet is used to retieve the DHCP statistics of a specific server and its scopes. The return value for success is a DHCPStatistics object.
.Parameter Server
The value for this parameter can be a DHCPServer object or the name or FQDN of the DHCP server. The designated server must by a valid DHCP server.
.Outputs
DHCPStatistics
.Notes
Name: Get-DHCPStatistics
Module: Microsoft.DHCP.PowerShell.Admin.psm1
Author: Jeremy Engel
Date: 12.16.2010
#>
Param([Parameter(Mandatory=$true,ValueFromPipeline=$true)][PSObject]$Server)
$text = $(Invoke-Expression "cmd /c netsh dhcp server \\$Server show mibinfo")
if($text[2].Contains("Server may not function properly")) { Write-Host "ERROR: $Server is inaccessible or is not a DHCP server." -ForeGroundColor Red; return }
$serverStats = New-Object DHCPServerStatistics
$serverStats.Server = "$Server"
for($i=2;$i-lt$text.Count;$i++) {
if(!$text[$i]) { break }
$parts = $text[$i].Split("=") | %{ $_.Trim().Trim(".") }
switch($parts[0]) {
"Discovers" { $serverStats.Discovers = [int]$parts[1] }
"Offers" { $serverStats.Offers = [int]$parts[1] }
"Requests" { $serverStats.Requests = [int]$parts[1] }
"Acks" { $serverStats.Acks = [int]$parts[1] }
"Naks" { $serverStats.Naks = [int]$parts[1] }
"Declines" { $serverStats.Declines = [int]$parts[1] }
"Releases" { $serverStats.Releases = [int]$parts[1] }
"ServerStartTime" { $serverStats.StartTime = [DateTime]$parts[1] }
"Scopes" { $serverStats.Scopes = [int]$parts[1] }
"Subnet" {
$scopeStats = New-Object DHCPScopeStatistics
$scopeStats.Scope = $parts[1]
$scopeStats.Server = "$Server"
$parts = $text[++$i].Split("=") | %{ $_.Trim().Trim(".") }
$used = [int]$parts[1]
$parts = $text[++$i].Split("=") | %{ $_.Trim().Trim(".") }
$scopeStats.TotalAddresses = $used+[int]$parts[1]
$scopeStats.UsedAddresses = $used
$parts = $text[++$i].Split("=") | %{ $_.Trim().Trim(".") }
$scopeStats.PendingOffers = [int]$parts[1]
$serverStats.ScopeStatistics += $scopeStats
}
}
}
return $serverStats
}
function New-DHCPOptionDefinition {
<#
.Synopsis
Creates a new option on a given server.
.Example
New-DHCPOptionDefinition -Server dhcp01.contoso.com -OptionID 200 -Name TestOption -DataType IPADDRESS -IsArray
This example creates option 200 on server dhcp01.contoso.com with the name, TestOption, and an accepted value as an array of IP addresses.
.Example
$server | New-DHCPOptionDefinition -OptionID 200 -Name TestOption -DataType IPADDRESS -IsArray -Description "A test option."
Given that $server is the DHCPServer object for dhcp01.contoso.com, this example accomplishes the same as the one in Example 1 with a description too.
.Description
The New-DHCPOptionDefinition cmdlet is used to create a new option on a given server.
.Parameter Server
The value for this parameter can be a DHCPServer object or the name or FQDN of the DHCP server. The designated server must by a valid DHCP server.
.Parameter OptionID
An integer between 1 and 254 that is not currently used by another option.
.Parameter Name
A self-explanatory string value.
.Parameter DataType
A string value for the data type of the value(s).
The available options are:
BYTE
WORD
DWORD
STRING
IPADDRESS
.Parameter IsArray
A boolean value for whether the accepted value is unary or an array.
.Parameter VendorClass
A string value for the name of the vendor class of DHCP options. If none is given, the Standard DHCP Options class is used.
.Parameter UserClass
A string value for the name of the user class of the DHCP option. If none is given, the Default User Class is used.
.Parameter DefaultValue
A string value for the initial value of the option, if not explicitly defined by an admin.
.Outputs
Success = String message
Failure = NULL
.Notes
Name: New-DHCPOptionDefinition
Module: Microsoft.DHCP.PowerShell.Admin.psm1
Author: Jeremy Engel
Date: 12.16.2010
#>
Param([Parameter(Mandatory=$true,ValueFromPipeline=$true)][PSObject]$Server,
[Parameter(Mandatory=$true)][ValidateRange(1,254)][int]$OptionID,
[Parameter(Mandatory=$true)][string]$Name,
[Parameter(Mandatory=$true)][ValidateSet("BYTE","WORD","DWORD","STRING","IPADDRESS")][string]$DataType,
[Parameter(Mandatory=$false)][switch]$IsArray,
[Parameter(Mandatory=$false)][string]$VendorClass,
[Parameter(Mandatory=$false)][string]$Description,
[Parameter(Mandatory=$false)][string]$DefaultValue
)
$arrayInt = if($IsArray){1}else{0}
if(!!$VendorClass) { $VendorClass = "vendor=`"$VendorClass`"" }
if(!!$Description) { $Description = "comment=`"$Description`"" }
$text = $(Invoke-Expression "cmd /c netsh dhcp server \\$Server add optiondef $OptionID `"$Name`" $DataType $arrayInt $VendorClass $Description `"$DefaultValue`"")
$result = if($text.GetType() -eq [string]){$text}else{$text[($text.Count-1)]}
if($result.Contains("completed successfully")) { return $result }
elseif($result.Contains("Server may not function properly")) { Write-Host "ERROR: $Server is inaccessible or is not a DHCP server." -ForeGroundColor Red }
else { Write-Host "ERROR: $result" -ForeGroundColor Red }
}
function New-DHCPReservation {
<#
.Synopsis
Creates a new reservation within a given scope.