Powershell:如何为网站创建 link

Powershell: how to create a link to a website

我有一个小问题。现在我有以下代码:

$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
}

我还有以下 xml 文件:

<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/%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>

这就是我的输出:

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

我现在有两个问题。并非我的所有组件都有许可证。我现在想为没有许可证的组件写“No License”。 我将不得不做这样的事情。

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

不幸的是,我的解决方案还没有完全奏效。

我的第二个问题是我也想输出引用。 我可以很简单地做到这一点:

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

现在在这里它逻辑上输出所有对我的引用。但是,我只想要 type="website".

的参考

在下一步中,我想 link 带有名称的引用。这样,当您单击该名称时,它会将您带到相应的网站。我问了google。在这里我只找到了这个:

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

但是,这很可能不是正确的方法。

有什么办法可以解决吗?

感谢您的帮助。

|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)|

您的 XML 示例缺少最后两个结束标记。

与您的 一样,您可以使用这个:

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

输出: