如何自定义"DHCP All scopes and leases"脚本?



大家好,

我想修改下面的脚本。

$DHCPServers = Get-DhcpServerInDC
$R = @()
ForEach ($Server in $DHCPServers)
{ $S = $Server.DnsName
$Scopes = Get-DhcpServerv4Scope –ComputerName $S
ForEach ($Scope in $Scopes) 
{
$AllScopes = $Scope.ScopeID.IPAddressToString
$Results = Get-DhcpServerv4Lease -ComputerName $S -ScopeId $AllScopes -AllLeases | Select-Object @{Name="DHCPServer"; Expression={$S}},ScopeId,IPAddress,AddressState,ClientId,ClientType,HostName,Description,DnsRegistration,DnsRR,LeaseExpiryTime,NapCapable,NapStatus,PolicyName,ProbationEnds,ServerIP,PSComputerName
$R += $Results
}

}
$R | Sort-Object -Property DHCPServer,ScopeId,IPAddress | Out-GridView
  1. 我如何在输出中添加一个额外的列,显示"Scope IdName";就在ScopeId列之后?前| DHCPServer | ScopeId |ScopeIdName| IPAddress |

  2. 按IP地址排序时,192.168.1.19前面会显示192.168.1.100等IP。如何让排序对象命令识别。100在。19之后?大多数以单位数或双位数结尾的地址被正确排序。

  3. 这不是必需的,但如果可以缩短服务器DnsName,将不胜感激。从dc1.orgination.org到DC1

我尝试在输出中添加一个额外的表达式,这样我就可以添加"ScopeIdName"列,但收到一个错误。我尝试的表达式是....

@{Name="ScopeName"; Expression={$Scopes.Name}}

感谢您的宝贵时间。

这几乎是在黑暗中拍摄,我没有访问您用于测试的cmdlet,但可能能够帮助您完成3个要求。

$sortproperties = @(
'DHCPServer'
'ScopeId'
# lexicographical sorting
{ '{0:D3}.{1:D3}.{2:D3}.{3:D3}' -f @($_.GetAddressBytes()) }
)
Get-DhcpServerInDC | ForEach-Object {
foreach($scope in Get-DhcpServerv4Scope -ComputerName $_.DnsName) {
$getDhcpServerv4LeaseSplat = @{
ComputerName = $_.DnsName
ScopeId      = $scope.ScopeID.IPAddressToString
AllLeases    = $true
}
foreach($lease in Get-DhcpServerv4Lease @getDhcpServerv4LeaseSplat) {
[pscustomobject]@{
# it is possible that `$_` already has a `.Name` property here
# and splitting on `DnsName` might not be needed
DHCPServer      = $_.DnsName.Split('.')[0]
ScopeId         = $lease.ScopeId
# Assuming `$scope` object has a `.Name` property:
ScopeName       = $scope.Name
# not sure if this is already an `ipaddress` instance or not,
# using `-as` just in case (might not be needed):
IPAddress       = $lease.IPAddress -as [ipaddress]
AddressState    = $lease.AddressState
ClientId        = $lease.ClientId
ClientType      = $lease.ClientType
HostName        = $lease.HostName
Description     = $lease.Description
DnsRegistration = $lease.DnsRegistration
DnsRR           = $lease.DnsRR
LeaseExpiryTime = $lease.LeaseExpiryTime
NapCapable      = $lease.NapCapable
NapStatus       = $lease.NapStatus
PolicyName      = $lease.PolicyName
ProbationEnds   = $lease.ProbationEnds
ServerIP        = $lease.ServerIP
PSComputerName  = $lease.PSComputerName
}
}
}
} | Sort-Object $sortproperties | Out-GridView

谢谢圣地亚哥!在你的帮助下,我能够让脚本工作。

$sortproperties = @(
'DHCPServer'
'ScopeId'
# lexicographical sorting
{ '{0:D3}.{1:D3}.{2:D3}.{3:D3}' -f @($_.GetAddressBytes()) }
)
Get-DhcpServerInDC | ForEach-Object {
foreach($scope in Get-DhcpServerv4Scope -ComputerName $_.DnsName) {
$getDhcpServerv4LeaseSplat = @{
ComputerName = $_.DnsName
ScopeId      = $scope.ScopeID.IPAddressToString
AllLeases    = $true
}
foreach($lease in Get-DhcpServerv4Lease @getDhcpServerv4LeaseSplat) {
[pscustomobject]@{
# it is possible that `$_` already has a `.Name` property here
# and splitting on `DnsName` might not be needed
DHCPServer      = $_.DnsName.Split('.')[0]
ScopeId         = $lease.ScopeId
# Assuming `$scope` object has a `.Name` property:
ScopeName       = $scope.Name
# not sure if this is already an `ipaddress` instance or not,
# using `-as` just in case (might not be needed):
IPAddress       = $lease.IPAddress -as [ipaddress]
AddressState    = $lease.AddressState
ClientId        = $lease.ClientId
ClientType      = $lease.ClientType
HostName        = $lease.HostName
Description     = $lease.Description
DnsRegistration = $lease.DnsRegistration
DnsRR           = $lease.DnsRR
LeaseExpiryTime = $lease.LeaseExpiryTime
NapCapable      = $lease.NapCapable
NapStatus       = $lease.NapStatus
PolicyName      = $lease.PolicyName
ProbationEnds   = $lease.ProbationEnds
ServerIP        = $lease.ServerIP
PSComputerName  = $lease.PSComputerName
}
}
}
} | Sort-Object -Property DHCPServer,ScopeId,{ [Version]$_.IPAddress.IPAddressToString } | Out-GridView -Title "All Scopes and Leases for All DHCP Servers"

最新更新