CDXML–scripting creation

So far you’ve seen how to create CDXML files by hand – though you probably used cut and paste rather than typing everything from scratch.

Its time to bring a bit of automation to creating CDXML files. The XML is fairly simple and you’re only changing a couple of values so you can do this:

function new-cdxml {
param (
[string]$namespace = ‘ROOT\cimv2’,
[string]$path = “C:\Scripts\Modules\Hardware”

$code = @”
<?xml version=’1.0′ encoding=’utf-8′?>
<PowerShellMetadata xmlns=’′>
  <Class ClassName=’$namespace\$class’>

      <GetCmdletParameters DefaultCmdletParameterSet=’DefaultSet’>

$file = Join-Path -Path $path -ChildPath “$class.cdxml”
Write-Verbose -Message  $file
Set-Content -Path $file -Value $code


I saved this as NewCDXML.ps1.  This will eventually  become the basis of a CDXML file creation module. I set defaults on the namespace and the path – feel free to change them if required.

The function is run as:

new-cdxml -class Win32_DiskDrive -noun PhysicalDisk

which produces this CDXML file

<?xml version=’1.0′ encoding=’utf-8′?>
<PowerShellMetadata xmlns=’′>
  <Class ClassName=’ROOT\cimv2\Win32_DiskDrive’>

      <GetCmdletParameters DefaultCmdletParameterSet=’DefaultSet’>


The Hardware.psd1 file needs to be altered:

# Modules to import as nested modules of the module specified in RootModule/ModuleToProcess
NestedModules = @(‘Win32_BIOS.cdxml’,



# Functions to export from this module
FunctionsToExport = @(‘Get-Bios’,


The module now looks like this:

£> Get-Command -Module Hardware

CommandType      Name
———–                —-
Function                Get-Bios
Function                Get-ComputerSystem
Function                Get-PhysicalDisk


This makes creating additional components for your module much easier.

This entry was posted in CDXML, CIM, PowerShell and WMI, PowerShell V3, PowerShell v4. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s