Help with Powershell Script for Creating VLANs and Network Adapters

0
4
Asked By CuriousCat92 On

I'm trying to create a Powershell script that sets up virtual network interface cards (NICs) for VLANs. The script should be able to allow me to input multiple VLANs at once. I'm having trouble with my if statements and checking for existing connections or switches, as they don't seem to be parsing correctly. I'm fairly new to Powershell and started with some basic code generated by a tool, but it didn't initially work. Any pointers on what I'm doing wrong would be greatly appreciated!

4 Answers

Answered By ScriptSlinger On

It could also help to limit your Read-Host inputs to ensure they're correct, like setting up a do-while loop that checks if the input is '1' or '2' before proceeding. That way, you can avoid any unexpected inputs messing up your script.

Answered By TechGuru88 On

It sounds like you're struggling with how your if statements are working. For instance, your first if statement checks for the existence of the switch, but it should be more specific. Try replacing `if (Get-VMSWitch | where Name -ne "vLanSwitch")` with `if (-not (Get-VMSwitch -Name "vLanSwitch" -ErrorAction SilentlyContinue))`, which correctly handles whether the switch exists or not. Also, check your VLAN handling; instead of using `$vmName$vlan`, it’s better to concatenate with `$vmName + $vlan` or use `${vmName}$vlan`. It's these minor adjustments that might make a world of difference. Good luck!

Answered By NerdyNellie On

Totally agree with the points so far! Also, make sure to log what your inputs and outputs are throughout the script. It gives you a better idea of what's happening at each step, especially when something goes wrong!

Answered By PowerUser101 On

You also seem to have a typo in your command with `Set-VMNetworkAdapterVlan -MangementOS`. It should be `-ManagementOS`. These simple spelling errors can lead to frustrating issues. Also, if your inputs are hard to manage, consider defining parameters instead of using `Read-Host`. This way, you'll have clearer control over your inputs and can prevent invalid entries.

DebuggingDiva -

That’s a great suggestion! Using parameters can definitely simplify the input process, especially if you're checking for valid options. Using `ValidateSet` is a good way to ensure only correct values are accepted.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.