PoshCode Logo PowerShell Code Repository

Export-HTML by Dmitry Sotnikov 4 years ago
embed code: <script type="text/javascript" src="http://PoshCode.org/embed/1380"></script>download | new post

This is ConvertTo-HTML with -Path parameter added to specify the output file. E.g.
Get-Process | Export-Html -Path C:\temp\processes.html
Get-Process | Export-Html C:\temp\processes.html -Title ‘My Processes’
Get-Process | Export-Html C:\temp\processes.html -Property Name, Handles -Title ‘My Processes’

  1. #Requires -Version 2.0
  2.  
  3. <#
  4.         Export-Html behaves exactly like native ConvertTo-HTML
  5.         However it has one optional parameter -Path
  6.         Which lets you specify the output file: e.g.
  7.         Get-Process | Export-Html C:\temp\processes.html
  8.        
  9.         (c) Dmitry Sotnikov
  10.         http://dmitrysotnikov.wordpress.com
  11.        
  12.         Proxy generated using PowerGUI Script Editor code snippets:
  13.         http://poshoholic.com/2009/08/28/learn-powershell-v2-features-using-powershell-code-snippets/
  14.        
  15.         All changes to the proxy commented
  16.        
  17.         This sample from Kirk used for inspiration:
  18.         http://poshoholic.com/2009/09/18/powershell-3-0-why-wait-importing-typed-objects-with-typed-properties-from-a-csv-file/
  19. #>
  20.  
  21. function Export-Html {
  22. [CmdletBinding(DefaultParameterSetName='Page')]
  23. param(
  24.     [Parameter(ValueFromPipeline=$true)]
  25.     [System.Management.Automation.PSObject]
  26.     ${InputObject},
  27.  
  28. # Adding Path parameter
  29. # (made it Position 0, and incremented Position for others)
  30.     [Parameter(Position=0)]
  31.     [Alias('PSPath', 'FilePath')]
  32.     [ValidateNotNullOrEmpty()]
  33.     [System.String]
  34.     ${Path},
  35.  
  36.     [Parameter(Position=1)]
  37.     [ValidateNotNullOrEmpty()]
  38.     [System.Object[]]
  39.     ${Property},
  40.  
  41.     [Parameter(ParameterSetName='Page', Position=4)]
  42.     [ValidateNotNullOrEmpty()]
  43.     [System.String[]]
  44.     ${Body},
  45.  
  46.     [Parameter(ParameterSetName='Page', Position=2)]
  47.     [ValidateNotNullOrEmpty()]
  48.     [System.String[]]
  49.     ${Head},
  50.  
  51.     [Parameter(ParameterSetName='Page', Position=3)]
  52.     [ValidateNotNullOrEmpty()]
  53.     [System.String]
  54.     ${Title},
  55.  
  56.     [ValidateSet('Table','List')]
  57.     [ValidateNotNullOrEmpty()]
  58.     [System.String]
  59.     ${As},
  60.  
  61.     [Parameter(ParameterSetName='Page')]
  62.     [Alias('cu','uri')]
  63.     [ValidateNotNullOrEmpty()]
  64.     [System.Uri]
  65.     ${CssUri},
  66.  
  67.     [Parameter(ParameterSetName='Fragment')]
  68.     [ValidateNotNullOrEmpty()]
  69.     [Switch]
  70.     ${Fragment},
  71.  
  72.     [ValidateNotNullOrEmpty()]
  73.     [System.String[]]
  74.     ${PostContent},
  75.  
  76.     [ValidateNotNullOrEmpty()]
  77.     [System.String[]]
  78.     ${PreContent})
  79.  
  80. begin
  81. {
  82.     try {
  83.         $outBuffer = $null
  84.         if ($PSBoundParameters.TryGetValue('OutBuffer', [ref]$outBuffer))
  85.         {
  86.             $PSBoundParameters['OutBuffer'] = 1
  87.         }
  88.         $wrappedCmd = $ExecutionContext.InvokeCommand.GetCommand('ConvertTo-Html',
  89.                         [System.Management.Automation.CommandTypes]::Cmdlet)
  90.                
  91.                 # define string variable to become the target command line
  92.                 #region Initialize helper variable to create command
  93.                 $scriptCmdPipeline = ''
  94.                 #endregion
  95.  
  96.                 # add new parameter handling
  97.                 #region Process and remove the Path parameter if it is present
  98.                 if ($Path) {
  99.                         $PSBoundParameters.Remove('Path') | Out-Null
  100.                         $scriptCmdPipeline += " | Out-File -FilePath $Path"
  101.                 }
  102.                 #endregion
  103.                
  104.                 $scriptCmd = {& $wrappedCmd @PSBoundParameters}
  105.                
  106.                 # redefine command invocation
  107.                 #region Append our pipeline command to the end of the wrapped command script block.
  108.                 $scriptCmd = $ExecutionContext.InvokeCommand.NewScriptBlock(
  109.                                 [string]$scriptCmd + $scriptCmdPipeline
  110.                         )
  111.                 #endregion
  112.                
  113.                
  114.         $steppablePipeline = $scriptCmd.GetSteppablePipeline($myInvocation.CommandOrigin)
  115.         $steppablePipeline.Begin($PSCmdlet)
  116.     } catch {
  117.         throw
  118.     }
  119. }
  120.  
  121. process
  122. {
  123.     try {
  124.         $steppablePipeline.Process($_)
  125.     } catch {
  126.         throw
  127.     }
  128. }
  129.  
  130. end
  131. {
  132.     try {
  133.         $steppablePipeline.End()
  134.     } catch {
  135.         throw
  136.     }
  137. }
  138. <#
  139.  
  140. .ForwardHelpTargetName ConvertTo-Html
  141. .ForwardHelpCategory Cmdlet
  142.  
  143. #>}

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me