检索 DHCP 范围

Retrieving DHCP Scopes

我的脚本在 运行 本地时有效,我试图从远程服务器获取所有 DHCP 范围信息,我收到脚本下方的错误

$A = "TestDH01"
ForEach ($B in $A) {

Get-DHCPServerv4Lease -ScopeID $_.ScopeID -AllLeases | where 
{$_.AddressState -like '*Reservation'}

} Select-Object ScopeId,IPAddress,HostName,ClientID,AddressState | ExportCsv "\TermServer\d$\New\Steve$($A)-Reservations1.csv" -NoTypeInformation

Get-DhcpServerv4Lease : Cannot validate argument on parameter 'ScopeId'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again. At line:4 char:36 + Get-DHCPServerv4Lease -ScopeID $.ScopeID -AllLeases | where {$. ... + ~~~~~~~~~~ + CategoryInfo : InvalidData: (:) [Get-DhcpServerv4Lease], ParameterBindingValidationException + FullyQualifiedErrorId : ParameterArgumentValidationError,Get-DhcpServerv4Lease

我假设您正在寻找这样的东西:

#Get all DHCP Servers
$ServerList = Get-DhcpServerInDC | select IPADdress, DNSName
foreach ($server in $serverlist)
{
#Get the scopes from each server
    Get-DHCPServerv4Scope -ComputerName $server.IPAddress | select ScopeID | 
#Get the lease information for each scope
    ForEach-Object {Get-DHCPServerv4Lease -ScopeId $_.ScopeId -ComputerName $Server.DNSName -AllLeases  | 
        where {$_.AddressState -like "*Reservation"} | Select-Object ScopeId,IPAddress,HostName,ClientID,AddressState }
}