Home > Software design >  Powershell: how to create a link to a website
Powershell: how to create a link to a website

Time:09-17

I have a litte problem. Right now I have the following code:

$xmlFile        = 'C:\Users\jonb\Desktop\bom.xml'
    
    [xml]$xml = Get-Content $xmlFile

    $xml.bom.components.component | ForEach-Object {
        $finalObject = [PSCustomObject]@{
        'Name'      = $_.name
        'Version'   = $_.version
        'License'   = $_.licenses.license.id
    }
    Write-Output $finalObject
}

I have also the following xml file:

<bom xmlns="http://cyclonedx.org/schema/bom/1.2" serialNumber="urn:uuid:7994040c-b12e-4d3d-96a2-b2112e725d0b" version="1">
<metadata>
<timestamp>2021-08-25T23:47:51.782Z</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/@mdi/[email protected]">
<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/@mdi/[email protected]</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/@storybook/[email protected]">
<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/@storybook/[email protected]</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/@storybook/[email protected]">
<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/@storybook/[email protected]</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/@storybook/[email protected]">
<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/@storybook/[email protected]</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/@reach/[email protected]">
<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/@reach/[email protected]</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/[email protected]">
<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/[email protected]</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/[email protected]">
<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/[email protected]</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/[email protected]">
<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/[email protected]</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/[email protected]">
<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/[email protected]</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>

And thats my output:

Name                 Version License
----                 ------- -------
font                 4.9.95
addon-a11y           6.2.9   MIT
addons               6.2.9   MIT
api                  6.2.9   MIT
router               1.3.4   MIT
create-react-context 0.3.0   MIT
gud                  1.0.0   MIT
warning              4.0.3   MIT
loose-envify         1.4.0   MIT

I now have two problems. Not all of my components have a license. I would now like to write "No License" for the components that do not have a license. I would have to do something like this.

$license = $(if ($lic_cond) {$lic_cond} else {"No license"})

Unfortunately, my solutions don't quite work yet.

My second problem is that I also want to output the reference. I can do that quite simply with so:

$xml.bom.components.component | ForEach-Object {
    [PSCustomObject]@{
        'Name' = $_.name
        'Version' = $_.version
        'License' = $_.licenses.license.id
        'Website' = $_.externalReferences.reference.url
    }
}
}

Now here it logically outputs all references to me. However, I only want the reference with the type="website".

In a further step I would like to link the reference with the name. So that when you click on the name it takes you to the corresponding website. I asked google. Here I found only this:

New-Item -ItemType Junction -Path "C:\LINK" -Target "C:\SOURCE".

However, this is most likely not the way to go.

Are there any ways to solve this?

Thanks for your help.

|Name|Version|License|Website|
|----|-------|-------|-------|
|@mdi/font|4.9.95|No license|[TheNameForTheUrl](https://materialdesignicons.com)|
|@storybook/addon-a11y|6.2.9|MIT|[TheNameForTheUrl](https://github.com/storybookjs/storybook#readme)|
|@storybook/addons|6.2.9|MIT|[TheNameForTheUrl](https://github.com/storybookjs/storybook/tree/master/lib/addons)|
|@storybook/api|6.2.9|MIT|[TheNameForTheUrl](https://github.com/storybookjs/storybook/tree/master/lib/api)|
|@reach/router|1.3.4|MIT|[TheNameForTheUrl](https://github.com/reach/router#readme)|

CodePudding user response:

Your XML example was missing the last two closing tags.

As with your previous question, You can use this:

function ConvertTo-MarkDownTable {
    [CmdletBinding()] param(
        [Parameter(Mandatory = $true, ValueFromPipeline = $true, Position = 0)] 
        $InputObject
    )
    Begin { 
        $headersDone = $false
        $pattern = '(?<!\\)\|'  # escape every '|' unless already escaped
    }
    Process {
        if (!$headersDone) {
            $headersDone = $true
            # output the header line and below that a dashed line
            # -replace '(?<!\\)\|', '\|' escapes every '|' unless already escaped
            '|{0}|' -f (($_.PSObject.Properties.Name -replace $pattern, '\|') -join '|')
            '|{0}|' -f (($_.PSObject.Properties.Name -replace '.', '-') -join '|')
        }
        '|{0}|' -f (($_.PsObject.Properties.Value -replace $pattern, '\|') -join '|')
    }
}

$finalObject = $xml.bom.components.component | ForEach-Object {
    $lic = $_.licenses.license.id
    $web = ($_.externalReferences.reference | Where-Object { $_.type -eq 'website' }).url
    [PSCustomObject]@{
        'Name'    = $_.name
        'Version' = $_.version
        'License' = if ([string]::IsNullOrWhiteSpace($lic)) {'No license' } else {$lic}
        'Website' = if ($web) { '[{0}]({0})' -f $web }
    }
}
# convert to markdown
$finalObject | ConvertTo-MarkDownTable

Output:

  • Related