Skip to content

Add CIM-based Get-SdnVMCim and Get-SdnVMNetworkAdapterCim for faster VM enumeration#605

Draft
arudell wants to merge 7 commits into
microsoft:mainfrom
arudell:arudell-microsoft-cim-vm-adapter-perf
Draft

Add CIM-based Get-SdnVMCim and Get-SdnVMNetworkAdapterCim for faster VM enumeration#605
arudell wants to merge 7 commits into
microsoft:mainfrom
arudell:arudell-microsoft-cim-vm-adapter-perf

Conversation

@arudell

@arudell arudell commented Jul 15, 2026

Copy link
Copy Markdown
Member

Summary

Introduces two new functions that use direct CIM/WMI queries against the root/virtualization/v2 namespace instead of Get-VM and Get-VMNetworkAdapter cmdlets. This provides significantly faster performance on hosts with large numbers of VMs where the Hyper-V cmdlets often timeout.

Changes

  • Get-SdnVMCim: Retrieves VMs with state, health, and network adapter info via Msvm_ComputerSystem
  • Get-SdnVMNetworkAdapterCim: Retrieves VM NICs (synthetic + emulated + management OS) via Msvm_SyntheticEthernetPortSettingData

Both functions support -ComputerName and -Credential for remote execution, and -VMName/-MacAddress filtering. The original Get-SdnVMNetworkAdapter is preserved for backward compatibility.

Why CIM over Hyper-V cmdlets

The Get-VM and Get-VMNetworkAdapter cmdlets internally perform extensive object hydration and property enumeration that is unnecessary for diagnostics scenarios. Direct CIM queries return the needed data with minimal overhead, avoiding timeout exceptions on heavily loaded hosts.

Closes #404

arudell and others added 7 commits July 15, 2026 09:29
…VM enumeration

Introduces two new functions that use direct CIM/WMI queries against the
root/virtualization/v2 namespace instead of Get-VM and Get-VMNetworkAdapter
cmdlets. This provides significantly faster performance on hosts with large
numbers of VMs where the Hyper-V cmdlets often timeout.

- Get-SdnVMCim: Retrieves VMs with state, health, and network adapter info
- Get-SdnVMNetworkAdapterCim: Retrieves VM NICs (synthetic + emulated)

Both functions support -ComputerName and -Credential for remote execution,
and -VMName/-MacAddress filtering. The original Get-SdnVMNetworkAdapter is
preserved for backward compatibility.

Closes microsoft#404

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 0881b5f1-0c2d-470d-ab0c-6da9f8744d9b
Replace Get-VM and Get-VMNetworkAdapter calls in Get-ServerConfigState with
the new Get-SdnVMCim and Get-SdnVMNetworkAdapterCim functions. This avoids
the slow Hyper-V cmdlet overhead when collecting config state on hosts with
many VMs.

The management OS adapter collection now also uses the CIM path, removing
the need for individual Get-VMNetworkAdapterIsolation, TeamMapping, VLAN,
and RoutingDomainMapping calls that each re-enumerate all adapters.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 220ab25a-0e38-4217-8e3c-e0a2e22fd7d9
- Get-SdnVMNetworkAdapterPortProfile now uses Get-SdnVMNetworkAdapterCim
  and queries Msvm_EthernetSwitchPortSecuritySettingData directly instead
  of calling Get-VMSwitchExtensionPortFeature per adapter.
- Set-SdnVMNetworkAdapterPortProfile uses CIM to locate adapters and
  update existing port security settings via Set-CimInstance. Falls back
  to Hyper-V cmdlets only for initial port profile creation.

This eliminates the per-adapter Get-VMSwitchExtensionPortFeature and
Get-VMSwitchExtensionPortData calls that compound the enumeration
overhead on hosts with many VMs.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 220ab25a-0e38-4217-8e3c-e0a2e22fd7d9
Resolve the virtual switch name by querying Msvm_VirtualEthernetSwitch
and Msvm_EthernetPortAllocationSettingData. The port allocation's Parent
property links to the adapter and HostResource links to the switch,
allowing us to build a lookup table for both VM and ManagementOS adapters.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 220ab25a-0e38-4217-8e3c-e0a2e22fd7d9
Add a dedicated Get-SdnVMSwitchCim function that queries
Msvm_VirtualEthernetSwitch and returns switch name, GUID, and metadata.
Supports -Name filtering, -ComputerName/-Credential for remote access,
and -CimSession for reusing an existing session.

Update Get-SdnVMNetworkAdapterCim to call Get-SdnVMSwitchCim for the
switch name lookup instead of querying the CIM class directly.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 220ab25a-0e38-4217-8e3c-e0a2e22fd7d9
…mSession

Add New-SdnCimSession and Remove-SdnCimSession to SdnDiag.Utilities.psm1,
mirroring the existing PSRemotingSession pattern. Sessions are named
SdnDiag-Cim-* and reused when TestConnection() passes.

Update Get-SdnVMSwitchCim, Get-SdnVMNetworkAdapterCim, and Get-SdnVMCim
to use the centralized session management instead of inline New-CimSession
calls. Remove per-function finally cleanup blocks since sessions are now
managed centrally via Remove-SdnCimSession.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 220ab25a-0e38-4217-8e3c-e0a2e22fd7d9
When -All is specified, the function returns both VM network adapters
and Management OS adapters together, matching the behavior of
Get-VMNetworkAdapter -All. This fixes the bound parameter error when
Get-SdnVMNetworkAdapterPortProfile -All passes through to the CIM function.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 220ab25a-0e38-4217-8e3c-e0a2e22fd7d9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Returning list of VMs and VMNetworkAdapters is slow

1 participant