Powershell netsh自动运行脚本

时间:2014-10-01 15:22:02

标签: powershell scripting ipv4 netsh

有没有办法向记事本输入文本,说明IP地址,子网掩码和网关,然后运行执行以下netsh接口的PowerShell脚本ipv4> set address name =“无线网络连接”source = static addr = 192.xxx.x.xx mask = 255.255.0.0 gateway = xxx.xxx.x.x gwmetric = 0

目前我只是在Powershell终端或cmd中手动输入它并且它可以工作(更改我想要更改的内容)但我希望能够只编辑一个记事本文件并激活一个自动执行此操作的批处理文件。 。

我对PowerShell和脚本编写很新,但如果有人能指出我正确的方向,那将非常感激。

感谢。

1 个答案:

答案 0 :(得分:1)

有很多方法可以做到这一点。这是一个:

ip_properties.txt内容:

192.168.1.15,255.255.255.0,192.168.1.1

powershell脚本内容:

$properties = (Get-Content ip_properties.txt) -split ','
Invoke-Expression "netsh interface ip set address name='Wireless Network Connection' source=static addr=$($properties[0]) mask=$($properties[1]) gateway=$($properties[2]) gwmetric=0"

基本上,您以逗号分隔格式提供接口参数,将这些属性拆分为数组,然后使用数组元素作为netsh命令的输入