From 978e8d38b5a33c48c6770eea86dc65ad51ab943c Mon Sep 17 00:00:00 2001 From: jhoneill Date: Thu, 2 May 2019 16:34:00 -0700 Subject: [PATCH 1/4] Added Example for PSScriptAnalyzer --- Examples/ScriptAnalyzer/Analyze_this.ps1 | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 Examples/ScriptAnalyzer/Analyze_this.ps1 diff --git a/Examples/ScriptAnalyzer/Analyze_this.ps1 b/Examples/ScriptAnalyzer/Analyze_this.ps1 new file mode 100644 index 0000000..f5ee3ad --- /dev/null +++ b/Examples/ScriptAnalyzer/Analyze_this.ps1 @@ -0,0 +1,24 @@ +$xlfilename = $pwd.path -replace "^.*\\(.*?)\\(.*?)$", '$1-$2.xlsx' +$xlpath = Join-Path -Path $env:TEMP -ChildPath $xlfilename +Remove-Item -Path $xlpath -ErrorAction SilentlyContinue + +$ScriptAnalyzerResults = Invoke-ScriptAnalyzer . + +$xlPkg = $ScriptAnalyzerResults | Group-Object -Property RuleName -NoElement | Sort-Object -Property Name | + Select-Object -Property Name,Count | + Export-Excel -Path $xlpath -WorksheetName Summary -AutoSize -PassThru + +$params = @{ + WorksheetName = 'FullResults' + AutoSize = $true + AutoFilter = $true + IncludePivotTable = $true + Activate = $true + PivotRows = 'Severity', 'RuleName' + PivotData = @{RuleName = 'Count' } + IncludePivotChart = $true + ChartType = 'BarClustered' + Show = $true +} + +Export-Excel -ExcelPackage $xlpkg -InputObject $ScriptAnalyzerResults @params \ No newline at end of file From f20a9de3dfd381e2f883fc7ac05c202f4a753796 Mon Sep 17 00:00:00 2001 From: jhoneill Date: Fri, 3 May 2019 12:45:30 -0700 Subject: [PATCH 2/4] New "Analyze-This" example --- Examples/ScriptAnalyzer/Analyze_this.ps1 | 66 +++++++++++++++++------- 1 file changed, 46 insertions(+), 20 deletions(-) diff --git a/Examples/ScriptAnalyzer/Analyze_this.ps1 b/Examples/ScriptAnalyzer/Analyze_this.ps1 index f5ee3ad..c88d67f 100644 --- a/Examples/ScriptAnalyzer/Analyze_this.ps1 +++ b/Examples/ScriptAnalyzer/Analyze_this.ps1 @@ -1,24 +1,50 @@ -$xlfilename = $pwd.path -replace "^.*\\(.*?)\\(.*?)$", '$1-$2.xlsx' -$xlpath = Join-Path -Path $env:TEMP -ChildPath $xlfilename -Remove-Item -Path $xlpath -ErrorAction SilentlyContinue +<# + .Synopsis -$ScriptAnalyzerResults = Invoke-ScriptAnalyzer . +#> +[CmdletBinding()] +param ( + [parameter(ValueFromPipeline = $true)] + $Path = $PWD, + $xlfile = "$env:TEMP\ScriptAnalyzer.xlsx", + $ChartType = 'BarClustered' , + [switch]$Quiet +) -$xlPkg = $ScriptAnalyzerResults | Group-Object -Property RuleName -NoElement | Sort-Object -Property Name | - Select-Object -Property Name,Count | - Export-Excel -Path $xlpath -WorksheetName Summary -AutoSize -PassThru - -$params = @{ - WorksheetName = 'FullResults' - AutoSize = $true - AutoFilter = $true - IncludePivotTable = $true - Activate = $true - PivotRows = 'Severity', 'RuleName' - PivotData = @{RuleName = 'Count' } - IncludePivotChart = $true - ChartType = 'BarClustered' - Show = $true +begin { + Remove-Item -Path $xlfile -ErrorAction SilentlyContinue + $xlparams = @{ + Path = $xlfile + WorksheetName = 'FullResults' + AutoSize = $true + AutoFilter = $true + Activate = $true + Show = (-not $Quiet) + } + $pivotParams = @{ + PivotTableName = 'BreakDown' + PivotData = @{RuleName = 'Count' } + PivotRows = 'Severity', 'RuleName' + PivotColumns = 'Location' + PivotTotals = 'Rows' + } + $dirsToProcess = @() +} +process { + if ($path.fullName) {$dirsToProcess += $path.fullName} + elseif ($path.path) {$dirsToProcess += $path.Path} + else {$dirsToProcess += $path} } -Export-Excel -ExcelPackage $xlpkg -InputObject $ScriptAnalyzerResults @params \ No newline at end of file +end { + $pivotParams['-PivotChartDefinition'] = New-ExcelChartDefinition -ChartType $chartType -Column $dirsToProcess.Count -Title "Script analysis" -LegendBold + $xlparams['PivotTableDefinition'] = New-PivotTableDefinition @pivotParams + + $dirsToProcess | ForEach-Object { + $dirName = (Resolve-Path -Path $_) -replace "^.*\\(.*?)\\(.*?)$", '$1-$2' + Write-Progress -Activity "Running Script Analyzer" -CurrentOperation $dirName + Invoke-ScriptAnalyzer -Path $_ -ErrorAction SilentlyContinue | + Add-Member -MemberType NoteProperty -Name Location -Value $dirName -PassThru + } | Export-Excel @xlparams + Write-Progress -Activity "Running Script Analyzer" -Completed +} From cfd3db5803bd23efacac177e84212387a8ab5fe7 Mon Sep 17 00:00:00 2001 From: jhoneill Date: Mon, 6 May 2019 09:43:38 +0100 Subject: [PATCH 3/4] Example : Analyze_this --- Examples/ScriptAnalyzer/Analyze_this.ps1 | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/Examples/ScriptAnalyzer/Analyze_this.ps1 b/Examples/ScriptAnalyzer/Analyze_this.ps1 index c88d67f..634ff71 100644 --- a/Examples/ScriptAnalyzer/Analyze_this.ps1 +++ b/Examples/ScriptAnalyzer/Analyze_this.ps1 @@ -1,13 +1,25 @@ <# .Synopsis + Runs PsScriptAnalyzer against one or more folders and pivots the results to form a report. + .Example + Analyze_this.ps1 + Invokes script analyzer on the current directory; creates a file in $env:temp and opens it in Excel + .Example + Analyze_this.ps1 -xlfile ..\mymodule.xlsx -quiet + Invokes script analyzer on the current directory; creates a file in the parent directory but does not open it + .Example + "." , (dir 'C:\Program Files\WindowsPowerShell\Modules\ImportExcel\') | .\examples\ScriptAnalyzer\Analyze_this.ps1 + run from a developemnt directory for importExcel it will produce a report for that directory compared against installed versions + this creates the file in the default location and opens it #> [CmdletBinding()] param ( [parameter(ValueFromPipeline = $true)] - $Path = $PWD, - $xlfile = "$env:TEMP\ScriptAnalyzer.xlsx", - $ChartType = 'BarClustered' , + $Path = $PWD, + $xlfile = "$env:TEMP\ScriptAnalyzer.xlsx", + $ChartType = 'BarClustered' , + $PivotColumns = 'Location', [switch]$Quiet ) From 6bfdea6d3ec72bcfa3cd60c18bb0104e3c15d055 Mon Sep 17 00:00:00 2001 From: jhoneill Date: Mon, 6 May 2019 09:43:38 +0100 Subject: [PATCH 4/4] Example : Analyze_this --- Examples/ScriptAnalyzer/Analyze_this.ps1 | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/Examples/ScriptAnalyzer/Analyze_this.ps1 b/Examples/ScriptAnalyzer/Analyze_this.ps1 index c88d67f..77aad5d 100644 --- a/Examples/ScriptAnalyzer/Analyze_this.ps1 +++ b/Examples/ScriptAnalyzer/Analyze_this.ps1 @@ -1,13 +1,25 @@ <# .Synopsis + Runs PsScriptAnalyzer against one or more folders and pivots the results to form a report. + .Example + Analyze_this.ps1 + Invokes script analyzer on the current directory; creates a file in $env:temp and opens it in Excel + .Example + Analyze_this.ps1 -xlfile ..\mymodule.xlsx -quiet + Invokes script analyzer on the current directory; creates a file in the parent directory but does not open it + .Example + "." , (dir 'C:\Program Files\WindowsPowerShell\Modules\ImportExcel\') | .\examples\ScriptAnalyzer\Analyze_this.ps1 + run from a developemnt directory for importExcel it will produce a report for that directory compared against installed versions + this creates the file in the default location and opens it #> [CmdletBinding()] param ( [parameter(ValueFromPipeline = $true)] - $Path = $PWD, - $xlfile = "$env:TEMP\ScriptAnalyzer.xlsx", - $ChartType = 'BarClustered' , + $Path = $PWD, + $xlfile = "$env:TEMP\ScriptAnalyzer.xlsx", + $ChartType = 'BarClustered' , + $PivotColumns = 'Location', [switch]$Quiet ) @@ -37,7 +49,7 @@ process { } end { - $pivotParams['-PivotChartDefinition'] = New-ExcelChartDefinition -ChartType $chartType -Column $dirsToProcess.Count -Title "Script analysis" -LegendBold + $pivotParams['-PivotChartDefinition'] = New-ExcelChartDefinition -ChartType $chartType -Column (1 + $dirsToProcess.Count) -Title "Script analysis" -LegendBold $xlparams['PivotTableDefinition'] = New-PivotTableDefinition @pivotParams $dirsToProcess | ForEach-Object {