【问题标题】:powershell - storing cmdlet Get-DhcpServerv4Reservation into a variable and then printing it, doesn't give expected outputpowershell - 将 cmdlet Get-DhcpServerv4Reservation 存储到一个变量中,然后打印它,没有给出预期的输出
【发布时间】:2023-04-09 03:13:01
【问题描述】:

我正在 Windows Server 2012 R2 中运行具有活动范围的 DHCP 服务器,并且我制作了一个脚本,该脚本要求用户提供 MAC 地址,然后将范围内的可用 IP 保留给用户的 MAC。

实际预订工作没有问题,但我引入了 IF ELSE 语句,希望条件表达式在预订成功时被评估为 TRUE,否则为 FALSE。

但该表达式的计算结果始终为 FALSE,因为将 cmdlet Get-DhcpServerv4Reservation 的执行存储到一个变量中然后打印它会产生一个非常混乱的输出:基本上它会将“DHCPServerv4Reservation”打印为多次作为范围中存在的保留,而不是直接执行 cmdlet 时给出的输出。

代码如下:

clear-host

$mac=read-host -prompt "Please type the MAC address for the host "

$freeip=Get-DhcpServerv4FreeIPAddress -scopeid 10.10.10.0

Add-DhcpServerv4Reservation -ScopeId 10.10.10.0 -ClientId $mac -IPAddress $freeip

$reservedips=Get-DhcpServerv4Reservation -ScopeId 10.10.10.0

if ($reservedips -match $freeip) {

    write-host "The ip $freeip has been succesfully reserved for the host with the MAC address $mac"
}

else {write-host "I'm sorry but there are no free ip addresses to be  reserved"}

# this is just to see what's inside $reservedips
write-host $reservedips

为什么会这样?谢谢

【问题讨论】:

    标签: powershell


    【解决方案1】:

    Get-DhcpServerv4Reservation 的结果是一个预留对象。

    -match 运算符用于将字符串与正则表达式进行匹配。

    您可能正在寻找类似的东西:

    if (($reservedips | Where-Object { $_.IPAddress -eq $freeip })) {
        # success
    }
    

    我认为您考虑将代码重组为如下所示:

    $mac=read-host -prompt "Please type the MAC address for the host "
    
    $freeip=Get-DhcpServerv4FreeIPAddress -scopeid 10.10.10.0
    
    try {
        Add-DhcpServerv4Reservation -ScopeId 10.10.10.0 -ClientId $mac -IPAddress $freeip -ErrorAction Stop
    } catch {
        Write-Host "An error has occurred while trying to add a reservation for '$mac' with IP '$freeip'."
    }
    

    添加-ErrorAction Stop 强制所有异常都被try/catch 块捕获。

    【讨论】:

    • 谢谢,我自己找到了一个替代方案,我会尝试以某种方式在这里展示
    • 是的,我的替代方案与您的非常相似。问题是,当我使用 get-content cmdlet 将文本文件的内容存储到变量中时,我可以让 -match 运算符工作
    • 我不确定你的意思;我在您的代码中没有看到对 Get-Content 的调用。在任何情况下,您都需要小心使用 -match 运算符直接比较字符串,因为右侧的运算符被解释为正则表达式。考虑使用-like-eq,除非你真的想要一个正则表达式匹配。
    • 是的,当我发表该评论时,我指的是另一个脚本(对此感到抱歉)。但是,当数据最终来自文本文件(我相信,事实并非如此)和当它不是(我相信这是案例)
    • 无论如何,明天我有一个关于powershell的期末考试,这实际上是另一个恢复考试的恢复考试。我只是希望我能通过,希望我永远不必再使用这种脚本语言......绝对是我的克星
    【解决方案2】:

    这是我想出的替代方案,可行

    clear-host
    
    $mac=read-host -prompt "Please type the MAC address for the host "
    
    $freeip=Get-DhcpServerv4FreeIPAddress -scopeid 10.10.10.0
    
    Add-DhcpServerv4Reservation -ScopeId 10.10.10.0 -ClientId $mac -IPAddress $freeip
    
    $reservedips=Get-DhcpServerv4Reservation -ScopeId 10.10.10.0 | Where-Object ipaddress -eq $freeip
    
    if ($reservedips -eq "") {write-host "I'm sorry but there are no free ip addresses to be reserved"}
    
    else {write-host "The ip $freeip has been succesfully reserved for the host with the MAC address $mac"}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-07-08
      • 2019-07-16
      • 1970-01-01
      • 2015-09-01
      • 2019-10-27
      • 2020-10-09
      • 1970-01-01
      • 2013-06-21
      相关资源
      最近更新 更多