OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

How can I use a variable as a property value when using Add-DhcpServerv4Resevation?

  • Thread starter Thread starter Mr. Puli
  • Start date Start date
M

Mr. Puli

Guest
I am new to powershell. There may be better ways to acomplish what I am trying to do. I am writing a script to set a dhcp reservation when the user gives a device name. First the scopeID is searched for and saved in $newScope. It is also convert to an IP format. The same is done for the IP address and stored in $IP. It is also converted to an IP format. Finally the MAC is found and stored as a string in $MAC.

The issue is that I am trying to use those variables in the Add-DhcpServerv4Reservation command. If I input the information manually it works. How can I take the values that I found and input them into the respective fields.

Code:
viceName = Read-Host "Enter the name of the device" 
$PC= $DeviceName + ".domainname.com"
$DHCPServer = 'domainDHCPServer.com'


$scopes = Get-DhcpServerv4Scope -ComputerName $DHCPServer

#Get Scope
 $newScope = foreach ($scope in $scopes){
 
    Get-DhcpServerv4Lease -ScopeId $scope.ScopeID -ComputerName $DHCPServer |
    Where-Object hostname -eq $PC |
        Select-Object -ExpandProperty ScopeID |
        Select-Object -ExpandProperty IPAddressToString

   }
   #Convert to IP format
   $newScope= [IPAddress]$newScope.Trim()

  #Get IP
 $IP = foreach ($scope in $scopes){
 
    Get-DhcpServerv4Lease -ScopeId $scope.ScopeID -ComputerName $DHCPServer |
        Where-Object hostname -eq $PC |
            Select-Object -ExpandProperty IPAddress |
                Select-Object -ExpandProperty IPAddressToString

   }
   #Convert to IP format
   $IP= [IPAddress]$IP.Trim() 
   
#Get Mac
$Mac = foreach ($scope in $scopes){
 
    Get-DhcpServerv4Lease -ScopeId $scope.ScopeID -ComputerName $DHCPServer |
        Where-Object hostname -eq $PC |
            Select-Object -ExpandProperty ClientID 
       
   }
   #Converts to String
   Out-String -InputObject $Mac

#Set Reservation

Add-DhcpServerv4Reservation -ScopeId $newScope -IPAddress $IP -ClientID $Mac -ComputerName $DHCPServer

The error I get says:

Add-DhcpServerv4Reservation : Cannot validate argument on parameter 'ScopeId'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.

I tried putting the variables striaght in and that did not work. I did a bunch of searching online and found similar issues but nothing specific to what I am trying to accomplish-putting variables in as property values.

Output for just the $newScope For Loop*******

I commented out the coversion to IP:

Code:
>      #Get Scope
>      $newScope = foreach ($scope in $scopes){
>      
>         Get-DhcpServerv4Lease -ScopeId $scope.ScopeID -ComputerName $DHCPServer |
>         Where-Object hostname -eq $PC |
>             Select-Object -ExpandProperty ScopeID |
>             Select-Object -ExpandProperty IPAddressToString
>     
>        }
>     write-Output $newScope

OUTPUT: 192.168.x.x

I do get the scope in string format.

Thanks for all the help in advance!
<p>I am new to powershell. There may be better ways to acomplish what I am trying to do. I am writing a script to set a dhcp reservation when the user gives a device name. First the scopeID is searched for and saved in $newScope. It is also convert to an IP format. The same is done for the IP address and stored in $IP. It is also converted to an IP format. Finally the MAC is found and stored as a string in $MAC.</p>
<p>The issue is that I am trying to use those variables in the Add-DhcpServerv4Reservation command. If I input the information manually it works. How can I take the values that I found and input them into the respective fields.</p>
<pre><code>viceName = Read-Host "Enter the name of the device"
$PC= $DeviceName + ".domainname.com"
$DHCPServer = 'domainDHCPServer.com'


$scopes = Get-DhcpServerv4Scope -ComputerName $DHCPServer

#Get Scope
$newScope = foreach ($scope in $scopes){

Get-DhcpServerv4Lease -ScopeId $scope.ScopeID -ComputerName $DHCPServer |
Where-Object hostname -eq $PC |
Select-Object -ExpandProperty ScopeID |
Select-Object -ExpandProperty IPAddressToString

}
#Convert to IP format
$newScope= [IPAddress]$newScope.Trim()

#Get IP
$IP = foreach ($scope in $scopes){

Get-DhcpServerv4Lease -ScopeId $scope.ScopeID -ComputerName $DHCPServer |
Where-Object hostname -eq $PC |
Select-Object -ExpandProperty IPAddress |
Select-Object -ExpandProperty IPAddressToString

}
#Convert to IP format
$IP= [IPAddress]$IP.Trim()

#Get Mac
$Mac = foreach ($scope in $scopes){

Get-DhcpServerv4Lease -ScopeId $scope.ScopeID -ComputerName $DHCPServer |
Where-Object hostname -eq $PC |
Select-Object -ExpandProperty ClientID

}
#Converts to String
Out-String -InputObject $Mac

#Set Reservation

Add-DhcpServerv4Reservation -ScopeId $newScope -IPAddress $IP -ClientID $Mac -ComputerName $DHCPServer
</code></pre>
<p>The error I get says:</p>
<blockquote>
<p>Add-DhcpServerv4Reservation : Cannot validate argument on parameter 'ScopeId'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.</p>
</blockquote>
<p>I tried putting the variables striaght in and that did not work. I did a bunch of searching online and found similar issues but nothing specific to what I am trying to accomplish-putting variables in as property values.</p>
<p><strong><strong><strong><strong><strong>Output for just the $newScope For Loop</strong></strong></strong></strong></strong>*******</p>
<p>I commented out the coversion to IP:</p>
<pre><code>> #Get Scope
> $newScope = foreach ($scope in $scopes){
>
> Get-DhcpServerv4Lease -ScopeId $scope.ScopeID -ComputerName $DHCPServer |
> Where-Object hostname -eq $PC |
> Select-Object -ExpandProperty ScopeID |
> Select-Object -ExpandProperty IPAddressToString
>
> }
> write-Output $newScope
</code></pre>
<p>OUTPUT: 192.168.x.x</p>
<p>I do get the scope in string format.</p>
<p>Thanks for all the help in advance!</p>
Continue reading...
 

Latest posts

Online statistics

Members online
0
Guests online
3
Total visitors
3
Top