1

I have created a script to change my IP address/mask and VLAN ID of an interface but the problem is that in order to change the VLAN ID, I need to execute a PowerShell command. My variable containing the interface name contains a space, so I need to put it in quotes. The problem is that I need to insert two variables for the same interfaceName, one with single quotes for the Powershell command and another for the batch netsh command, otherwise, I am getting an error. This is my batch file:

:: Configuration Variables
set ifName='Ethernet 2'
set connectionName="Ethernet 2"
set ipAddress=10.88.167.27
set subnetMask=255.255.255.240
set vlanID=100

:: set defaultGateway=x.x.x.x
:: set primaryDNS=x.x.x.x
:: set alternateDNS=x.x.x.x

:: Change of IP address and NetMask ::
netsh interface ipv4 set address name=%connectionName% source=static addr=%ipAddress% mask=%subnetMask%


:: Change VLAN ID ::
powershell -Command "& {Set-NetAdapter -Name %ifName% -VlanID %vlanID% -Confirm:$false}"
echo The VLAN ID of %ifName% has been successfully changed to %vlanID%

pause > null

My batch script is running fine but I want to have only one variable for the interface name, instead of two. My problem is that:

If I use ifName in the change IP address command, I am getting the following error: The filename, directory name, or volume label syntax is incorrect..

If I use the connectionName for the PowerShell command with double quotes, I got the following error:

Set-NetAdapter : A positional parameter cannot be found that accepts argument '2'.
At line:1 char:4
+ & {Set-NetAdapter -Name Ethernet 2 -VlanID 100 -Confirm:$false}
+    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Set-NetAdapter], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Set-NetAdapter

I have even tried to enclose the Powershell command in single quotes and use inside connectionName like this:

powershell -Command '& {Set-NetAdapter -Name %connectionName% -VlanID %vlanID% -Confirm:$false}'

but the network interface VLAN stays the same.

1 Answer 1

3

Set variables using set "varname=varvalue" syntax pattern and use them double quoted if necessary as echo "%varname%". Then, your code snippet should be as follows:

:: Configuration Variables
set "ifName=Ethernet 2"
set "connectionName=Ethernet 2"
set "ipAddress=10.88.167.27"
set "subnetMask=255.255.255.240"
set "vlanID=100"

:: Change of IP address and NetMask ::
netsh interface ipv4 set address name="%connectionName%" source=static addr=%ipAddress% mask=%subnetMask%

:: Change VLAN ID :: needs some tricky escaping

powershell -Command "& {Set-NetAdapter -Name """"'%connectionName%'"""" -VlanID %vlanID% -Confirm:$false}"
echo The VLAN ID of %ifName% has been successfully changed to %vlanID%
2
  • great tip. Just a small correction from my side, the """"'%connectionName%'"""" needs to be changed to '%connectionName%' otherwise it doesn't work and I am getting the same error message. May 20, 2020 at 10:17
  • Tested using Write-Host and Write-Output cmdlets instead of Set-NetAdapter one. Both show properly single-quoted'Ethernet 2' for the """"'%connectionName%'"""" token. So I don't see any reason for failing with Set-NetAdapter cmdlet…
    – JosefZ
    May 20, 2020 at 10:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.