本文介绍了将 Powershell 输出转换为 Markdown 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下代码:
$xmlFile = 'C:\Users\kraer\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
}
现在我想将我的 $finalObject 转换为 MarkDown 表.这里有什么可能吗?
Now I would like to convert my $finalObject to a MarkDown Table. Are there any possibilities here?
对于另一个问题,我收到了这个答案,但现在它不适用于我的代码.
For another question I received this answer but right now it doesn't work for my code.
function ConvertTo-MarkDownTable {
[CmdletBinding()] param(
[Parameter(Position = 0, ValueFromPipeLine = $True)] $InputObject
)
Begin { $Init = $True }
Process {
if ( $Init ) {
$Init = $False
$_.PSObject.Properties.Name -Join '|'
$_.PSObject.Properties.ForEach({ '-' }) -Join '|'
}
$_.PSObject.Properties.Value -Join '|'
}
}
您有其他解决方案吗?
感谢您的帮助
推荐答案
不知道你的 bom.xml 的内容,你可以试试这个稍微修改过的函数版本:
Not knowing the contents of your bom.xml, you might try this slightly adapted version of the function:
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 '|')
}
}
用法:
# load the xml from file
$xml= New-Object System.XML.XMLDocument
$xml.Load('C:\Users\kraer\Desktop\bom.xml')
$finalObject = $xml.bom.components.component | ForEach-Object {
[PSCustomObject]@{
'Name' = $_.name
'Version' = $_.version
'License' = $_.licenses.license.id
}
}
# convert to markdown
$finalObject | ConvertTo-MarkDownTable
附言$_.licenses.license.id
可能是错误的,因为它看起来像 licenses
是一个 array 许可证.你可能想在这里做这样的事情:
P.S. $_.licenses.license.id
might be wrong, because it looks like licenses
is an array of licences. You would probably want to do something like this here:
($_.licenses | ForEach-Object { $_.license.id }) -join '; '
这篇关于将 Powershell 输出转换为 Markdown 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!