我已经编写了一个适用于windows10的powershell脚本,但不适用于较早版本的powershell。它还会在某些PC上抛出CIMs错误。真正的麻烦是,它不能在我需要更换的2000多台电脑上工作;Windows7电脑,每台电脑都在不同的网络中。
我需要这个来获取IP,更改最后一个八位字节,然后设置IP;我可以从LANDesk运行它,但它需要在任何版本的Windows上运行,所以我认为powershell是不是已经过时了?
一般来说,我并不精通脚本编写,而且我只在Powershell中工作过,然而,这并不需要是powershell。
我的脚本在某种程度上是有效的,也是有问题的:
$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发布于 2018-08-22 02:02:27
我有一个使用wmi的解决方案:
$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")发布于 2020-05-19 20:14:12
使用PowerShell将IP地址从dhcp更改为静态:
$interface.EnableStatic("192.168.1.25","255.255.255.0") | out-null
$interface.SetGateways("192.168.2.15")
$interface.SetDNSServerSearchOrder($dnsRecords)
$interface.SetDynamicDNSRegistration($false)https://stackoverflow.com/questions/51950605
复制相似问题