I have the following script to output scope data from my DHCP server. In short, I want all the scope IDs and names, and then all the configured options for those scopes. This script seems to output to the console just fine:
$scopelist = Get-DhcpServerv4Scope -ComputerName localhost | Select-Object ScopeID, Name foreach($s in $scopelist){ Write-Host $s Get-DhcpServerv4OptionValue –ScopeID $s.ScopeID | Select-Object OptionID, Name, Value }
The output looks like this:
@{ScopeId=10.1.1.0; Name=Testscope 1} OptionID Name Value -------- ---- ----- 51 Lease {691200} 3 Router {10.1.1.1} 15 DNS Domain Name {adatum.com} 6 DNS Servers {192.168.1.200} @{ScopeId=10.2.2.0; Name=Testscope 2} 51 Lease {86400} 3 Router {10.2.2.1} 15 DNS Domain Name {foo.bar} 6 DNS Servers {10.10.10.10} 44 WINS/NBNS Servers {10.10.10.11} 46 WINS/NBT Node Type {0x08} @{ScopeId=10.3.3.0; Name=Testscope 3} 51 Lease {691200}
The problem is, I can’t export this data to a file containing data from both cmdlets. I tried Export-CSV output.csv -Append in the foreach loop:
Write-Host $s | Export-CSV output.csv -Append Get-DhcpServerv4OptionValue –ScopeID $s.ScopeID | Select-Object OptionID, Name, Value | Export-CSV output.csv -Append
, but then output.csv only contains the output from Get-DhcpServerv4OptionValue. I also tried “.\script.ps1 >output.csv”, it doesn’t work either. Also it would save a lot of time in Excel if I could get the data in proper table format, with ScopeID and scope name defined for all rows, like this:
ScopeID, Name,OptionID, Name, Value
10.2.2.0, Testscope 2, 3, Router, 10.2.2.1
and so on
I could not find examples how to do this, all Export-CSV help examples just take input from a single cmdlet or object. Should I make a new object first with my data? I have no idea how to pass the values to New-Object.