Create a CSV Report of DHCP Scope Options


You're running Windows Server 2012 DHCP. Alternatively, you might be running Windows Server 2012 R2 DHCP. Whatever the flavour, the point is this - you have DHCP cmdlets at your disposal! Oorah!

Now, you also have a lot of scopes and you also have a lot of scopes with there very own scope options. You can run the following to list all of those scope-specific option values:

Get-DhcpServerv4Scope | Get-DhcpServerv4OptionValue

 

Easy enough, you might think. There's only one problem: the option value output objects aren't associated with a particular scope. That's a bummer. A bummer that's easily fixed though:

Add-Content -Value "SCOPE,OPTIONID, NAME, TYPE, VALUE" -Path .\Windows_Scope_Options.csv

$Scopes = Get-DhcpServerv4Scope

foreach ($Scope in $Scopes) {

$Options = Get-DhcpServerv4OptionValue -ScopeId $Scope.ScopeId.IPAddressToString -All | Sort-Object -Descending -Property OptionId

for ($i = ($Options.Count -1); $i -gt -1; $i--) {

Add-Content -Value "$($Scope.ScopeId.IPAddressToString),$($Options[$i].OptionId),$($Options[$i].Name),$($Options[$i].Type),$($Options[$i].Value)" -Path .\Windows_Scope_Options.csv

}

}

 

So, we start out by adding a CSV header to a file with Add-Content. I've always liked this method to construct a CSV file. It's a method I used before the -Append switch was added to Export-CSV in v3 of PowerShell.

Next, assign our scopes from the local DHCP server to a varible. Now, loop through those scopes and for each scope capture its option information in a variable called $Options.

Spin up another loop, this time with the for statement. $i will be the count of the number of scope options found. We keep looping while $i is greater than 0 and decrement $i by one on each completed loop.

for ($i = ($Options.Count -1); $i -gt 0; $i--)

 

The following script block is executed per iteration in the loop, i.e. per scope option.

Add-Content -Value "$($Scope.ScopeId.IPAddressToString),$($Options[$i].OptionId),$($Options[$i].Name),$($Options[$i].Type),$($Options[$i].Value)" -Path .\Windows_Scope_Options.csv

 

This writes a line to our CSV file that contains choice information about our current scope option.

The current scope option is accessed from the array of scope options by using the current value of $i as the array element. We then pick out certain properties, e.g.:

$($Options[$i].OptionId)

 

And, the best bit of all, because we can use $Scope to represent the current scope iteration from our scopes loop (remember that?) we can append this scope information to the string representing the scope options.

$($Scope.ScopeId.IPAddressToString)

 

Here's what a sample output file looks like:

 

Comments (9)

  1. turbomcp says:

    thanks
    very nice work

  2. Jefferson says:

    Exactly what I was trying to achieve - Thanks!

  3. Philip Horan says:

    I receive error:
    At line:13 char:46
    + for ($i = ($Options.Count –1); $i -gt -1; $i—) {
    + ~
    You must provide a value expression following the '—' operator.
    + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : ExpectedValueExpression

    How does working script look?

    1. Philip Horan says:

      [code]
      $Scopes = Get-DhcpServerv4Scope | Get-DhcpServerv4OptionValue

      Add-Content -Value "SCOPE,OPTIONID, NAME, TYPE, VALUE" -Path .\Windows_Scope_Options.csv

      foreach ($Scope in $Scopes) {

      $Options = Get-DhcpServerv4OptionValue -ScopeId $Scope.ScopeId.IPAddressToString -All | Sort-Object -Descending -Property OptionId

      for ($i = ($Options.Count –1); $i -gt -1; $i—0) {

      Add-Content -Value "$($Scope.ScopeId.IPAddressToString),$($Options[$i].OptionId),$($Options[$i].Name),$($Options[$i].Type),$($Options[$i].Value)" -Path .\Windows_Scope_Options.csv

      }

      }
      [/code]

      The code runs and creates the .csv file but does not output expected results. Error displayed.

      [Error]
      Get-DhcpServerv4OptionValue : 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 C:\scripts\dhcp\dhcp.ps1:8 char:49
      + $Options = Get-DhcpServerv4OptionValue -ScopeId $Scope.ScopeId.IPAddressToString ...
      + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo : InvalidData: (:) [Get-DhcpServerv4OptionValue], ParameterBindingValidationException
      + FullyQualifiedErrorId : ParameterArgumentValidationError,Get-DhcpServerv4OptionValue
      [/Error]

  4. Suncode says:

    Thanks for sharing.

    1. Manfred Preissner says:

      Thanks for the great IDEA
      As i got the same error like Philip Horan
      I want to share the idea that we also can do it like this:
      PS i used Tab as seperator
      ###########################################
      $path1 = "c:\temp\All_Scope_Options.csv"
      Add-Content -Value "SCOPE `t OPTIONID `t NAME `t TYPE `t VALUE" -Path $path1
      $Scopes = Get-DhcpServerv4Scope
      foreach ($Scope in $Scopes) {$Options = Get-DhcpServerv4OptionValue -ScopeId $Scope.ScopeId.IPAddressToString -All | Sort-Object -Descending -Property OptionId;foreach ($I in $Options ) {Add-Content -Value "$($Scope.ScopeId.IPAddressToString) `t $($I.OptionId) `t $($I.Name) `t $($I.Type) `t $($i.Value)" -Path $path1}}
      ##################################################

      1. Manfred Preissner says:

        And now the real PS Way for a Script to Run so you can keep on tracking Changes if necessary ... sorry i couldn't stop it 🙂

        $D = get-date -uformat "%d.%m.%Y"
        $path1 = "c:\temp\All_Scope_Options_$D.csv"
        $Scopes = Get-DhcpServerv4Scope
        foreach ($Scope in $Scopes) {Get-DhcpServerv4OptionValue -ScopeId $Scope.ScopeId.IPAddressToString -All | Sort-Object -Descending -Property OptionId | select @{Expression={$Scope.ScopeId.IPAddressToString};Label="SCOPE"},OptionId,Name,Type,UserClass,PolicyName,@{Expression={$_.Value};Label="Value"} | export-csv -Path $path1 -append }

        1. Greg Marcoux says:

          This was helpful, thanks so much.

        2. GMB says:

          Brilliant, thanks you for this!

Skip to main content