Framework/Managers/AzSDKPDFExtension.ps1
Set-StrictMode -Version Latest class AzSDKPDFExtension { static [void] GeneratePDF([string] $reportFolderPath, [PSObject] $subscriptionObject, [PSObject] $dataObject, [bool] $isLandscape) { # Get Context Info $executedBy = (Get-AzureRmContext).Account # Initialize word file try { $Word = New-Object -ComObject word.application $Word.Visible = $false; $AzSDKReportDoc = $Word.Documents.Add(); if($isLandscape) { $AzSDKReportDoc.PageSetup.Orientation = 1 } else { $AzSDKReportDoc.PageSetup.Orientation = 0 } $pdfPath = "$reportFolderPath\SecurityReport.pdf" $margin = 36 # 1.26 cm $AzSDKReportDoc.PageSetup.LeftMargin = $margin $AzSDKReportDoc.PageSetup.RightMargin = $margin #$AzSDKReportDoc.PageSetup.TopMargin = $margin $AzSDKReportDoc.PageSetup.BottomMargin = $margin $isSubscriptionCore = $false $selection = $Word.Selection # Region Front Page [AzSDKPDFExtension]::WriteText($selection, 'Secure DevOps Kit for Azure (AzSDK)','Title', $true) [AzSDKPDFExtension]::WriteText($selection, 'Security Report','TOC Heading', $true) $selection.InsertBreak(6) $selection.InsertBreak(6) $selection.InsertBreak(6) $selection.InsertBreak(6) $selection.InsertBreak(6) $TitleTableRange = $selection.Range(); $AzSDKReportDoc.Tables.Add($TitleTableRange,11,2) | Out-Null $AzSDKTitleTable = $AzSDKReportDoc.Tables.item(1) [AzSDKPDFExtension]::WriteHeaderTableCell($AzSDKTitleTable, 1, 'Subscription Name', $subscriptionObject.SubscriptionName) [AzSDKPDFExtension]::WriteHeaderTableCell($AzSDKTitleTable, 2, 'SubscriptionId', $subscriptionObject.SubscriptionId) [AzSDKPDFExtension]::WriteHeaderTableCell($AzSDKTitleTable, 3, 'AzSDK Version', $dataObject.MyCommand.Version.ToString()) [AzSDKPDFExtension]::WriteHeaderTableCell($AzSDKTitleTable, 4, 'Generated by', 'AzSDK') [AzSDKPDFExtension]::WriteHeaderTableCell($AzSDKTitleTable, 5, 'Generated on', (get-date).ToUniversalTime().ToString("MMMM dd, yyyy HH:mm") + " (UTC)") [AzSDKPDFExtension]::WriteHeaderTableCell($AzSDKTitleTable, 6, 'Requested by', $executedBy.Id.ToString() + " (" + $executedBy.Type.ToString() + ")") [AzSDKPDFExtension]::WriteHeaderTableCell($AzSDKTitleTable, 7, 'Command Executed', $dataObject.Line.Trim()) [AzSDKPDFExtension]::WriteHeaderTableCell($AzSDKTitleTable, 8, 'Documentation', 'http://aka.ms/azsdkdocs') [AzSDKPDFExtension]::WriteHeaderTableCell($AzSDKTitleTable, 9, 'FAQ', 'http://aka.ms/azsdkdocs/faq') [AzSDKPDFExtension]::WriteHeaderTableCell($AzSDKTitleTable, 10, 'Support DL', 'mailto:isrmazsdksup@microsoft.com') $AzSDKTitleTable.Borders.OutsideLineStyle = 1 $AzSDKTitleTable.Style = 'Table Grid Light' $AzSDKTitleTable.Borders.OutsideLineStyle = 1 $AzSDKTitleTable.Borders.InsideLineStyle = 0 $AzSDKTitleTable.Columns.AutoFit() $Word.Selection.Start= $AzSDKReportDoc.Content.End $selection.InsertBreak(7) #end region # Region TOC [AzSDKPDFExtension]::WriteText($selection, 'Contents','TOC Heading', $false) $range = $Selection.Range $toc = $AzSDKReportDoc.TablesOfContents.Add($range) $selection.TypeParagraph() $selection.InsertBreak(7) # End region TOC # Region Headers/Footers #$Section = $AzSDKReportDoc.Sections.Item(1) #$Header = $Section.Footers.Item(1) #$Header.Range.Text = (get-date).ToUniversalTime().ToString("HH:mm MMMM dd, yyyy") + "(UTC)" #$Header.Range.Font.Size = 9 #$Header.Range.ParagraphFormat.Alignment = 2 $AzSDKReportDoc.Sections(1).Footers(1).PageNumbers.Add(2) # End region Headers/Footers #region -> Add the CSV report $selection.TypeText("Security Report Summary"); $selection.Style = 'Heading 1' $selection.TypeParagraph() $selection.Style = 'Normal' $selection.InsertBreak(6) $ReportRange = $selection.Range(); $reportCSVFilePath = @(); $reportCSVFilePath += Get-ChildItem -Path $reportFolderPath -Filter "*.CSV" -Recurse if($reportCSVFilePath.Length -le 0) { throw "Didn't find the required security report under the report folder."; } $controls = Import-Csv -Path $reportCSVFilePath[0].FullName $Number_Of_Controls = (($controls | Measure-Object).Count +1) $Number_Of_Columns = 6 # ControlID, Status, RG, ResourceName, Control Severity $x = 2 $AzSDKReportDoc.Tables.Add($ReportRange,$Number_Of_Controls,$Number_Of_Columns) | Out-Null $AzSDKReportTable = $AzSDKReportDoc.Tables.item(2) $AzSDKReportTable.Cell(1,1).Range.Text = "ControlId" $AzSDKReportTable.Cell(1,2).Range.Text = "Status" $AzSDKReportTable.Cell(1,3).Range.Text = "ResourceGroup" $AzSDKReportTable.Cell(1,4).Range.Text = "Resource" $AzSDKReportTable.Cell(1,5).Range.Text = "Severity" $AzSDKReportTable.Cell(1,6).Range.Text = "Description" Foreach($control in $controls) { $AzSDKReportTable.Cell($x,1).Range.Text=$control.ControlId $AzSDKReportTable.Cell($x,2).Range.Text=$control.Status if($control | Get-Member -Name "ResourceGroupName") { $AzSDKReportTable.Cell($x,3).Range.Text=$control.ResourceGroupName if(($control | Get-Member -Name "ChildResourceName") -and (-Not [string]::IsNullOrEmpty($control.ChildResourceName))) { $AzSDKReportTable.Cell($x,4).Range.Text=$control.ResourceName + "/" + $control.ChildResourceName } else { $AzSDKReportTable.Cell($x,4).Range.Text=$control.ResourceName } } else { $isSubscriptionCore = $true $AzSDKReportTable.Cell($x,3).Range.Text="Subscription" $AzSDKReportTable.Cell($x,4).Range.Text="Subscription" } $AzSDKReportTable.Cell($x,5).Range.Text=$control.ControlSeverity $AzSDKReportTable.Cell($x,6).Range.Text=$control.Description $AzSDKReportTable.Cell($x,6).Range.Font.Size = 9 $x++ } $AzSDKReportTable.Style = 'Grid Table 4 - Accent 1' $selection = $Word.Selection $wdStory = 6 $wdMove = 0 $ret = $selection.EndKey($wdStory, $wdMove) $selection.TypeParagraph() $selection.InsertBreak(7) #end region #region -> Adding PowerShell output Get-ChildItem -Path $reportFolderPath -Directory | Where-Object {($_.Name -eq "etc")} | %{ $rootfolder = $_ [AzSDKPDFExtension]::WriteText($selection, 'PowerShell Output','Heading 1', $false) Get-ChildItem -Path $rootfolder.FullName -Recurse -Filter "PowerShellOutput.log" | %{ $logfilepath = $_ $log = Get-Content $logfilepath.FullName | Out-String [AzSDKPDFExtension]::WriteText($selection, $log,'No Spacing', $false) $selection.TypeText("#################################################################"); $selection.TypeParagraph() } } $selection.InsertBreak(7) #end region -> Adding PowerShell output #region -> Adding detailed logs [AzSDKPDFExtension]::WriteText($selection, 'Detailed Output','Heading 1', $false) $selection.InsertBreak(6) Get-ChildItem -Path $reportFolderPath -Directory | Where-Object {-not ($_.Name -eq "etc")} | %{ $rootfolder = $_ if($isSubscriptionCore) { [AzSDKPDFExtension]::WriteText($selection, 'Subscription Name: '+ ($rootfolder.Name),'Heading 2', $false) } else { [AzSDKPDFExtension]::WriteText($selection, 'Resource Group Name: ' + ($rootfolder.Name),'Heading 2', $false) } Get-ChildItem -Path $rootfolder.FullName -Recurse -Filter "*.log" | %{ $logfilepath = $_ [AzSDKPDFExtension]::WriteText($selection, 'Resource Type: ' + ($logfilepath.BaseName),'Heading 3', $false) $logs = Get-Content $logfilepath.FullName ForEach($log in $logs) { [AzSDKPDFExtension]::WriteText($selection, ($log | Out-String),'No Spacing', $false) } $selection.TypeParagraph() $selection.InsertBreak(7) } } #end region # Update table of content $toc.Update() } catch { throw $_.Exception } finally { $wdExportFormatPDF = 17 $wdDoNotSaveChanges = 0 $AzSDKReportDoc.ExportAsFixedFormat($pdfPath,$wdExportFormatPDF) $AzSDKReportDoc.close([ref]$wdDoNotSaveChanges) $Word.Quit() [System.Runtime.Interopservices.Marshal]::ReleaseComObject($AzSDKReportDoc) | Out-Null [System.Runtime.Interopservices.Marshal]::ReleaseComObject($word) | Out-Null [System.Runtime.Interopservices.Marshal]::ReleaseComObject($ReportRange) | Out-Null [System.Runtime.Interopservices.Marshal]::ReleaseComObject($AzSDKReportTable) | Out-Null Remove-Variable Doc,Word, range, table [gc]::collect() [gc]::WaitForPendingFinalizers() } } static [void] WriteText([PSObject] $selectionObj, [string] $textToWrite, [string] $style, [bool] $bold) { $selectionObj.TypeText($textToWrite); $selectionObj.Style = $style if($bold) { $selectionObj.Range.Font.Bold = 1 } else { $selectionObj.Range.Font.Bold = 0 } $selectionObj.TypeParagraph() $selectionObj.Style = 'Normal' } static [void] WriteHeaderTableCell([PSObject] $tableObj, [int] $row, [string] $title, [string] $value) { $tableObj.Cell($row,1).Range.Text = $title $tableObj.Cell($row,1).Range.Bold = 1 $tableObj.Cell($row,2).Range.Text = $value } } |