“ RenewDHCPLease()”和“ IPConfig / renew”之间的区别?

时间:2019-02-13 12:08:13

标签: powershell dhcp

我正在使用RenewDHCPLease()来更新系统的IP地址。

  1. RenewDHCPLease()ipconfig /renew之间的确切区别是什么?
  2. 在Powershell中使用哪个更好?
  3. 是否有必要在ReleaseDHCPLease()之前使用RenewDHCPLease()?如果是,为什么?

下面是我的代码:

try {
    $ips = Get-WmiObject -Class Win32_NetworkAdapterConfiguration | Where { $_.IpEnabled -eq $true -and $_.DhcpEnabled -eq $true} -ErrorAction Stop
    if (!$ips) {
        Write-Output "`nDHCP is not Enabled on this system"
        Exit
    }
    foreach ($ip in $ips) { 
        Write-Output "`nRenewing IP Addresses" 
        $ip.RenewDHCPLease() | Out-Null  
        if ($?) {
            Write-Output "IP address has been renewed for this system"
        }
        else {
            Write-Error "IP Address Could not be renewed"
        }
    }
}
catch {
    $_.Exception.Message
}

1 个答案:

答案 0 :(得分:0)

ReleaseDHCPLease releases the IP address bound to a specific DHCP enabled network adapter RenewDHCPLease Renews the IP address on a specific DHCP enabled network adapter

ReleaseDHCPLease之前不需要使用RenewDHCPLease,因为使用RenewDHCPLease时会释放旧地址并自动获取新地址

Get-WmiObject Win32_NetworkAdapterConfiguration -Filter 'IpEnabled=True AND DhcpEnabled=True' | Foreach-Object{
    $_.RenewDHCPLease() 
}

您还可以使用RenewDHCPLeaseAll方法立即更新它们。希望这会有所帮助:)