I was recently asked about adding a filter parameter to a cmdlet created through CDXML. If you’ve not seen it before (see PowerShell and WMI Chapters 18 & 19 from www.manning.com) CDXML allows you to creat ecmdlets by wrapping a WMI class in some simple XML.
The resultaing CDXML (Cmdlet Definition XML) is thn published as a module. Here’s a simple example using the Win32_NetworkAdapterConfiguration class
<?xml version=’1.0′ encoding=’utf-8′?>
<PowerShellMetadata xmlns=’http://schemas.microsoft.com/cmdlets-over-objects/2009/11′>
<Class ClassName=’ROOT\cimv2\Win32_NetworkAdapterConfiguration’>
<Version>1.0</Version>
<DefaultNoun>NetworkAdapterConfiguration</DefaultNoun>
<InstanceCmdlets>
<GetCmdletParameters DefaultCmdletParameterSet=’DefaultSet’>
</GetCmdletParameters>
</InstanceCmdlets>
</Class>
</PowerShellMetadata>
The first 2 lines are boilerplate. The NameSpace and WMI class are defined on line 3, follwoed by a version number (arbitary) and a default noun for you cmdlet to use. Instance cmdlets defines how you’ll pull the data for existing instances of the class – in other words the Get-NetworkAdapterConfiguration cmdlet.
Save as a CDXML file and import as a module
Import-Module .\NetworkAdapterConfiguration.cdxml
Get-Module will sjow it as a Cim module with a single exported command. Use it like any other cmdlet
PS> Get-NetworkAdapterConfiguration | ft -a
ServiceName DHCPEnabled Index Description
———– ———– —– ———–
kdnic True 0 Microsoft Kernel Debug Network Adapter
mwlu97w8 True 1 Marvell AVASTAR Wireless Composite Device
msu64w8 False 2 Surface Ethernet Adapter
mwlu97w8 True 3 Marvell AVASTAR 350N Wireless Network Controller
RFCOMM False 4 Bluetooth Device (RFCOMM Protocol TDI)
BthPan True 5 Bluetooth Device (Personal Area Network)
vwifimp True 6 Microsoft Wi-Fi Direct Virtual Adapter
vwifimp True 7 Microsoft Wi-Fi Direct Virtual Adapter
RasSstp False 8 WAN Miniport (SSTP)
RasAgileVpn False 9 WAN Miniport (IKEv2)
Rasl2tp False 10 WAN Miniport (L2TP)
PptpMiniport False 11 WAN Miniport (PPTP)
RasPppoe False 12 WAN Miniport (PPPOE)
NdisWan False 13 WAN Miniport (IP)
NdisWan False 14 WAN Miniport (IPv6)
NdisWan False 15 WAN Miniport (Network Monitor)
Using the cmdlet is equivalent to
Get-CimInstance -ClassName Win32_NetworkAdapterConfiguration
but is easier and requires less typing.
Very often you’ll want to pick a specific adapter – for instance
Get-CimInstance -ClassName Win32_NetworkAdapterConfiguration -Filter ‘Index=3′
You can implement the same kind of filters using CDXML. You add a queryable properties section as shown below:
<?xml version=’1.0′ encoding=’utf-8′?>
<PowerShellMetadata xmlns=’http://schemas.microsoft.com/cmdlets-over-objects/2009/11′>
<Class ClassName=’ROOT\cimv2\Win32_NetworkAdapterConfiguration’>
<Version>1.0</Version>
<DefaultNoun>NetworkAdapterConfiguration</DefaultNoun>
<InstanceCmdlets>
<GetCmdletParameters DefaultCmdletParameterSet=’DefaultSet’>
<QueryableProperties>
<Property PropertyName=’Index’>
<Type PSType =’UInt32’/>
<RegularQuery AllowGlobbing=’true’>
<CmdletParameterMetadata PSName=’Index’ ValueFromPipelineByPropertyName=’true’ CmdletParameterSets=’DefaultSet’ />
</RegularQuery>
</Property>
</QueryableProperties>
</GetCmdletParameters>
</InstanceCmdlets>
</Class>
</PowerShellMetadata>
Set the paraemter name – same as property to use here – and the type (unsigned integer). Decide whether pipeline input and wildcards (globbing) are allowed and save the file.
Re-import the module (use the Force) and your new parameter is available
Get-NetworkAdapterConfiguration -Index 3
Its important to understand CDXML – even if you never create a CDXML module – because 2/3 of the cmdlets in Windows Server 2012 and later are created this way.
The post CDXML filter parameters appeared first on PowerShell for Windows Admins.