gpt4 book ai didi

windows - IPv4 属性 UI 中与 "Use the following IP Address"等效的 Powershell 是什么?

转载 作者:行者123 更新时间:2023-12-02 23:39:27 25 4
gpt4 key购买 nike

我正在寻找一个 Powershell 命令来禁用 DHCP 并将机器的私有(private) IP 设置为静态 IP;基本上,我正在寻找与 UI 中的以下操作等效的 Powershell。

控制面板 -> 网络和共享中心 -> 以太网 -> 属性 -> IPv4 -> 属性 -> 关闭“自动获取 IP 地址”并打开“使用以下 IP 地址” -> 填写 IPv4 地址、默认网关和子网掩码。

I want to Powershell this window

以下命令取自this guide ,似乎描述了我所追求的内容,但是 Remove-NetIPAddress 导致将我踢出服务器并将我锁定。

$IP = "10.10.10.10"
$MaskBits = 24 # This means subnet mask = 255.255.255.0
$Gateway = "10.10.10.1"
$Dns = "10.10.10.100"
$IPType = "IPv4"
# Retrieve the network adapter that you want to configure
$adapter = Get-NetAdapter | ? {$_.Status -eq "up"}
# Remove any existing IP, gateway from our ipv4 adapter
If (($adapter | Get-NetIPConfiguration).IPv4Address.IPAddress) {
$adapter | Remove-NetIPAddress -AddressFamily $IPType -Confirm:$false
}
If (($adapter | Get-NetIPConfiguration).Ipv4DefaultGateway) {
$adapter | Remove-NetRoute -AddressFamily $IPType -Confirm:$false
}
# Configure the IP address and default gateway
$adapter | New-NetIPAddress `
-AddressFamily $IPType `
-IPAddress $IP `
-PrefixLength $MaskBits `
-DefaultGateway $Gateway

只要现有 IPv4 地址仍然存在,我就无法创建新的 IPv4 地址,但删除现有地址会中断与服务器的连接。

最佳答案

这是不正确的。您可以在同一接口(interface)上设置多个IP。因此,您只需使用 New-NetIPAddress 添加新的地址,然后删除前一个即可。

enter image description here

$currentIP = Get-NetIPAddress | where ipaddress -eq '192.168.43.96'

New-NetIPAddress -InterfaceAlias $currentIP.InterfaceAlias -IPAddress 192.168.43.20 -PrefixLength 24

Remove-NetIPAddress -InterfaceAlias $currentIP.InterfaceAlias -IPAddress $currentIP.IPAddress

enter image description here

DNS 和网关是不同的东西。如果不需要,就不要更改它们。要避免出现确认提示,只需将 -Confirm:$false 添加到 Remove-NetIPAddress 命令即可。

关于windows - IPv4 属性 UI 中与 "Use the following IP Address"等效的 Powershell 是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64600675/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com