Added synopsis, and tests the value is a scriptblock

This commit is contained in:
dfinke
2020-05-23 17:32:22 -04:00
parent a5a98d5c92
commit 001aabf526

View File

@@ -1,6 +1,19 @@
function Export-MultipleExcelSheets { function Export-MultipleExcelSheets {
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingPlainTextForPassword", "")] <#
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseSingularNouns", "", Justification="No suitable singular")] .Synopsis
Takes a hash table of scriptblocks and exports each as a sheet in an Excel file
.Example
$p = Get-Process
$InfoMap = @{
PM = { $p | Select-Object company, pm }
Handles = { $p | Select-Object company, handles }
Services = { Get-Service }
}
Export-MultipleExcelSheets -Path $xlfile -InfoMap $InfoMap -Show -AutoSize
#>
param( param(
[Parameter(Mandatory = $true)] [Parameter(Mandatory = $true)]
$Path, $Path,
@@ -18,10 +31,15 @@ function Export-MultipleExcelSheets {
$parameters.Path = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($Path) $parameters.Path = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($Path)
foreach ($entry in $InfoMap.GetEnumerator()) { foreach ($entry in $InfoMap.GetEnumerator()) {
Write-Progress -Activity "Exporting" -Status "$($entry.Key)" if ($entry.Value -is [scriptblock]) {
$parameters.WorkSheetname = $entry.Key Write-Progress -Activity "Exporting" -Status "$($entry.Key)"
$parameters.WorkSheetname = $entry.Key
& $entry.Value | Export-Excel @parameters & $entry.Value | Export-Excel @parameters
}
else {
Write-Warning "$($entry.Key) not exported, needs to be a scriptblock"
}
} }
if ($Show) { Invoke-Item $Path } if ($Show) { Invoke-Item $Path }