首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将IP地址从dhcp更改为静态

问将IP地址从dhcp更改为静态
EN

Stack Overflow用户
提问于 2018-08-21 22:09:22
回答 2查看 1.1K关注 0票数 1

我已经编写了一个适用于windows10的powershell脚本,但不适用于较早版本的powershell。它还会在某些PC上抛出CIMs错误。真正的麻烦是,它不能在我需要更换的2000多台电脑上工作;Windows7电脑,每台电脑都在不同的网络中。

我需要这个来获取IP,更改最后一个八位字节,然后设置IP;我可以从LANDesk运行它,但它需要在任何版本的Windows上运行,所以我认为powershell是不是已经过时了?

一般来说,我并不精通脚本编写,而且我只在Powershell中工作过,然而,这并不需要是powershell。

我的脚本在某种程度上是有效的,也是有问题的:

代码语言:javascript
复制
$computername = HOSTNAME.EXE
$info = Get-NetIPAddress -CimSession $computername -AddressFamily IPv4 | where { $_.InterfaceAlias -notmatch 'Loopback'} | Select PSComputername,IPAddress,InterfaceIndex
$ip = $info.IPAddress
$intindex = $info.InterfaceIndex

write "IP: "$ip
write "InterfaceIndex: "$intindex

$ip2 = $ip.split('.')
$ip2[-1] = 80
$newip = $ip2 -join "." 

Write "New IP: "$newip

Set-NetIPAddress -InterfaceIndex $intindex -IPAddress $newip -PrefixLength 24 -WhatIf
EN

回答 2

Stack Overflow用户

发布于 2018-08-22 02:02:27

感谢BenH和Paxz!

我有一个使用wmi的解决方案:

代码语言:javascript
复制
$ip = Get-WmiObject Win32_NetworkAdapterConfiguration | Where-Object { $_.IPAddress } | Select-Object -Expand IPAddress | Where-Object { $_ -like '10.*' }
$ip2 = $ip.split('.')
$ip2[-1] = 80
$newip = $ip2 -join "."
$gateway = (Get-wmiObject Win32_networkAdapterConfiguration | ?{$_.IPEnabled}).DefaultIPGateway
$wmi = Get-WmiObject win32_networkadapterconfiguration -filter "ipenabled = 'true'"
$wmi.EnableStatic($newip ,"255.255.255.0")
$wmi.SetGateways($gateway,1)
$wmi.SetDNSServerSearchOrder("10.x.x.x")
票数 1
EN

Stack Overflow用户

发布于 2020-05-19 20:14:12

使用PowerShell将IP地址从dhcp更改为静态:

代码语言:javascript
复制
   $interface.EnableStatic("192.168.1.25","255.255.255.0") | out-null
   $interface.SetGateways("192.168.2.15") 
   $interface.SetDNSServerSearchOrder($dnsRecords) 
   $interface.SetDynamicDNSRegistration($false)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51950605

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档