PSLansweeper.psm1
function Get-LansweeperReport { [CmdletBinding()] param([string] $SqlInstance, [string] $Database = 'lansweeperdb') DynamicParam { $ParameterSetsAttributes = New-Object System.Management.Automation.ParameterAttribute $ParameterSetsAttributes.Mandatory = $true $ParameterSetsAttributes.ParameterSetName = 'Manual' $Names = (Get-ReportFiles -Path "$PSScriptRoot\Resources\Reports").Keys $ReportAttrib = New-Object System.Collections.ObjectModel.Collection[System.Attribute] $ReportAttrib.Add($ParameterSetsAttributes) $ReportAttrib.Add($ParamAttribDatesRange) $ReportAttrib.Add($ParameterSetsAttributesDateManual) $ReportAttrib.Add((New-Object System.Management.Automation.ValidateSetAttribute($Names))) $ReportRuntimeParam = New-Object System.Management.Automation.RuntimeDefinedParameter('Report', [string[]], $ReportAttrib) $RuntimeParamDic = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary $RuntimeParamDic.Add('Report', $ReportRuntimeParam) return $RuntimeParamDic } Process { [Array] $Reports = $PSBoundParameters.Report $AvailableReports = Get-ReportFiles -Path "$PSScriptRoot\Resources\Reports" $Output = [ordered] @{ } foreach ($Report in $Reports) { [System.Collections.IDictionary] $CurrentReport = $AvailableReports[$Report] try { $Output[$Report] = Invoke-DbaQuery -File $CurrentReport.FullPath -SqlInstance $SqlInstance -Database $Database -ErrorAction Stop | Select-Object -Property * -ExcludeProperty Table, ItemArray, HasErrors, RowError, RowState } catch { $ErrorMessage = $_.Exception.Message -replace "`n", " " -replace "`r", " " Write-Warning "Get-LansweeperReport - Error message: $ErrorMessage" } } if ($Reports.Count -eq 1) { $Output[$Reports[0]] } else { $Output } } } function Get-ReportFiles { [CmdletBinding()] param([string] $Path) $Files = Get-ChildItem -LiteralPath $Path -Filter '*.sql' $Reports = [ordered] @{ } foreach ($_ in $Files) { $Name = $_.Name.Replace(' ', '').Replace('.sql', '').Replace('-', '').Replace('–', '') $Reports[$Name] = [ordered] @{Name = $Name DisplayName = ($_.Name).Replace('.sql', '').Replace('-', '') FullPath = $_.FullName } } $Reports } Export-ModuleMember -Function @('Get-LansweeperReport') -Alias @() |