通过输入设备名称添加 DHCP 预留

问题描述 投票:0回答:1

我是 powershell 新手。可能有更好的方法来完成我想做的事情。我正在编写一个脚本,用于在用户提供设备名称时设置 dhcp 预留。首先搜索scopeID并将其保存在$newScope中。它还会转换为 IP 格式。对 IP 地址进行同样的操作并将其存储在 $IP 中。它还会转换为 IP 格式。最后找到 MAC 并将其作为字符串存储在 $MAC 中。

问题是我尝试在 Add-DhcpServerv4Reservation 命令中使用这些变量。如果我手动输入信息,它就会起作用。我如何获取找到的值并将其输入到相应的字段中。

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 

我收到的错误是:

Add-DhcpServerv4Reservation:无法验证参数“ScopeId”的参数。参数为 null 或为空。提供一个不为 null 或空的参数,然后重试该命令。

我尝试直接输入变量,但没有成功。我在网上进行了大量搜索,发现了类似的问题,但没有具体说明我想要完成的任务 - 将变量作为属性值放入。

$newScope For 循环的输出********

我注释掉了IP的转换:

>      #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

输出:192.168.x.x

我确实获得了字符串格式的范围。

感谢您提前提供的所有帮助!

powershell dhcp
1个回答
0
投票

经过多次修改和研究,我终于把剧本写好了。基本上我必须将 forloops 的输出转换为作用域和 IP 的 system.net.ipaddress 。 以下脚本经过修改/工作。感谢大家的意见,它确实有帮助。干杯!

> $DeviceName = Read-Host "Enter the name of the device" 
$PC= $DeviceName + ".companydomain.com"
$DHCPServer = 'dhcpserver.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 
   }

#Informative
write-host "The scope for" $PC "is" $newScope

#Convert to IP format
$TheScope = [System.Net.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 
   }

#Informative
write-host "The IP for" $PC "is" $IP

#Convert to IP format
$newIP = [System.Net.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     
   }
#Informative
write-host "The Mac for" $PC "is" $Mac


#Set Reservation

Add-DhcpServerv4Reservation -ScopeId $TheScope -IPAddress $newIP -ClientID $Mac -ComputerName $DHCPServer 
© www.soinside.com 2019 - 2024. All rights reserved.