Skip to content

Instantly share code, notes, and snippets.

@hkarthik7
Created February 18, 2021 16:43
Show Gist options
  • Save hkarthik7/7579bf8e71b9256ad893d9c04494fa9f to your computer and use it in GitHub Desktop.
Save hkarthik7/7579bf8e71b9256ad893d9c04494fa9f to your computer and use it in GitHub Desktop.
Script to export the results as html report
function Export-Html {
[CmdletBinding(DefaultParameterSetName='Page')]
param(
[Parameter(ValueFromPipeline=$true)]
[psobject]
${InputObject},
[Parameter(Position=0)]
[System.Object[]]
${Property},
[Parameter(ParameterSetName='Page', Position=3)]
[string[]]
${Body},
[Parameter(ParameterSetName='Page', Position=1)]
[string[]]
${Head},
[Parameter(ParameterSetName='Page', Position=2)]
[ValidateNotNullOrEmpty()]
[string]
${Title},
[ValidateNotNullOrEmpty()]
[ValidateSet('Table','List')]
[string]
${As},
[Parameter(ParameterSetName='Page')]
[Alias('cu','uri')]
[ValidateNotNullOrEmpty()]
[uri]
${CssUri},
[Parameter(ParameterSetName='Fragment')]
[ValidateNotNullOrEmpty()]
[switch]
${Fragment},
[ValidateNotNullOrEmpty()]
[string[]]
${PostContent},
[ValidateNotNullOrEmpty()]
[string[]]
${PreContent},
[ValidateNotNullOrEmpty()]
[string]
$FilePath
)
begin
{
try {
$outBuffer = $null
if ($PSBoundParameters.TryGetValue('OutBuffer', [ref]$outBuffer))
{
$PSBoundParameters['OutBuffer'] = 1
}
$wrappedCmd = $ExecutionContext.InvokeCommand.GetCommand('ConvertTo-Html', [System.Management.Automation.CommandTypes]::Cmdlet)
if($PSBoundParameters['FilePath']) {
$null = $PSBoundParameters.Remove('FilePath')
if($PSBoundParameters['CssUri']) {
$null = $PSBoundParameters.Remove('CssUri')
}
if($PSBoundParameters['Head']) {
$null = $PSBoundParameters.Remove('Head')
$header = "
<div class='shadow-sm p-3 mb-5 bg-white rounded'>
<nav class='navbar navbar-dark bg-dark'>
<a class='navbar-brand'>$($Head)</a>
</nav>
</div>
"
}
if($PSBoundParameters['PreContent']) {
$null = $PSBoundParameters.Remove('PreContent')
$Content = "
<div class='card'>
<div class='card-body'>
<h4 class='card-title'>$($PreContent)</h4>
<hr>
<div class='table-responsive'>
"
}
$ContentPost = "
</div>
</div>
</div>
<script src='https://code.jquery.com/jquery-3.4.1.slim.min.js' integrity='sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n' crossorigin='anonymous'></script>
<script src='https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js' integrity='sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo' crossorigin='anonymous'></script>
<script src='https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js' integrity='sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6' crossorigin='anonymous'></script>
"
$scriptCmd = { & $wrappedCmd @PSBoundParameters -CssUri "https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" `
-Head $header `
-PreContent $Content `
-PostContent $ContentPost `
| ForEach-Object {
if($_ -like "*<table>*") { $_ -replace "<table>", '<table class="table table-bordered table-hover">' }
else { $_ }
} | Out-File -FilePath $FilePath }
} else { $scriptCmd = { & $wrappedCmd @PSBoundParameters } }
$steppablePipeline = $scriptCmd.GetSteppablePipeline($myInvocation.CommandOrigin)
$steppablePipeline.Begin($PSCmdlet)
} catch {
throw
}
}
process
{
try {
$steppablePipeline.Process($_)
} catch {
throw
}
}
end
{
try {
$steppablePipeline.End()
} catch {
throw
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment