Girish Kumar Mayigowda Girish Kumar Mayigowda - 3 months ago 13
PowerShell Question

How to pass parameter to netsh command

Please let me know how to pass ipaddress,subnetmask,gateway while execution of the script

Below is the script:

$VMNetworkSettings = 'netsh interface ip set address "Ethernet0" static x.x.x.x(ipaddress) x.x.x.x(subnet mask) x.x.x.x(gateway)'


i have tried the below way to achieve but got an error message as invalid parameter

$Ip = Read-Host -Prompt "Enter the ip address"
$sub = Read-Host -Prompt "Enter the subnet"
$gateway = Read-Host -Prompt "Enter the gateway"
$VMNetworkSettings = 'netsh interface ip set address "Ethernet0" static $Ip $sub $gateway'

Answer

Try escaping the double quotes (`") so that you can use them at the start of the string.

$VMNetworkSettings="netsh interface ip set address `"Ethernet0`" static $Ip $sub $gateway" 
Comments