Add-DhcpServerV4Scope import-csv LeaseDuration Time Value Entry

Nicholas J. Conger 1 Reputation point
2021-10-06T04:13:46.707+00:00

Hello,

We're trying to export about 450 scopes and re-create them on another DHCP Server importing the values from a CSV and running the Add-DhcpServerV4Scope cmdlet.

Everything works perfectly except the LeaseDuration value.

See the value is set to "day.hrs:mins:secs" format.

Running the script gets the error "Add-DhcpServerv4Scope : Cannot process argument transformation on parameter 'LeaseDuration'. Cannot convert null to type "System.TimeSpan"."

Does anybody have a value that the time can be inputted into a CSV that the Add-DhcpServerV4Scope cmdlet can use to set the scope value to the specified LeaseDuration.

Windows DHCP
Windows DHCP
Windows: A family of Microsoft operating systems that run across personal computers, tablets, laptops, phones, internet of things devices, self-contained mixed reality headsets, large collaboration screens, and other devices.DHCP: Dynamic Host Configuration Protocol (DHCP). A communications protocol that lets network administrators manage centrally and automate the assignment of Internet Protocol (IP) addresses in an organization's network.
1,023 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,381 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Limitless Technology 39,371 Reputation points
    2021-10-06T11:20:00.187+00:00

    Hello,

    I am not sure how exactly looks like the csv that you are having prepared, but to import multiple DHCP scopes to a DHCP server. Some settings needs to be added on top level. For example DNS servers.

    Required header in CSV:
    name;description;startrange;endrange;subnetmask;scopeid;router

    $dhcpserver = "1.1.1.1"
    $scopes = Import-Csv -Path dhcp.csv -Delimiter ";"
    foreach ($scope in $scopes)
    {
    $name = $scope.name
    $description = $scope.description
    Write-Output "Creating scope $name"
    Add-DhcpServerv4Scope -ComputerName $dhcpserver -Name "$name" -Description "$description" -StartRange $scope.startrange -EndRange $scope.endrange -SubnetMask $scope.subnetmask -State Active -LeaseDuration 1.00:00:00
    Set-DhcpServerv4OptionValue -Router $scope.router -ScopeId $scope.scopeid -ComputerName $dhcpserver
    }


    --If the reply is helpful, please Upvote and Accept as answer--

    0 comments No comments

  2. Rich Matheisen 45,091 Reputation points
    2021-10-06T14:46:57.573+00:00

    Is it safe to assume that you're using a variable as the value for the lease duration? The error says that the variable either doesn't exist or that it contains a $null value.

    If you can't find where the problem lies, try adding Set-PSDebug -Strict at the the beginning of your script. If you try to use an undeclared variable you'll get an error like "The variable '$NewVar' cannot be retrieved because it has not been set." along with the line number in the script.

    It may simply be a misspelling of the variable name (or a property name).

    0 comments No comments