2014-10-01 53 views
-1

有没有办法输入文字记事本,说明IP地址,子网掩码和网关,然后运行PowerShell脚本,将执行以下netsh接口ipv4>设置地址名称=“无线网络连接” source = static addr = 1922.xxx.x.xx mask = 255.255.0.0 gateway = xxx.xxx.xx gwmetric = 0Powershell netsh自动运行脚本

此刻我只是在Powershell终端或cmd中手动输入它,我想要改变)但我希望能够编辑一个记事本文件,并exute批处理文件,将自动做到这一点..

我是相当新的PowerShell和脚本,但如果有人可以指出我在右它将是方向非常感谢。

谢谢。

回答

0

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

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命令的输入

+0

谢谢!我试过了,它给了我以下错误: 'Invoke-Expression:无法将参数绑定到参数'Command',因为它是一个空字符串。 在C:\桌面\ change.ps1:2炭:18 +调用-表达<<<< + CategoryInfo:InvalidData:(:) [调用-表达式],ParameterBindingValidationException + FullyQualifiedErrorId:ParameterArgumentValidationErrorEmptyStringNotAllowed,Microsoft.PowerShell。 Commands.InvokeExpressionCommand' – Jendizer 2014-10-01 15:58:21

+0

我在'Get-Content'中犯了一个错误,所以再试一次,修改了这个问题。你也不用_have_使用'Invoke-Expression',所以试着在没有它的情况下运行这个字符串 – arco444 2014-10-01 16:17:56