Hello, I am wondering if any of the powershell gurus out there can see what I’m doing wrong with my script. when I take the output and put it in without the variable, it works fine.

$path = "C:\scripts\DHCP-Policy-Macs.csv"
$csv = Import-csv -path $path
$MA = "EQ"

foreach($line in $csv)
{
     $MA = $MA + "," + $line.EQ
}
Trim($MA)
Set-DhcpServerv4Policy -ScopeId X.X.X.X -Name "<PolicyName>" -MacAddress $MA

I get this error:

Set-DhcpServerv4Policy : One or more conditions specified is missing the operator EQ or NE. For Fqdn criteria, operators ISL and INSL can also be used.
At line:12 char:1
+ Set-DhcpServerv4Policy -ScopeId X.X.X.X -Name "<PolicyName>" -Condition Or -MacAdd ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo         : InvalidArgument: (UIU-AD2:root/Microsoft/...pServerv4Policy) [Set-DhcpServerv4Policy], CimException
    + FullyQualifiedErrorId : WIN32 87,Set-DhcpServerv4Policy

Before I write to the set-dhcpserverv4policy, I did a write-host $MA and this is what it shows:

 EQ,f4-96-34-1f-73-6d,14-ab-c5-de-7d-a3,28-16-ad-c8-a8-f2,c8-21-58-fa-ae-92,14-ab-c5-49-77-bc,98-54-1b-46-89-58,98-54-1b-45-06-55,98-54-1b-45-00-9c,98-54-1b-44-c7-e4,68-0
7-15-56-a7-b1,f4-8c-50-3e-a9-86,f4-8c-50-3e-c7-72,f4-8c-50-3e-c7-f4,8c-70-5a-6e-6f-00,d8-fc-93-58-5f-d9,d8-fc-93-54-17-df,e4-a7-a0-08-61-a5,a4-34-d9-a3-a2-96,08-11-96-38-
25-14,08-11-96-22-b4-2c,e4-b3-18-3c-d2-be,8c-70-5a-5c-1b-04,e4-b3-18-32-a9-6a,e4-b3-18-2a-b9-ee,e4-b3-18-3c-d2-8c,e4-b3-18-3c-d2-aa,44-85-00-4e-24-74,30-52-cb-dd-6a-1c,10
-02-b5-9c-f6-b7,10-02-b5-9c-f7-39,10-02-b5-9c-f7-7a,10-02-b5-96-f8-89,10-02-b5-6c-d7-1b,dc-53-60-d4-b8-e5,80-00-0b-d1-e5-c0,d8-fc-93-54-8e-3b,ac-fd-ce-8e-cd-75,d8-fc-93-5
4-27-e3,ac-fd-ce-54-cf-54,c0-33-5e-31-72-a9,d8-fc-93-56-91-04,d8-fc-93-56-3b-cd,ac-fd-ce-25-79-98,d8-fc-93-58-5f-d4,34-02-86-75-c5-dd,d8-fc-93-53-ff-3e,d8-fc-93-56-3d-2b,
d8-fc-93-54-47-78,d8-fc-93-56-65-99,d8-fc-93-58-c3-2a,d8-fc-93-58-2d-a2,d8-fc-93-54-c0-77,d8-fc-93-54-42-7d,d8-fc-93-54-14-51,d8-fc-93-49-e4-54,d8-fc-93-49-e3-50,d9-fc-93
-58-2d-d4,d8-fc-93-4a-40-5c,d8-fc-93-56-9d-2a,d8-fc-93-49-e3-3c,d8-fc-93-4a-24-0f,d8-fc-93-49-23-57,10-0b-a9-91-b3-ac,48-51-b7-79-1f-67,00-27-10-10-46-60,50-1a-c5-f3-39-9
9,80-19-34-e1-eb-a0,80-19-34-70-d4-01,f8-16-54-f0-1b-12,00-23-14-84-dc-54,50-1a-c5-e8-16-e3,f8-16-54-50-2d-73,80-86-f2-cb-cd-2f,80-86-f2-cb-d3-c9,28-6a-ba-bd-3e-f2,a0-a8-
cd-68-0b-87,18-3b-d2-8e-18-41,18-3d-a2-64-1e-3c,18-3d-a2-64-1e-54

Any ideas of what I’m doing wrong? If I do the last step with the pasted output above without running my script to get the list, it works fine, just when i try to use the variable does it not work.

6 Spice ups

My guess is this:

Set-DhcpServerv4Policy -ScopeId X.X.X.X -Name "<PolicyName>" -MacAddress [-eq $MA]

Based off that Set-DhcpServerv4Policy (DhcpServer) | Microsoft Learn

It appears that the parameter -MacAddress wants a comparative, I could totally be wrong though.

3 Spice ups

Look at examples 2 and 3 in the link that RebootsSolveProblems posted.

1 Spice up

Using 2 and 3 in the example, I came up with the top. It works great if I don’t use the script and pass $MA, but instead put them all in by hand. It is like you can’t use a variable to pass in that function.

Should I be doing an array instead of a string?

It does look like the cmdlet needs an array.

This is never going to work this way 1st you need to fix the csv need a line break instead of comma.

the same I also provided

any thoughts guys

@alexw @gungnir

How do I add to an array from a csv file?

I haven’t used this cmdlet before, but if I’m understanding the help doc correctly then something along the lines of this should work.

$Path = 'C:\scripts\DHCP-Policy-Macs.csv'

$MacAddressPieces = Import-Csv -Path $Path |
    Select-Object -ExpandProperty EQ

$MacAddresses = @( 'EQ' ) + $MacAddressPieces

Set-DhcpServerv4Policy -ScopeId X.X.X.X -Name '<PolicyName>' -MacAddress $MacAddresses -Condition Or