Scripting DHCP Policies

DevilWAHDevilWAH Member Posts: 2,997 ■■■■■■■■□□
Hi,

I am trying to add about 500 mac address to a policy,for now all I want to do is create the policy with the list of MAc address and disable it, so it is ready for use later. In fact I want to create a number of policies but don't yet have ip ranges or options defined.

So i tried the following script


param( 
 
     
    [parameter(Mandatory=$true)] 
    [string]  
    $InputFileName
     
 
) 


$filename = $InputFileName 


$maclist = @()


    $operator = "EQ" 
    $condition = "OR" 
   
 
# Attempt to read the entire list of MAC-addresses into an array. 
# Finally, The array $maclist would look like the following for an allow list - 
# @("EQ", "mac-address1", "EQ", "mac-address2", "EQ", "mac-address3" ..) 
foreach ($mac in $filecontent) 
{ 
    $maclist += $operator 
    if($mac -ne "") 
    { 
        $maclist += $mac 
     } 
} 
 
try 
{ 
    Add-DhcpServerv4Policy -Name "laptops" -Description "laptop-mac" -ScopeId "10.1.1.1" -Condition $condition -MacAddress $maclist -ErrorAction Stop 
} 


catch{throw}


 
Exit

which should ask me for the input file which is a list of MAc address and create the policy for me.

How ever I get an error
Add-DhcpServerv4Policy : Please specify at least one of the optional parameters: VendorClass, UserClass, MacAddress, ClientId, RelayAgent, CircuitId, RemoteId, SubscriberId.
At C:\temp\add policy.ps1:35 char:5
+     Add-DhcpServerv4Policy -Name "laptops" -Description "laptop-mac" -ScopeId "1 ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (PS_DhcpServerv4Policy:root/Microsoft/...pServerv4Policy) [Add-DhcpServerv4Policy], CimException
    + FullyQualifiedErrorId : WIN32 87,Add-DhcpServerv4Policy

any help in getting this to work would be greatly appreciated.

Cheers
  • If you can't explain it simply, you don't understand it well enough. Albert Einstein
  • An arrow can only be shot by pulling it backward. So when life is dragging you back with difficulties. It means that its going to launch you into something great. So just focus and keep aiming.

Comments

  • EveryoneEveryone Member Posts: 1,661
    You don't have anything asking for the input file. $filename = $InputFileName but $InputFileName is never defined. You need something like:
    $InputFileName = Read-Host "Please enter the filename to process:"

    I don't use try/catch/throw in my scripts, but I don't think that's going to work as written even if you fix the file input. You have $maclist which is an array, so just having $maclist as an argument for the cmdlet isn't going to work as it would return the entire array. You want to just return a single value, so you need a For Each statement to process each entry in the array list.
  • hackmerhackmer Users Awaiting Email Confirmation Posts: 44 ■■□□□□□□□□
    DevilWAH wrote: »
    any help in getting this to work would be greatly appreciated.

    Cheers
    I think, it is just piece of this script
  • EveryoneEveryone Member Posts: 1,661
    ^^ That makes more sense. Having the whole script and following the usage of it, should work. ;)
  • DevilWAHDevilWAH Member Posts: 2,997 ■■■■■■■■□□
    hackmer wrote: »
    I think, it is just piece of this script

    You are indeed correct, but the whole script is not what I need. But don't worry if I get it to work I will be sure to put a reference to the original script here. :^)
    • If you can't explain it simply, you don't understand it well enough. Albert Einstein
    • An arrow can only be shot by pulling it backward. So when life is dragging you back with difficulties. It means that its going to launch you into something great. So just focus and keep aiming.
  • DevilWAHDevilWAH Member Posts: 2,997 ■■■■■■■■□□
    Cheers for the help guys

    in the end I just removed the last bit of the full script that sets the IP range, and worked perfect :) now just got another 20 or so subnets to run through :) full script below

    Script Scope based Link Layer filtering using DHCP Policies
    • If you can't explain it simply, you don't understand it well enough. Albert Einstein
    • An arrow can only be shot by pulling it backward. So when life is dragging you back with difficulties. It means that its going to launch you into something great. So just focus and keep aiming.
Sign In or Register to comment.