PowerShell for Windows Admins

Dec 5 2013   2:33PM GMT

CDXML–scripting creation

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

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 {

[CmdletBinding()]

param (

[string]$class,

[string]$noun,

[string]$namespace = ‘ROOT\cimv2′,

[string]$path = “C:\Scripts\Modules\Hardware”

)

$code = @”

<?xml version=’1.0′ encoding=’utf-8′?>

<PowerShellMetadata xmlns=’http://schemas.microsoft.com/cmdlets-over-objects/2009/11′>

<Class ClassName=’$namespace\$class’>

<Version>1.0</Version>

<DefaultNoun>$noun</DefaultNoun>

<InstanceCmdlets>

<GetCmdletParameters DefaultCmdletParameterSet=’DefaultSet’>

</GetCmdletParameters>

</InstanceCmdlets>

</Class>

</PowerShellMetadata>

“@

$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=’http://schemas.microsoft.com/cmdlets-over-objects/2009/11′>

<Class ClassName=’ROOT\cimv2\Win32_DiskDrive’>

<Version>1.0</Version>

<DefaultNoun>PhysicalDisk</DefaultNoun>

<InstanceCmdlets>

<GetCmdletParameters DefaultCmdletParameterSet=’DefaultSet’>

</GetCmdletParameters>

</InstanceCmdlets>

</Class>

</PowerShellMetadata>

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’,

‘Win32_ComputerSystem.cdxml’,

‘Win32_DiskDrive.cdxml’

)

and

# Functions to export from this module

FunctionsToExport = @(‘Get-Bios’,

‘Get-ComputerSystem’,

‘Get-PhysicalDisk’

)

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.

2  Comments on this Post

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.
  • dmcdonough
    Is there a way to add filter parameters? I haven't seen much expansion on the InstanceCmdlets block.
    0 pointsBadges:
    report
  • Richard Siddaway
    see http://itknowledgeexchange.techtarget.com/powershell/cdxml-filter-parameters/ for an example
    6,705 pointsBadges:
    report

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

Share this item with your network: