在Powershell中使用参数作为netsh的参数

时间:2018-10-03 13:01:05

标签: powershell netsh

我正在尝试在脚本中使用netsh更改IP地址。我收到错误:“参数不正确” 这是脚本:

$ManagmentAddress = '192.168.11.130'
Invoke-Command -ComputerName $OldName -ScriptBlock {netsh interface ip set address "Ethernet0" static $ManagmentAddress 255.255.0.0 } -Credential $Creds

如果我将命令更改为使用实际值而不是像这样的参数:

Invoke-Command -ComputerName $OldName -ScriptBlock {netsh interface ip set address "Ethernet0" static 192.168.11.130 255.255.0.0 } -Credential $Creds

有效。

1 个答案:

答案 0 :(得分:1)

您缺少具有相应param()值的 ScripBlock -ArgumentList块。

$ManagmentAddress = '192.168.11.130'
Invoke-Command -ComputerName $OldName -ScriptBlock {param($ManagmentAddress) netsh interface ip set address "Ethernet0" static $ManagmentAddress 255.255.0.0 } -Credential $Creds -ArgumentList $ManagmentAddress