I'm working with the Get-DhcpServerv4Scope
cmdlet and am looking to pull multiple scopes into a variable with the -scopeid
switch.
Get-DhcpServerv4Scope -scopeid 10.1.0.0,10.1.1.0
However if I do:
$IP = "10.2.0.0,10.1.0.0"
Get-DhcpServerv4Scope -scopeid $IP
I get the error:
Get-DhcpServerv4Scope : Cannot process argument transformation on
parameter 'ScopeId'. Cannot convert value "10.2.0.0,10.1.0.0" to type
"System.Net.IPAddress[]". Error: "Cannot convert value
"10.2.0.0,10.1.0.0" to type "System.Net.IPAddress". Error: "An invalid
IP address was specified.""
Anyone have an idea how to get do this conversion?
Edit: Here's the script I'm trying to fit this into. I'd like to have a way to point this script at a server and it rebuilds all scopes. right now I output the scopes in a tab delimited file, then copy & paste them into the Get-dhcpv4scope
to store all of the scopes in one variable. I'm open to any suggestions:
#GetScopes
$ScopeExclusions = Get-DhcpServerv4ExclusionRange
[string]$ScopesCSV=Get-DhcpServerv4Scope | Select ScopeID | convertto-
csv -notypeinformation
$ScopesCSV.replace('" ', ',').replace('"', '').replace('ScopeId,', '')
| out-file c:\users\aturner\desktop\csv.txt
**Copy contents from file to clipboard**
$Scopes=Get-DhcpServerv4Scope -scopeid *PasteScopeCSVListHere* | select
ScopeId,Name,Description,SubnetMask,StartRange,EndRange,LeaseDuration
#Delete Scopes
$Scopes | % { Remove-DhcpServerv4Scope -ScopeId $_.scopeid -force}
#Rebuild Scopes
$Scopes | % {Add-DhcpServerv4Scope -Name $_.name -Description
$_.description -SubnetMask $_.SubnetMask -StartRange $_.StartRange -
EndRange $_.EndRange -LeaseDuration 8.00:00:00 }
$ScopeExclusions | % {Add-DHCPServerv4ExclusionRange -ScopeID $_.ScopeID -
StartRange $_.StartRange -EndRange $_.EndRange}
New Code:
#GetScopes
$ScopeExclusions = Get-DhcpServerv4ExclusionRange
[string]$ScopesArray=Get-DhcpServerv4Scope | Select ScopeID | convertto-csv -notypeinformation
$ScopesCSV=$ScopesArray.replace('" ', ',').replace('"', '').replace('ScopeId,', '')
$ScopeReservations = Get-DhcpServerv4Scope | Get-DhcpServerv4Reservation | select *
$Scopes=Get-DhcpServerv4Scope -scopeid $ScopesCSV.Split(',') | select ScopeId,Name,Description,SubnetMask,StartRange,EndRange,LeaseDuration
#Delete Scopes
$Scopes | % { Remove-DhcpServerv4Scope -ScopeId $_.scopeid -force}
#Rebuild Scopes
$Scopes | % {Add-DhcpServerv4Scope -Name $_.name -Description $_.description -SubnetMask $_.SubnetMask -StartRange $_.StartRange -EndRange $_.EndRange -LeaseDuration 8.00:00:00 }
$ScopeReservations | % {Add-DhcpServerv4Reservation -ScopeID $_.ScopeID -IPAddress $_.IPAddress -ClientId $_.ClientID -Description $_.Description -Name $_.Name -Type $_.Type}
New
Best Answer
However your main problem is that
$IP
is a string, while when you manually pass 10.1.0.0,10.1.1.0 as parameters you are actually passing 2 strings.You would be better off creating
$IP
as an array:This way you can script / loop through the array if you have multiple scopes.
Adam asked how to do this dynamically and although I don't know where are we getting "10.2.0.0" and "10.1.0.0" from, you'll just need to keep populating the
$IP
array with all the scope IDs you need to check, then loop as follows:The above code goes through each entry in
$IP
, temporarily storing it in$scopeid
and using it as parameter to call theGet-DhcpServerv4Scope
cmdlet.