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 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 DHCPLeasedIp {
public string IPAddress;
public string SubnetMask;
public string MACAddress;
public string LeaseExpires;
public string Tip;
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 DHCPLeasedIp[] LeasedIPs;
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 Show-DHCPServers {
<#
.Synopsis
Displays a list of all DHCP servers in Active Directory.
.Description
The Show-DHCPServers cmdlet is used to display all DHCP servers registered in Active Directory.
.Outputs
PSObject
.Notes
Name: Show-DHCPServers
Module: Microsoft.DHCP.PowerShell.Admin.psm1
Author: Jeremy Engel
Date: 03.15.2011
#>
$servers = @()
$text = $(Invoke-Expression "cmd /c netsh dhcp show server")
$result = if($text.GetType() -eq [string]){$text}else{$text[($text.Count-1)]}
if($result.Contains("completed successfully")) {
foreach($line in $text) {
if($line.Contains("Server [")) {
$parts = $line.Split("[")
$name = $parts[1].Split("]")[0]
$ip = $parts[2].Split("]")[0]
$server = New-Object PSObject
$server | Add-Member NoteProperty Server($name)
$server | Add-Member NoteProperty IPAddress($ip)
$servers += $server
}
}
return $servers
}
else { Write-Host "ERROR: $result" -ForeGroundColor Red }
}
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]))
}
<#
$scopeList:
Name Value
---- -----
10.214.15.128 {255.255.255.128, Disabled, pdawireless, }
10.214.15.0 {255.255.255.128, Active, base, }
#>
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
$dhcpScope.LeasedIPs = Get-DHCPLeasedIps -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
<#
Address : 10.214.15.128
SubnetMask : 255.255.255.128
Name : pdawireless
Description :
State : Disabled
Server : 10.214.15.12
IPRanges : {10.214.15.140 - 10.214.15.180}
ExclusionRanges :
Reservations :
Lease : 691200
Options : {Router, DNS Domain Name, DNS Servers}
DNSConfiguration : Inherited from Parent
Address : 10.214.15.0
SubnetMask : 255.255.255.128
Name : base
Description :
State : Active
Server : 10.214.15.12
IPRanges : {10.214.15.21 - 10.214.15.99}
ExclusionRanges :
Reservations :
Lease : 691200
Options : {Router, DNS Domain Name, DNS Servers}
DNSConfiguration : Inherited from Parent
#>
}
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-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)][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)][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-DHCPLeasedIps {
<#
.Synopsis
Retieves all or specific DHCP leased IP lists 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)][string]$IPAddress
)
$leasedIPs = @()
if($Scope.GetType() -eq [DHCPScope] -and !$Server) { $Server = $Scope.Server }
$text = $(Invoke-Expression "cmd /c netsh dhcp server \\$Server scope $Scope show clients")
$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=8;$i -lt $text.Count;$i++) {
if(!$text[$i]) { break }
$parts = $text[$i].Split("-") | %{ $_.Trim() }
if($IPAddress -and $parts[0] -ne $IPAddress) { continue }
$leased = New-Object DHCPLeasedIp
$leased.IPAddress = $parts[0]
$leased.SubnetMask = $parts[1]
$leased.MACAddress = [string]::Join("-",$parts[2..7])
$leased.LeaseExpires = $parts[8]
$leased.Tip = $parts[9]
$leasedIPs += $leased
}
return $leasedIPs
}
$servers = Show-DHCPServers
$scopes = Get-DHCPScope -Server $servers[0].IPAddress
$rezervasyonlar = $scopes[0].Reservations
$cem = "Topkaya"