Getting DHCP Server scope from multiple servers

Hello Team,

Hope you all are doing good… back again :slight_smile:

This time I am trying to get the DHCP server scope from 2 servers and trying to collect the report into a csv file;
I am using following script;

$ab = Get-DhcpServerv4Scope
$a = $ab[6,8]
foreach($b in $a)
{
Get-DhcpServerv4Scope -ComputerName $b.dnsname | Export-Csv c:\report.csv
}

This script is giving me the output from only one server and that too randomly. I cross verified it using
Get-Help Get-DhcpServerv4Scope -full, found that ComputerName parameter is accepting a single value.
Kindly help me with the workaround.

Thanks

Hey,

Your problem is not with Get-DhcpServer4Scope, but with Export-Csv, you need to use the -append parameter. From the Help:

"-Append

Indicates that this cmdlet adds the CSV output to the end of the specified file. Without this parameter, Export-CSV replaces the file contents without warning.

This parameter was introduced in Windows PowerShell 3.0."

Collect all of the results and then export to a CSV:

$ab = Get-DhcpServerv4Scope
$a = $ab[6,8]
$results = foreach($b in $a) {
    Get-DhcpServerv4Scope -ComputerName $b.dnsname 
}

$results | Export-Csv c:\report.csv -NoTypeInformation