2
votes

My script works when run locally, I am trying to get all DHCP scope info from remote servers, I receive the error below the script

$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

1
You're not piping anything into $_ so $_.ScopeID will always be null. Do something like -ScopeID $B - Nick
That at least got me a different error, 'ScopeID' cannot convert value "TestDH01" to type "System.Net.IPAddress". Error: "An invalid IP address address was specified" - Slyons
The ScopeID parameter requires an IPv4 address, and specifies the scope in which address leases are being retrieved. - Nick

1 Answers

1
votes

I'm assuming you're looking for something like this:

#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 }
}