I've asked the same question before now I'll try to make my question a little more specific. My goal is to create a md file from a bom (xml) file using Powershell. From the bom file three values should be read out (name, version, license). Here is the following code:
[XML]$xml = Get-Content $XMLfile
$xml.components.component | ForEach-Object {
[PSCustomObject]@{
'Name' = $_.name
'Version' = $_.version
'License' = $_.license
}
}
The code should be correct in my opinion to read out the desired data or not? Now I get the following error code:
Cannot convert value "System.Object[]" to type "System.Xml.XmlDocument". Error: "'=' is an unexpected token. The expected token is ';'. Line 1820, position 60."
+ [xml]$xml = Get-Content $xmlFile
Could it be that an error code occurs because the bom file uses a different Unicode(UTF-16-LE)? Here is a part of the XML file:
<bom xmlns="http://cyclonedx.org/schema/bom/1.2" serialNumber="urn:uuid:cdd205c6-beb8-49db-998d-884cfeef678c" version="1">
<metadata>
<timestamp>2021-08-20T23:41:11.390Z</timestamp>
<tools>
<tool>
<vendor>CycloneDX</vendor>
<name>Node.js module</name>
<version>2.0.0</version>
</tool>
</tools>
</metadata>
<components>
<component type="library" bom-ref="pkg:npm/%40mdi/font@4.9.95">
<group>@mdi</group>
<name>font</name>
<version>4.9.95</version>
<description>
<![CDATA[ Dist for Material Design Webfont. This includes the Stock and Community icons in a single webfont collection. ]]>
</description>
<hashes>
<hash alg="SHA-512">9b6b1b02cf923304675a990cae207112e970ba19aa47287a5fe85d39996a4b161950cd82d936839d0e20722973768020aeed97627595899ff13ee4831a045757</hash>
</hashes>
<purl>pkg:npm/%40mdi/font@4.9.95</purl>
<externalReferences>
<reference type="website">
<url>https://materialdesignicons.com</url>
</reference>
<reference type="issue-tracker">
<url>https://github.com/Templarian/MaterialDesign/issues</url>
</reference>
<reference type="vcs">
<url>git+https://github.com/Templarian/MaterialDesign-Webfont.git</url>
</reference>
</externalReferences>
</component>
<component type="library" bom-ref="pkg:npm/%40storybook/addon-a11y@6.2.9">
<group>@storybook</group>
<name>addon-a11y</name>
<version>6.2.9</version>
<description>
<![CDATA[ Test component compliance with web accessibility standards ]]>
</description>
<hashes>
<hash alg="SHA-512">c28ee716912a11e8870ec44a9e1a9eda020767d67bfc079fc39ef4901811794e6d2a59ab6f9ac501f4d50561816652c759e24cb05b11b16ad6ae691fd41e7639</hash>
</hashes>
<licenses>
<license>
<id>MIT</id>
</license>
</licenses>
<purl>pkg:npm/%40storybook/addon-a11y@6.2.9</purl>
<externalReferences>
<reference type="website">
<url>https://github.com/storybookjs/storybook#readme</url>
</reference>
<reference type="issue-tracker">
<url>https://github.com/storybookjs/storybook/issues</url>
</reference>
<reference type="vcs">
<url>git+https://github.com/storybookjs/storybook.git</url>
</reference>
</externalReferences>
</component>
<component type="library" bom-ref="pkg:npm/%40storybook/addons@6.2.9">
<group>@storybook</group>
<name>addons</name>
<version>6.2.9</version>
<description>
<![CDATA[ Storybook addons store ]]>
</description>
<hashes>
<hash alg="SHA-512">1a798429b27088dd639dc37d35203c0ae4758b65c095ab0f725fd99f48e47d5f568ad41e73355c2423f0f3a4861fce000feb53042c85da2f540b429a395d5805</hash>
</hashes>
<licenses>
<license>
<id>MIT</id>
</license>
</licenses>
<purl>pkg:npm/%40storybook/addons@6.2.9</purl>
<externalReferences>
<reference type="website">
<url>https://github.com/storybookjs/storybook/tree/master/lib/addons</url>
</reference>
<reference type="issue-tracker">
<url>https://github.com/storybookjs/storybook/issues</url>
</reference>
<reference type="vcs">
<url>git+https://github.com/storybookjs/storybook.git</url>
</reference>
</externalReferences>
</component>
<component type="library" bom-ref="pkg:npm/%40storybook/api@6.2.9">
<group>@storybook</group>
<name>api</name>
<version>6.2.9</version>
<description>
<![CDATA[ Core Storybook API & Context ]]>
</description>
<hashes>
<hash alg="SHA-512">a24900dc7012704f6d1a7601ae34ce720cd3f8bd65447368121dd97c6821d6efd7344c8718d923e20aef91dea75fb07345c610fe5d9591c282aa63a35274a455</hash>
</hashes>
<licenses>
<license>
<id>MIT</id>
</license>
</licenses>
<purl>pkg:npm/%40storybook/api@6.2.9</purl>
<externalReferences>
<reference type="website">
<url>https://github.com/storybookjs/storybook/tree/master/lib/api</url>
</reference>
<reference type="issue-tracker">
<url>https://github.com/storybookjs/storybook/issues</url>
</reference>
<reference type="vcs">
<url>git+https://github.com/storybookjs/storybook.git</url>
</reference>
</externalReferences>
</component>
<component type="library" bom-ref="pkg:npm/%40reach/router@1.3.4">
<group>@reach</group>
<name>router</name>
<version>1.3.4</version>
<description>
<![CDATA[ Next generation Routing for React. ]]>
</description>
<hashes>
<hash alg="SHA-512">fa6b67f708e507d34dd823679c2fc1458b7074a05f4b2c9ab0f62b68d032bda575a1c72bff9367078095ce31198a9347c1879b430727746526a45cf1014a2a48</hash>
</hashes>
<licenses>
<license>
<id>MIT</id>
</license>
</licenses>
<purl>pkg:npm/%40reach/router@1.3.4</purl>
<externalReferences>
<reference type="website">
<url>https://github.com/reach/router#readme</url>
</reference>
<reference type="issue-tracker">
<url>https://github.com/reach/router/issues</url>
</reference>
<reference type="vcs">
<url>git+https://github.com/reach/router.git</url>
</reference>
</externalReferences>
</component>
<component type="library" bom-ref="pkg:npm/create-react-context@0.3.0">
<name>create-react-context</name>
<version>0.3.0</version>
<description>
<![CDATA[ Polyfill for the proposed React context API ]]>
</description>
<hashes>
<hash alg="SHA-512">74d95d2284ae352be54c9eec948282fd914629ec41301aeb71cf934ded4d76644e9da012b8b3efaa9c23f6fe174b8b9767cf983eed2c3664a15f6ad72392cdb3</hash>
</hashes>
<licenses>
<license>
<id>MIT</id>
</license>
</licenses>
<purl>pkg:npm/create-react-context@0.3.0</purl>
<externalReferences>
<reference type="website">
<url>https://github.com/thejameskyle/create-react-context#readme</url>
</reference>
<reference type="issue-tracker">
<url>https://github.com/thejameskyle/create-react-context/issues</url>
</reference>
<reference type="vcs">
<url>git+https://github.com/thejameskyle/create-react-context.git</url>
</reference>
</externalReferences>
</component>
<component type="library" bom-ref="pkg:npm/gud@1.0.0">
<name>gud</name>
<version>1.0.0</version>
<description>
<![CDATA[ Create a 'gud nuff' (not cryptographically secure) globally unique id ]]>
</description>
<hashes>
<hash alg="SHA-512">cc610e54a14ce6c54f3eb62cec9e7f858130d8fa1ff0a0b23b0ca11bcb00176ea60807941407183eed66c01ee186920fdbdcce201683ab0b8774ba748ef6578f</hash>
</hashes>
<licenses>
<license>
<id>MIT</id>
</license>
</licenses>
<purl>pkg:npm/gud@1.0.0</purl>
<externalReferences>
<reference type="website">
<url>https://github.com/jamiebuilds/global-unique-id#readme</url>
</reference>
<reference type="issue-tracker">
<url>https://github.com/jamiebuilds/global-unique-id/issues</url>
</reference>
<reference type="vcs">
<url>git+https://github.com/jamiebuilds/global-unique-id.git</url>
</reference>
</externalReferences>
</component>
<component type="library" bom-ref="pkg:npm/warning@4.0.3">
<name>warning</name>
<version>4.0.3</version>
<description>
<![CDATA[ A mirror of Facebook's Warning ]]>
</description>
<hashes>
<hash alg="SHA-512">ae9272376db629622f1c9fc5e775d266fd1997f69c72a1d1f1eb7592968c4c3fdf2c2471b55f225fc73333363bb1566ea53237cdc51383c7b2712da4345f65eb</hash>
</hashes>
<licenses>
<license>
<id>MIT</id>
</license>
</licenses>
<purl>pkg:npm/warning@4.0.3</purl>
<externalReferences>
<reference type="website">
<url>https://github.com/BerkeleyTrue/warning</url>
</reference>
<reference type="issue-tracker">
<url>https://github.com/BerkeleyTrue/warning/issues</url>
</reference>
<reference type="vcs">
<url>git+https://github.com/BerkeleyTrue/warning.git</url>
</reference>
</externalReferences>
</component>
<component type="library" bom-ref="pkg:npm/loose-envify@1.4.0">
<name>loose-envify</name>
<version>1.4.0</version>
<description>
<![CDATA[ Fast (and loose) selective `process.env` replacer using js-tokens instead of an AST ]]>
</description>
<hashes>
<hash alg="SHA-512">972bb13c6aff59f86b95e9b608bfd472751cd7372a280226043cee918ed8e45ff242235d928ebe7d12debe5c351e03324b0edfeb5d54218e34f04b71452a0add</hash>
</hashes>
<licenses>
<license>
<id>MIT</id>
</license>
</licenses>
<purl>pkg:npm/loose-envify@1.4.0</purl>
<externalReferences>
<reference type="website">
<url>https://github.com/zertosh/loose-envify</url>
</reference>
<reference type="issue-tracker">
<url>https://github.com/zertosh/loose-envify/issues</url>
</reference>
<reference type="vcs">
<url>git://github.com/zertosh/loose-envify.git</url>
</reference>
</externalReferences>
</component>
<component type="library" bom-ref="pkg:npm/js-tokens@4.0.0">
<name>js-tokens</name>
<version>4.0.0</version>
<description>
<![CDATA[ A regex that tokenizes JavaScript. ]]>
</description>
<hashes>
<hash alg="SHA-512">45d2547e5704ddc5332a232a420b02bb4e853eef5474824ed1b7986cf84737893a6a9809b627dca02b53f5b7313a9601b690f690233a49bce0e026aeb16fcf29</hash>
</hashes>
<licenses>
<license>
<id>MIT</id>
</license>
</licenses>
<purl>pkg:npm/js-tokens@4.0.0</purl>
<externalReferences>
<reference type="website">
<url>https://github.com/lydell/js-tokens#readme</url>
</reference>
<reference type="issue-tracker">
<url>https://github.com/lydell/js-tokens/issues</url>
</reference>
<reference type="vcs">
<url>git+https://github.com/lydell/js-tokens.git</url>
</reference>
</externalReferences>
</component>
<component type="library" bom-ref="pkg:npm/invariant@2.2.4">
<name>invariant</name>
<version>2.2.4</version>
<description>
<![CDATA[ invariant ]]>
</description>
<hashes>
<hash alg="SHA-512">a6125f41506e689339ada3a926349f9220fa0696c213836cfff2da5e5eb0198b54058f379d64ba45ff6d5e6d9ef1568aeb42448d895d6cf89ffc0d81d42da034</hash>
</hashes>
<licenses>
<license>
<id>MIT</id>
</license>
</licenses>
<purl>pkg:npm/invariant@2.2.4</purl>
<externalReferences>
<reference type="website">
<url>https://github.com/zertosh/invariant#readme</url>
</reference>
<reference type="issue-tracker">
<url>https://github.com/zertosh/invariant/issues</url>
</reference>
<reference type="vcs">
<url>git+https://github.com/zertosh/invariant.git</url>
</reference>
</externalReferences>
</component>
</components>
</bom>
I was able to format the xml file correctly and it should work now. My code now looks like this:
$xml = 'C:\Users\jonb\Desktop\bom.xml'
$ns = @{a = 'http://cyclonedx.org/schema/bom/1.2'}
$xPath = '//a:component'
$components = Select-Xml -Xml $xml -XPath $xPath -Namespace $ns
foreach ($component in $components) {
$name = Select-Xml -Xml $component.Node -XPath './/a:name/text()'-Namespace $ns
$version = Select-Xml -Xml $component.Node -XPath './/a:version/text()'-Namespace $ns
$lic_cond = Select-Xml -Xml $component.Node -XPath './/a:license/a:id/text()'-Namespace $ns
$license = $(If ($lic_cond) {$lic_cond} Else {"NA"})
$finalObject = [pscustomobject]@{
'Name' = $name
'Version' = $version
'License' = $license
}
Write-Output $finalObject
}
I now have the following error message:
Select-Xml : Cannot bind parameter 'Xml'. Cannot convert the "C:\Users\jonb\Desktop\bom.xml" value of type "System.String" to type "System.Xml.XmlNode".
At C:\Users\jonb\Bobby Jones\Programme\Powershell\Untitled-1.ps1:4 char:31
+ $components = Select-Xml -Xml $xml -XPath $xPath -Namespace $ns
+ ~~~~
+ CategoryInfo : InvalidArgument: (:) [Select-Xml], ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.SelectXmlCommand
Sorry for the inconvenience, I don't know Powershell at all.