SkillPilotAI

PowerShell NetworkSwitchManager module

The NetworkSwitchManager module manages a real physical network switch from PowerShell: its Ethernet ports, VLANs, port modes, features and saved configuration. Every command reaches the switch through a CimSession to the switch's OMI/CIM agent.

264.1 Why is it used?

  • Automate switch work (ports, VLANs, features) from the same PowerShell scripts that manage servers.
  • Standard DMTF profile, so one set of cmdlets works across vendors whose switches have an OMI/CIM agent.
  • Built into Windows Server and Windows 10/11; the video's office PC counted 19 cmdlets.
PowerShell
Get-Command -Module NetworkSwitchManager -ErrorAction SilentlyContinue | Measure-Object
Output
PS C:\> Get-Command -Module NetworkSwitchManager -ErrorAction SilentlyContinue | Measure-Object Count    : 19

264.2 How it works

Through a CimSession to the switch
  1. 1.Admin PC: $Session = New-CimSession -ComputerName "NetworkSwitch08"

  2. 2.Request reaches the switch's OMI agent over WS-Man

  3. 3.Disable-NetworkSwitchEthernetPort -CimSession $Session -PortNumber 21

  4. 4.Switch turns port 21 off: that PC's link goes down

  5. 5.PC on port 5 keeps working normally

  • Common parameters: -CimSession (which session/switch), -Confirm (ask first), -WhatIf (show only).
  • Pick a port by -PortNumber, -DeviceId or an -InputObject from the pipeline.

264.3 Ports: Enable, Disable, Get

PowerShell
$Session = New-CimSession -ComputerName "NetworkSwitch08"Disable-NetworkSwitchEthernetPort -CimSession $Session -PortNumber 21Disable-NetworkSwitchEthernetPort -CimSession $Session -DeviceId "Ethernet17"Enable-NetworkSwitchEthernetPort -Port 21 -CimSession $SessionGet-NetworkSwitchEthernetPort -PortNumber 21 -CimSession $Session
Output
Name            PortNumber DeviceID       FullDuplex    AutoSense     PortType  MaxDataSize----            ---------- --------       ----------    ---------     --------  -----------Ethernet17              21 Ethernet17           True        False            0         9214
  • Get-NetworkSwitchEthernetPort with only -CimSession lists all ports; -FullDuplexEnabled / -FullDuplexDisabled filter by duplex.

264.4 VLANs

VLAN: one switch, separate LANs
  1. 1.pc1 (VLAN 10) pings pc2 (VLAN 10)

  2. 2.switch1 forwards it within VLAN 10 -> reply

  3. 3.pc1 pings pc3 (VLAN 20), same subnet

  4. 4.Switch drops it: different VLAN = different LAN

Crossing VLANs needs a router / L3 port

CmdletJobKey options
Get-NetworkSwitchVlanList VLANs-Name, -VlanId, -Caption, -Description, -InstanceId
New-NetworkSwitchVlanCreate a VLAN-Name, -VlanID, -Caption, -Description
Enable- / Disable-NetworkSwitchVlanTurn a VLAN on/off-InstanceId, -Name, -VlanId
Remove-NetworkSwitchVlanDelete VLANs (wildcards)-VlanID, -Name "*Contoso*", -InputObject
Set-NetworkSwitchVlanPropertyRename / describe-Property @{ElementName=...; Description=...}
PowerShell
Get-NetworkSwitchVlan -CimSession $SessionNew-NetworkSwitchVlan -Name "Contoso07VLAN" -Caption "VLAN 07" -CimSession $Session
Output
Caption             Description         Name                InstanceID                       VlanID PSComputerName-------             -----------         -----------         ----------                       ------ --------------                    Vlan_description    default             Contoso:NetworkVL...                   1 10.19.246.18                    Vlan_description    VLAN0002            Contoso:NetworkVL...                   2 10.19.246.18 Caption             Description         ElementName         InstanceID                       VlanID PSComputerName-------             -----------         -----------         ----------                       ------ --------------VLAN 07                                 VLAN0004            Contoso:NetworkVL...                  4 10.19.26.4
PowerShell
Disable-NetworkSwitchVlan -CimSession $Session -VlanId 74Enable-NetworkSwitchVlan -CimSession $Session -Name "VLAN22"Remove-NetworkSwitchVlan -CimSession $Session -Name "*Contoso*"$VlanName = "Contoso VLAN 07"$Description = "This VLAN is used to create a virtual Contoso company LAN"Set-NetworkSwitchVlanProperty -VlanId 12 -Property @{ElementName = $VlanName; Description = $Description} -CimSession $Session

264.5 Port mode, port IP and port properties

ModeOptionMeaningTypical use
Access-AccessMode -VlanID 1One VLAN onlyPC, printer
Trunk-TrunkMode -VlanIDs 2,17,22Many VLANs on one linkSwitch to switch
Route-RouteMode -IpAddress -SubnetAddressLayer 3 port with its own IPRouting between networks
PowerShell
Set-NetworkSwitchPortMode -CimSession $Session -TrunkMode -VlanIDs 2,17,22Set-NetworkSwitchPortMode -CimSession $Session -IpAddress "10.14.1.1" -RouteMode -SubnetAddress "255.255.0.0"Set-NetworkSwitchEthernetPortIPAddress -IpAddress "10.4.10.12" -PortNumber 21 -SubnetAddress "255.255.255.248" -CimSession $SessionRemove-NetworkSwitchEthernetPortIPAddress -CimSession $Session -PortNumber 21
  • Set-NetworkSwitchPortProperty sets properties like Speed and Description with a hash table.

264.6 Features, save and restore

  • Get-NetworkSwitchFeature lists features like SSH, BGP, LACP, LLDP with IsEnabled; -Enabled / -Disabled filter.
  • Enable- / Disable-NetworkSwitchFeature by -Name, -InstanceId or -FeatureName.
  • Get-NetworkSwitchGlobalData returns the switch's global data (only -CimSession).
  • Save-NetworkSwitchConfiguration makes the current config the startup config so changes survive a reboot.
  • Restore-NetworkSwitchConfiguration goes back to the last saved startup config.
PowerShell
Disable-NetworkSwitchFeature -CimSession $Session -Name "SSH"Enable-NetworkSwitchFeature -CimSession $Session -InstanceID "Contoso:Feature:2"Get-NetworkSwitchFeature -CimSession $Session -FeatureName "*BGP*"Get-NetworkSwitchGlobalData -CimSession $SessionSave-NetworkSwitchConfiguration -CimSession $SessionRestore-NetworkSwitchConfiguration -CimSession $Session

264.7 Lab: a Linux bridge as a managed switch

  • Bridge switch1 = the switch; each veth pair = one switch port; pc1/pc2/pc3 = network namespaces (.11, .12, .13 in 192.168.64.0/24).
  • bridge, stp and llc were already loaded and veth is built in, so no new kernel module was loaded.
Terminal
ip link add switch1 type bridgeip link set switch1 upip link add pc1-port type veth peer name eth0 netns 51ip link set pc1-port master switch1ip link set pc1-port up# inside pc1:ip addr add 192.168.64.11/24 dev eth0ip link set eth0 up
  • Disable port idea: ip link set pc2-port down -> state disabled, pc2's eth0 DOWN / NO-CARRIER, ping 100% loss.
  • Enable port idea: ip link set pc2-port up -> forwarding again, 3/3 replies; nothing changed inside the PC.
Terminal
ip link set pc2-port downbridge link show dev pc2-portip -br link show eth0               # pc2ping -c 3 -W 1 192.168.64.12        # pc1ip link set pc2-port up
Output
[switchlab]$ bridge link show dev pc2-port5: pc2-port@eth0: <BROADCAST,MULTICAST> mtu 1500 master switch1 state disabled priority 32 cost 2[pc2]$ ip -br link show eth0eth0@if5         DOWN           0e:66:ce:8c:62:40 <NO-CARRIER,BROADCAST,MULTICAST,UP>[pc1]$ ping -c 3 -W 1 192.168.64.12--- 192.168.64.12 ping statistics ---3 packets transmitted, 0 received, 100% packet loss, time 2032ms
  • VLAN idea: vlan_filtering 1 (module list unchanged), pc1/pc2 access VLAN 10, pc3 VLAN 20 -> pc1 reaches pc2, 100% loss to pc3; move pc3 to VLAN 10 -> 3 replies.
  • Feature idea: STP on/off with stp_state 1/0; ip -d link show switch1 is like Global Data.
Terminal
ip link set switch1 type bridge vlan_filtering 1bridge vlan add dev pc1-port vid 10 pvid untaggedbridge vlan del dev pc1-port vid 1# pc2-port: VLAN 10, pc3-port: VLAN 20bridge vlan showping -c 3 -W 1 192.168.64.13   # pc1bridge vlan del dev pc3-port vid 20bridge vlan add dev pc3-port vid 10 pvid untagged
Output
[switchlab]$ bridge vlan showport              vlan-idswitch1           1 PVID Egress Untaggedpc1-port          10 PVID Egress Untaggedpc2-port          10 PVID Egress Untaggedpc3-port          20 PVID Egress Untagged[pc1]$ ping -c 3 -W 1 192.168.64.13--- 192.168.64.13 ping statistics ---3 packets transmitted, 0 received, 100% packet loss, time 2055ms

264.8 Common mistakes

  • Forgetting Save-NetworkSwitchConfiguration: all port and VLAN changes are lost after a reboot.
  • Assuming the module works on any switch: it needs an OMI/CIM agent with the DMTF profile and a CimSession.
  • Using a wildcard in Remove-NetworkSwitchVlan -Name without -WhatIf.
  • Expecting two hosts in the same subnet but different VLANs to talk.

264.9 Interview tips

  • Access port = one VLAN (PC/printer), trunk port = many VLANs on one link (switch to switch), route port = L3 port with IP + mask.
  • Changes lost after reboot? The answer is Save-NetworkSwitchConfiguration (startup config).
  • NetworkSwitchManager talks to the switch's OMI agent via New-CimSession over WS-Man.
  • A VLAN splits one physical switch into separate broadcast domains.