简体   繁体   English

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

[英]Difference between “RenewDHCPLease()” and “IPConfig /renew”?

I am using RenewDHCPLease() to renew the IP address of the system. 我正在使用RenewDHCPLease()来更新系统的IP地址。

  1. What is the exact difference between RenewDHCPLease() and ipconfig /renew ? RenewDHCPLease()ipconfig /renew之间的确切区别是什么?
  2. Which one is better to use in Powershell? 在Powershell中哪一个更好用?
  3. Is it necessary to use ReleaseDHCPLease() before RenewDHCPLease() ? 是否有必要在RenewDHCPLease()之前使用ReleaseDHCPLease() RenewDHCPLease() If so why? 如果是这样的话?

Below is my code: 以下是我的代码:

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
}

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 释放绑定到特定DHCP启用的网络适配器 的IP地址 RenewDHCPLease 更新 特定DHCP启用的网络适配器上 的IP地址

There is no need to use ReleaseDHCPLease before RenewDHCPLease since the old address is released and a new one is automatically acquired when using RenewDHCPLease RenewDHCPLease之前无需使用ReleaseDHCPLease ,因为旧地址已发布,并且在使用RenewDHCPLease时会自动获取新地址

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

You could also use the RenewDHCPLeaseAll method to renew them all at once. 您还可以使用RenewDHCPLeaseAll方法立即更新它们。 Hope this helps :) 希望这可以帮助 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM