Create DHCP Scope with Variables?


I need to Create a lot of DHCP scopes on diffrent server, i have tried to create a script that asks for the 3 first octets of the subnet, but i can´t get it to work as i want, does anyone has any idea how i can do it

I have done it this way ( the scripts ask for a lot of more info, but i have skipped the working parts)

$Subnet = Read-host "Enter Subnet for Office (ex. 192.25.58)"

Add-DhcpServerv4Scope -Name '$Office' -StartRange '$Subnet.100' -EndRange '$Subnet.150 '-SubnetMask -Description '$office'
Set-DhcpServerv4OptionValue -DnsServer, -DnsDomain "int.domain.local" -Router $Subnet.2

The Scope details are always the same , the only thing that is changing is the 3 first octets… (the 2nd and 3rd octets)

Single and double qoutes work differently. Double qoutes are expanded and will resolve variables. Single qoutes are literal, what you type is what is used.

PS C:\Users\rasim> $test = 'foo'

PS C:\Users\rasim> 'Test value is $test'
Test value is $test

PS C:\Users\rasim> "Test value is $test"
Test value is foo

There are a couple of other small things such as the DNS servers is a string array, not a string, so the values are separated by commas. Additionally, use splatting to make it a bit easier to read:

$Office = Read-Host "What office?"
$Subnet = Read-host “Enter Subnet for Office (ex. 192.25.58)”

$addParams = @{
    Name         = "$Office"
    StartRange   = "$Subnet.100" 
    EndRange     = "$Subnet.150"
    SubnetMask   = ""
    Description  = "$Office"

Add-DhcpServerv4Scope @addParams

$setParams = @{
    DnsServer = "","" 
    DnsDomain = “int.domain.local” 
    Router    = "$Subnet.2"

Set-DhcpServerv4OptionValue @setParams

Thanks for your incredible help :slight_smile:

It worked like a charm, i need to learn little more about this !