我正在使用RenewDHCPLease()
来更新系统的IP地址。
RenewDHCPLease()
和ipconfig /renew
之间的确切区别是什么? 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
}
答案 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
方法立即更新它们。希望这会有所帮助:)