Link to home
Start Free TrialLog in
Avatar of Member_2_2473503
Member_2_2473503Flag for Philippines

asked on

PowerShell script to set DHCP policies

Needs to work on Server 2012 and 2016

I am working on a PowerShell script that will
1) Configure vendor classes in DHCP - Working
2) Create policies based on these classes - Working
3) Set specific options (66 and 67) for each policy - Not working

The complete script is
Add-WindowsFeature -Name DHCP –IncludeManagementTools
Import-Module DhcpServer

$Server	= Read-Host -Prompt 'Enter the DHCP server name'
$Scope	= Read-Host -Prompt 'Enter the project network address (10.222.xxx.0)'
$WDSvr	= Read-Host -Prompt 'Enter the IP of the server hosting Deployment Shares'

$VendorClassUEFIx64 = @{
	Name = "PXEClient UEFI (x64)"
	Description = "PXEClient UEFI (x64)"
	Type = "Vendor"
	Data = "PXEClient:Arch:00007"
	}
$VendorClassUEFIx86 = @{
	Name = "PXEClient UEFI (x86)"
	Description = "PXEClient UEFI (x86)"
	Type = "Vendor"
	Data = "PXEClient:Arch:00006"
	}
$VendorClassBIOS = @{
	Name = "PXEClient BIOS (x86 & x64)"
	Description = "PXEClient BIOS (x86 & x64)"
	Type = "Vendor"
	Data = "PXEClient:Arch:00000"
	}
	
Add-DhcpServerv4Class @VendorClassUEFIx64 -ComputerName $Server
Add-DhcpServerv4Class @VendorClassUEFIx86 -ComputerName $Server
Add-DhcpServerv4Class @VendorClassBIOS -ComputerName $Server

Add-DhcpServerv4Policy -Name "PXEClient UEFI (x64)" -ScopeId $Scope -Condition OR -VendorClass EQ,"PXEClient UEFI (x64)*"
Add-DhcpServerv4Policy -Name "PXEClient UEFI (x86)" -ScopeId $Scope -Condition OR -VendorClass EQ,"PXEClient UEFI (x86)*"
Add-DhcpServerv4Policy -Name "PXEClient BIOS" -ScopeId $Scope -Condition OR -VendorClass EQ,"PXEClient BIOS (x86 & x64)*"

Set-DhcpServerv4OptionValue -ComputerName $Server -ScopeId $Scope -DhcpServerv4Policy "PXEClient BIOS" -OptionId 66 -Value $WDSvr
Set-DhcpServerv4OptionValue -ComputerName $Server -ScopeId $Scope -DhcpServerv4Policy "PXEClient BIOS" -OptionId 67 -Value "boot\x86\wdsnbp.com"
Set-DhcpServerv4OptionValue -ComputerName $Server -ScopeId $Scope -DhcpServerv4Policy "PXEClient UEFI (x64)*" -OptionId 66 -Value $WDSvr
Set-DhcpServerv4OptionValue -ComputerName $Server -ScopeId $Scope -DhcpServerv4Policy "PXEClient UEFI (x64)*" -OptionId 67 -Value "boot\x64\wdsmgfw.efi"
Set-DhcpServerv4OptionValue -ComputerName $Server -ScopeId $Scope -DhcpServerv4Policy "PXEClient UEFI (x86)*" -OptionId 66 -Value $WDSvr
Set-DhcpServerv4OptionValue -ComputerName $Server -ScopeId $Scope -DhcpServerv4Policy "PXEClient UEFI (x86)*" -OptionId 67 -Value "boot\x86\wdsmgfw.efi"

Open in new window


This is the section that is not working, I have tried multiple ways of doing this and I know the script code is wrong at the moment
Set-DhcpServerv4OptionValue -ComputerName $Server -ScopeId $Scope -DhcpServerv4Policy "PXEClient BIOS" -OptionId 66 -Value $WDSvr
Set-DhcpServerv4OptionValue -ComputerName $Server -ScopeId $Scope -DhcpServerv4Policy "PXEClient BIOS" -OptionId 67 -Value "boot\x86\wdsnbp.com"
Set-DhcpServerv4OptionValue -ComputerName $Server -ScopeId $Scope -DhcpServerv4Policy "PXEClient UEFI (x64)*" -OptionId 66 -Value $WDSvr
Set-DhcpServerv4OptionValue -ComputerName $Server -ScopeId $Scope -DhcpServerv4Policy "PXEClient UEFI (x64)*" -OptionId 67 -Value "boot\x64\wdsmgfw.efi"
Set-DhcpServerv4OptionValue -ComputerName $Server -ScopeId $Scope -DhcpServerv4Policy "PXEClient UEFI (x86)*" -OptionId 66 -Value $WDSvr
Set-DhcpServerv4OptionValue -ComputerName $Server -ScopeId $Scope -DhcpServerv4Policy "PXEClient UEFI (x86)*" -OptionId 67 -Value "boot\x86\wdsmgfw.efi"

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of David Sankovsky
David Sankovsky
Flag of Israel image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Member_2_2473503

ASKER

Hello David,

You may not work with Windows anymore but you got this right... worked perfectly

Replacement code for the last section is
Set-DhcpServerv4OptionValue -OptionId 66 -Value $WDSvr -ComputerName $Server -ScopeId $Scope -PolicyName "PXEClient BIOS"
Set-DhcpServerv4OptionValue -OptionId 67 -Value "boot\x86\wdsnbp.com" -ComputerName $Server -ScopeId $Scope -PolicyName "PXEClient BIOS" 
Set-DhcpServerv4OptionValue -OptionId 66 -Value $WDSvr -ComputerName $Server -ScopeId $Scope -PolicyName "PXEClient UEFI (x64)"
Set-DhcpServerv4OptionValue -OptionId 67 -Value "boot\x64\wdsmgfw.efi" -ComputerName $Server -ScopeId $Scope -PolicyName "PXEClient UEFI (x64)"
Set-DhcpServerv4OptionValue -OptionId 66 -Value $WDSvr -ComputerName $Server -ScopeId $Scope -PolicyName "PXEClient UEFI (x86)"
Set-DhcpServerv4OptionValue -OptionId 67 -Value "boot\x86\wdsmgfw.efi" -ComputerName $Server -ScopeId $Scope -PolicyName "PXEClient UEFI (x86)"

Open in new window


-eb
Glad I could help :)
I believe your original would have worked if you used the correct parameter name, -PolicyName instead of -DhcpServerv4Policy.
Position should never matter with named parameters.  In PowerShell position of arguments only matters when you don't supply the parameter name (and not all parameters can be used without a name).
Hello footech,
 
Yes it works if I replace DHCPServerv4Policy with PolicyName and leave it in the original order.