2014-10-01 158 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