- Create a self-signed certificate with PowerShell - Thu, Aug 9 2018
- Prevent copying of an Active Directory attribute when duplicating a user account - Thu, Mar 29 2018
- Find and delete unlinked (orphaned) GPOs with PowerShell - Thu, Mar 15 2018
A pop-up window will display when you start the script. It will ask on which DHCP server you want to add the scope. This works on your server remotely or locally. Using $env:computername will add the local computer name to input automatically.
The corresponding code looks like this:
[void][System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic') $dhcpserver = [Microsoft.VisualBasic.Interaction]::InputBox("Enter DHCP server name", "DHCP server name", "$env:computername")
The entered name will be saved into the $dhcpserver variable, so we can use it later to create the scope.
The script does the same for the scope name, scope ID, start and end IP, netmask, and the router. I predefined some values such as the netmask 255.255.255.0 to speed up the process.
$scopename = [Microsoft.VisualBasic.Interaction]::InputBox("Enter Scope name", "Scope name", "") $scopeID = [Microsoft.VisualBasic.Interaction]::InputBox("Enter Scope ID like 10.1.1.0", "Scope ID", "10.") $startrange = [Microsoft.VisualBasic.Interaction]::InputBox("Enter Start IP", "Start IP", "10.") $endrange = [Microsoft.VisualBasic.Interaction]::InputBox("Enter End IP", "End IP", "10.") $subnetmask = [Microsoft.VisualBasic.Interaction]::InputBox("Enter Subnetmask", "Subnetmask", "255.255.255.0") $router = [Microsoft.VisualBasic.Interaction]::InputBox("Enter Router IP", "Router IP", "10.")
The script will then display all entered values, so you can review everything:
Write-Host Write-Host ----------Preconfigured Settings----------- -foregroundcolor "yellow" Write-Host Write-Host Server: {}{}{}{}{}{}{}{} $dhcpserver -foregroundcolor "yellow" Write-Host Scope Name: {}{}{}{} $scopename -foregroundcolor "yellow" Write-Host Scope ID: {}{}{}{}{}{} $scopeID -foregroundcolor "yellow" Write-Host IP Range: {}{}{}{}{}{} $startrange - $endrange -foregroundcolor "yellow" Write-Host Subnetmask: {}{}{}{} $subnetmask -foregroundcolor "yellow" Write-Host Router: {}{}{}{}{}{}{}{} $router -foregroundcolor "yellow" Write-Host Write-Host ---------/Preconfigured Settings----------- -foregroundcolor "yellow"
In addition, you have to type in y to continue.
$input = [Microsoft.VisualBasic.Interaction]::InputBox("Type in y to continue `n or any key to cancel...", "Create Scope", "")
If you then press the y key, the script will create the DHCP by using the Add-DHCPServer4Scope and Set-DHCPServer4OptionValue cmdlets. To exit the script at this point, you can press any other key.
if(($input) -eq "y" ) { Add-DHCPServerv4Scope -ComputerName $dhcpserver -EndRange $endrange -Name $scopename -StartRange $startrange -SubnetMask $subnetmask -State Active Set-DHCPServerv4OptionValue -ComputerName $dhcpserver -ScopeId $scopeID -Router $router Write-Host Write-Host Write-Host Created Scope $scopename on Server $dhcpserver -foregroundcolor "green" Write-Host Write-Host ---------------Settings-------------------- -foregroundcolor "green" Write-Host ------------------------------------------- -foregroundcolor "green" Write-Host Write-Host Scope Name: {}{}{} $scopename -foregroundcolor "green" Write-Host Scope ID: {}{}{}{}{} $scopeID -foregroundcolor "green" Write-Host IP Range: {}{}{}{}{} $startrange - $endrange -foregroundcolor "green" Write-Host Subnetmask: {}{}{} $subnetmask -foregroundcolor "green" Write-Host Router: {}{}{}{}{}{}{} $router -foregroundcolor "green" Write-Host Write-Host ------------------------------------------- -foregroundcolor "green" Write-Host --------------/Settings-------------------- -foregroundcolor "green" } else { exit }
The script will then show the configured settings of your new DHCP scope:
When you open the DHCP console on your server, you should now see the new scope:
This is the complete script:
# DHCPScopeCreation.ps1 # Script by Tim Buntrock # This script will create a DHCP scope based on your input # You can verify the config after you add all values, and if you confirm with "y," the scope will be created! # You can add values like DNS server, Boot options, and so on to this script, but I set options like this using Server Options. ########### Script--->START ########### # Input Box [void][System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic') $dhcpserver = [Microsoft.VisualBasic.Interaction]::InputBox("Enter DHCP server name", "DHCP server name", "$env:computername") $scopename = [Microsoft.VisualBasic.Interaction]::InputBox("Enter Scope name", "Scope name", "") $scopeID = [Microsoft.VisualBasic.Interaction]::InputBox("Enter Scope ID like 10.1.1.0", "Scope ID", "10.") $startrange = [Microsoft.VisualBasic.Interaction]::InputBox("Enter Start IP", "Start IP", "10.") $endrange = [Microsoft.VisualBasic.Interaction]::InputBox("Enter End IP", "End IP", "10.") $subnetmask = [Microsoft.VisualBasic.Interaction]::InputBox("Enter Subnetmask", "Subnetmask", "255.255.255.0") $router = [Microsoft.VisualBasic.Interaction]::InputBox("Enter Router IP", "Router IP", "10.") Write-Host Write-Host ----------Preconfigured Settings----------- -foregroundcolor "yellow" Write-Host Write-Host Server: {}{}{}{}{}{}{}{} $dhcpserver -foregroundcolor "yellow" Write-Host Scope Name: {}{}{}{} $scopename -foregroundcolor "yellow" Write-Host Scope ID: {}{}{}{}{}{} $scopeID -foregroundcolor "yellow" Write-Host IP Range: {}{}{}{}{}{} $startrange - $endrange -foregroundcolor "yellow" Write-Host Subnetmask: {}{}{}{} $subnetmask -foregroundcolor "yellow" Write-Host Router: {}{}{}{}{}{}{}{} $router -foregroundcolor "yellow" Write-Host Write-Host ---------/Preconfigured Settings----------- -foregroundcolor "yellow" Write-Host Write-Host Write-Host Write-Host Type in y to continue or any key to cancel... Write-Host $input = [Microsoft.VisualBasic.Interaction]::InputBox("Type in y to continue `n or any key to cancel...", "Create Scope", "") if(($input) -eq "y" ) { Add-DHCPServerv4Scope -ComputerName $dhcpserver -EndRange $endrange -Name $scopename -StartRange $startrange -SubnetMask $subnetmask -State Active Set-DHCPServerv4OptionValue -ComputerName $dhcpserver -ScopeId $scopeID -Router $router Write-Host Write-Host Write-Host Created Scope $scopename on Server $dhcpserver -foregroundcolor "green" Write-Host Write-Host ---------------Settings-------------------- -foregroundcolor "green" Write-Host ------------------------------------------- -foregroundcolor "green" Write-Host Write-Host Scope Name: {}{}{} $scopename -foregroundcolor "green" Write-Host Scope ID: {}{}{}{}{} $scopeID -foregroundcolor "green" Write-Host IP Range: {}{}{}{}{} $startrange - $endrange -foregroundcolor "green" Write-Host Subnetmask: {}{}{} $subnetmask -foregroundcolor "green" Write-Host Router: {}{}{}{}{}{}{} $router -foregroundcolor "green" Write-Host Write-Host ------------------------------------------- -foregroundcolor "green" Write-Host --------------/Settings-------------------- -foregroundcolor "green" } else { exit }
The script is also available on TechNet. When I improve the script, I will update it there.
If you don’t want to work with input boxes, you can also define all values directly in the script:
Subscribe to 4sysops newsletter!
# dhcp scope variables $scopename = "MyScope" $startrange = "10.1.1.2" $endrange = "10.1.1.250" $subnetmask = "255.255.255.0" $scopeID = "10.1.1.0" $router = "10.1.1.254" # Creating scope Add-DHCPServerv4Scope -EndRange $endrange -Name $scopename -StartRange $startrange -SubnetMask $subnetmask -State Active # Adding router Set-DHCPServerv4OptionValue -ScopeId $scopeID -Router $router
So basically, you made a GUI for 2 lines of PowerShell?
I think this post should’ve been called ‘How to make a PowerShell-GUI’ (or inputbox really). No offense, it was nice to read.