2014-10-20 21 views
0

如何在腳本中添加多個函數,然後通過輸入參數調用它們?例如在下面的代碼它想要改變本地連接的配置,所以我想調用像SscriptNAme - 改變192.168.0.1,255.255.255.255,192.168.0.2的功能。我想有相同的腳本中的其他功能,我也想通過輸入參數來調用:powershell中的函數和參數

function Change{ 

$S=$executionContext.InvokeCommand.NewScriptBlock("netsh interface ip set address name = 'Local    Area Connection' source=static addr=$args[0] mask=$args[1] gateway=$args[2] gwmetric=0") 

} 

function ChangeOne{ 

$S=$executionContext.InvokeCommand.NewScriptBlock("netsh interface ip set address name = 'Local Area Connection' source=static addr=$args[0] mask=$args[1] gateway=$args[2] gwmetric=0") 

} 

可以有人如果即時通訊在正確的方向告訴我,並給我如何做一些指點。感謝

回答

0
function Change 
{ 
    param($addr, $mask, $gw, $metric=0) 
    $S=$executionContext.InvokeCommand.NewScriptBlock("netsh interface ip set address name='Local    Area Connection' source=static addr=$addr mask=$mask gateway=$gw gwmetric=$metric") 

} 

#To call use this, note, no commas between args, and $metric is defined 0 by default. 
Change 192.168.0.2 255.255.255.255 192.168.0.1 
1

你可以發現這與1個谷歌查詢,但在這裏你去:

​​

調用函數:

Change -address $address -mask $mask -gw $gw -metric $metric 

公制是不是強制性的,默認爲0。

如果參數address,mask和gw中包含一個值,則它們也會得到testet,以防一個強制參數eters是空的或只是不存在的功能會拋出一個錯誤。

您也可以指定參數的位置,使你沒有使用標誌(證明上的地址),像這樣的電話:

Change $address -mask $mask -gw $gw 

更多關於此處的參數:

Technet

但我強烈建議你不要使用netsh,甚至invoke-expression這種方式,有這種東西的cmdlet!例如:

Get-NetAdapter "Ethernet" | New-NetIPAddress $address -Defaultgateway $gw -PrefixLength $sn