From e52b36a244ef1ad997592803df1ae1e34d79b15b Mon Sep 17 00:00:00 2001 From: Jonathan Colon Date: Thu, 14 Sep 2023 08:59:28 -0400 Subject: [PATCH 1/7] Fix error message --- Src/Private/Get-RequiredFeature.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Src/Private/Get-RequiredFeature.ps1 b/Src/Private/Get-RequiredFeature.ps1 index 6f74fd4..d38c409 100644 --- a/Src/Private/Get-RequiredFeature.ps1 +++ b/Src/Private/Get-RequiredFeature.ps1 @@ -91,7 +91,7 @@ function Get-RequiredFeature { } } else { - throw "Unable to validate if $Name is installed. https://github.com/AsBuiltReport/AsBuiltReport.Microsoft.AD" + throw "Unable to validate if $Name is installed. https://github.com/AsBuiltReport/AsBuiltReport.Microsoft.Windows" } } end {} From 786bd6adc9702c9ebd19acd2d064b0ba5f4b997b Mon Sep 17 00:00:00 2001 From: Jonathan Colon Date: Fri, 29 Sep 2023 14:07:43 -0400 Subject: [PATCH 2/7] Initial SQL Server Support --- AsBuiltReport.Microsoft.Windows.json | 7 +- AsBuiltReport.Microsoft.Windows.psd1 | 4 + README.md | 10 ++- Src/Private/Get-AbrWinSQLBuild.ps1 | 66 ++++++++++++++++ Src/Private/Get-AbrWinSQLDatabase.ps1 | 79 +++++++++++++++++++ Src/Private/SharedUtilsFunctions.ps1 | 3 +- ...Invoke-AsBuiltReport.Microsoft.Windows.ps1 | 27 ++++++- 7 files changed, 190 insertions(+), 6 deletions(-) create mode 100644 Src/Private/Get-AbrWinSQLBuild.ps1 create mode 100644 Src/Private/Get-AbrWinSQLDatabase.ps1 diff --git a/AsBuiltReport.Microsoft.Windows.json b/AsBuiltReport.Microsoft.Windows.json index 00463b7..f0a6b93 100644 --- a/AsBuiltReport.Microsoft.Windows.json +++ b/AsBuiltReport.Microsoft.Windows.json @@ -24,7 +24,8 @@ "SMB": 1, "DHCP": 1, "DNS": 1, - "FailOverCluster": 1 + "FailOverCluster": 1, + "SQLServer": 1 }, "HealthCheck": { @@ -49,7 +50,9 @@ "Nodes": true, "Network": true, "ClusterSharedVolume": true - + }, + "SQLServer": { + "Firewall": true } } } diff --git a/AsBuiltReport.Microsoft.Windows.psd1 b/AsBuiltReport.Microsoft.Windows.psd1 index 254b21c..e092f14 100644 --- a/AsBuiltReport.Microsoft.Windows.psd1 +++ b/AsBuiltReport.Microsoft.Windows.psd1 @@ -55,6 +55,10 @@ RequiredModules = @( @{ ModuleName = 'AsBuiltReport.Core'; ModuleVersion = '1.3.0' + }, + @{ + ModuleName = 'dbatools'; + ModuleVersion = '2.0.4' } ) diff --git a/README.md b/README.md index f7776ec..e34cce4 100644 --- a/README.md +++ b/README.md @@ -72,6 +72,8 @@ PowerShell 5.1 and the following PowerShell modules are required for generating - [DhcpServer Module](https://docs.microsoft.com/en-us/powershell/module/dhcpserver/?view=windowsserver2019-ps) - [DnsServer Module](https://docs.microsoft.com/en-us/powershell/module/dnsserver/?view=windowsserver2019-ps) - [FailoverClusters Module](https://learn.microsoft.com/en-us/powershell/module/failoverclusters/?view=windowsserver2022-ps) +- [DBATools Module](https://dbatools.io/) + ### Linux & macOS @@ -97,13 +99,15 @@ Install-WindowsFeature -Name RSAT-DHCP # Hyper-V Server powershell modules Install-WindowsFeature -Name Hyper-V-PowerShell -#IIS Server powershell modules +# IIS Server powershell modules Install-WindowsFeature -Name web-mgmt-console Install-WindowsFeature -Name Web-Scripting-Tools -#FailOver Cluster powershell modules +# FailOver Cluster powershell modules Install-WindowsFeature -Name RSAT-Clustering-PowerShell +# DBATools for SQL +Install-Module dbatools ``` ### PowerShell v5.x running on Windows client computer (Target) @@ -126,6 +130,8 @@ Enable-WindowsOptionalFeature -Online -FeatureName IIS-WebServerRole Enable-WindowsOptionalFeature -Online -FeatureName IIS-WebServerManagementTools Enable-WindowsOptionalFeature -Online -FeatureName IIS-ManagementScriptingTools +# DBATools for SQL +Install-Module dbatools ``` ### GitHub diff --git a/Src/Private/Get-AbrWinSQLBuild.ps1 b/Src/Private/Get-AbrWinSQLBuild.ps1 new file mode 100644 index 0000000..9aff25b --- /dev/null +++ b/Src/Private/Get-AbrWinSQLBuild.ps1 @@ -0,0 +1,66 @@ +function Get-AbrWinSQLBuild { + <# + .SYNOPSIS + Used by As Built Report to retrieve Windows SQL Server build information. + .DESCRIPTION + Documents the configuration of Microsoft Windows Server in Word/HTML/Text formats using PScribo. + .NOTES + Version: 0.5.2 + Author: Andrew Ramsay + Editor: Jonathan Colon + Twitter: @asbuiltreport + Github: AsBuiltReport + Credits: Iain Brighton (@iainbrighton) - PScribo module + + .LINK + https://github.com/AsBuiltReport/AsBuiltReport.Microsoft.Windows + #> + [CmdletBinding()] + param ( + ) + + begin { + Write-PScriboMessage "SQL Server InfoLevel set at $($InfoLevel.SQLServer)." + Write-PscriboMessage "Collecting SQL Server build information." + } + + process { + if ($InfoLevel.SQLServer -ge 1) { + try { + $Build = Get-DbaBuild -SqlInstance $SQLServer + if ($Build) { + Section -Style Heading3 'Build' { + Paragraph 'The following table details sql server build information' + Blankline + [array]$SQLServerObjt = @() + $TempSQLServerObjt = [PSCustomObject]@{ + 'Instance Name' = $Build.SqlInstance + 'Build' = $Build.Build + 'Level' = $Build.NameLevel + 'Service Pack' = $Build.SPLevel + 'Comulative Update' = ConvertTo-EmptyToFiller $Build.CULevel + 'KB Level' = $Build.KBLevel + 'Supported Until' = $Build.SupportedUntil.ToShortDateString() + 'Warning' = ConvertTo-EmptyToFiller $Build.Warning + } + $SQLServerObjt += $TempSQLServerObjt + + $TableParams = @{ + Name = "SQL Server Build" + List = $True + ColumnWidths = 40, 60 + } + if ($Report.ShowTableCaptions) { + $TableParams['Caption'] = "- $($TableParams.Name)" + } + $SQLServerObjt | Table @TableParams + } + } + } + catch { + Write-PscriboMessage -IsWarning $_.Exception.Message + } + } + } + end {} +} \ No newline at end of file diff --git a/Src/Private/Get-AbrWinSQLDatabase.ps1 b/Src/Private/Get-AbrWinSQLDatabase.ps1 new file mode 100644 index 0000000..1548843 --- /dev/null +++ b/Src/Private/Get-AbrWinSQLDatabase.ps1 @@ -0,0 +1,79 @@ +function Get-AbrWinSQLDatabase { + <# + .SYNOPSIS + Used by As Built Report to retrieve Windows SQL Server build information. + .DESCRIPTION + Documents the configuration of Microsoft Windows Server in Word/HTML/Text formats using PScribo. + .NOTES + Version: 0.5.2 + Author: Andrew Ramsay + Editor: Jonathan Colon + Twitter: @asbuiltreport + Github: AsBuiltReport + Credits: Iain Brighton (@iainbrighton) - PScribo module + + .LINK + https://github.com/AsBuiltReport/AsBuiltReport.Microsoft.Windows + #> + [CmdletBinding()] + param ( + ) + + begin { + Write-PScriboMessage "SQL Server Dstabases InfoLevel set at $($InfoLevel.SQLServer)." + } + + process { + $SQLDBs = Get-AzBastion | Sort-Object Name + if ($SQLDBs) { + Write-PscriboMessage "Collecting SQL Server databases information." + Section -Style Heading4 'Databases' { + $SQLDBInfo = @() + foreach ($SQLDB in $SQLDBs) { + $InObj = [Ordered]@{ + 'Name' = $AzBastion.Name + 'Resource Group' = $SQLDB.ResourceGroupName + 'Location' = $SQLDB."$($AzBastion.Location)" + 'Subscription' = "$($AzSubscriptionLookup.(($SQLDB.Id).split('/')[2]))" + 'Virtual Network / Subnet' = $SQLDB.IpConfigurations.subnet.id.split('/')[-1] + 'Public DNS Name' = $SQLDB.DnsName + 'Public IP Address' = $SQLDB.IpConfigurations.publicipaddress.id.split('/')[-1] + } + $SQLDBInfo += [PSCustomObject]$InObj + } + + if ($InfoLevel.SQLServer -ge 2) { + Paragraph "The following sections detail the configuration of the databases within the sql server." + foreach ($SQLDB in $SQLDBInfo) { + Section -Style NOTOCHeading5 -ExcludeFromTOC "$($SQLDB.Name)" { + $TableParams = @{ + Name = "Database - $($SQLDB.Name)" + List = $true + ColumnWidths = 50, 50 + } + if ($Report.ShowTableCaptions) { + $TableParams['Caption'] = "- $($TableParams.Name)" + } + $AzBastion | Table @TableParams + } + } + } else { + Paragraph "The following table summarises the configuration of the databases within $($SQLDB.Name)." + BlankLine + $TableParams = @{ + Name = "Bastions - $($AzSubscription.Name)" + List = $false + Columns = 'Name', 'Resource Group', 'Location', 'Public IP Address' + ColumnWidths = 25, 25, 25, 25 + } + if ($Report.ShowTableCaptions) { + $TableParams['Caption'] = "- $($TableParams.Name)" + } + $AzBastionInfo | Table @TableParams + } + } + } + } + + end {} +} \ No newline at end of file diff --git a/Src/Private/SharedUtilsFunctions.ps1 b/Src/Private/SharedUtilsFunctions.ps1 index 618a91a..2a67f53 100644 --- a/Src/Private/SharedUtilsFunctions.ps1 +++ b/Src/Private/SharedUtilsFunctions.ps1 @@ -128,7 +128,8 @@ function ConvertTo-EmptyToFiller { Position = 0, Mandatory)] [AllowEmptyString()] - [string]$TEXT + [AllowNull()] + $TEXT ) switch ([string]::IsNullOrEmpty($TEXT)) { diff --git a/Src/Public/Invoke-AsBuiltReport.Microsoft.Windows.ps1 b/Src/Public/Invoke-AsBuiltReport.Microsoft.Windows.ps1 index 5ea1d4f..6b5dd8d 100644 --- a/Src/Public/Invoke-AsBuiltReport.Microsoft.Windows.ps1 +++ b/Src/Public/Invoke-AsBuiltReport.Microsoft.Windows.ps1 @@ -316,7 +316,7 @@ function Invoke-AsBuiltReport.Microsoft.Windows { if ($Status.Status -eq "Running") { try { $script:Cluster = Invoke-Command -Session $TempPssSession -ScriptBlock { (Get-Cluster).Name } - if ((Get-RequiredFeature -Name RSAT-Clustering-PowerShell -OSType $OSType.Value -Status )-and $Cluster) { + if ((Get-RequiredFeature -Name RSAT-Clustering-PowerShell -OSType $OSType.Value -Status) -and $Cluster) { Section -Style Heading2 "Failover Cluster Configuration" { Paragraph 'The following table details the Failover Cluster Settings' Blankline @@ -353,6 +353,31 @@ function Invoke-AsBuiltReport.Microsoft.Windows { } } + if ($InfoLevel.SQLServer -ge 1 -and $OSType.Value -ne 'WorkStation') { + $Status = Invoke-Command -Session $TempPssSession -ScriptBlock { Get-Service 'MSSQLSERVER' -ErrorAction SilentlyContinue } + if ($Status.Status -eq "Running") { + try { + $script:SQLServer = Connect-DbaInstance -SqlInstance $System -TrustServerCertificate -SqlCredential $Credential + if ($SQLServer) { + Section -Style Heading2 "SQL Server Configuration" { + Paragraph 'The following table details the SQL Server Settings' + Blankline + # Failover Cluster Server Configuration + Get-AbrWinSQLBuild + } + } + else { + Write-PScriboMessage -IsWarning "Unable to connect to SQL Instance $($Options.Instance)" + } + } + catch { + Write-PscriboMessage -IsWarning $_.Exception.Message + } + } else { + Write-PScriboMessage "No SQL Server service detected. Disabling SQL Server section" + } + } + } Remove-PSSession $TempPssSession Remove-CimSession $TempCimSession From 59a8e45190d3eed1829e14acbf229e1fe1e990b5 Mon Sep 17 00:00:00 2001 From: Jonathan Colon Date: Sat, 17 Feb 2024 20:04:24 -0400 Subject: [PATCH 3/7] Fix issue with coma char --- AsBuiltReport.Microsoft.Windows.json | 2 +- .../Invoke-AsBuiltReport.Microsoft.Windows.ps1 | 12 +++++------- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/AsBuiltReport.Microsoft.Windows.json b/AsBuiltReport.Microsoft.Windows.json index 6cc7837..6324634 100644 --- a/AsBuiltReport.Microsoft.Windows.json +++ b/AsBuiltReport.Microsoft.Windows.json @@ -22,7 +22,7 @@ "SMB": 1, "DHCP": 1, "DNS": 1, - "FailOverCluster": 1 + "FailOverCluster": 1, "SQLServer": 1 }, "HealthCheck": { diff --git a/Src/Public/Invoke-AsBuiltReport.Microsoft.Windows.ps1 b/Src/Public/Invoke-AsBuiltReport.Microsoft.Windows.ps1 index 375c4ec..e12b24e 100644 --- a/Src/Public/Invoke-AsBuiltReport.Microsoft.Windows.ps1 +++ b/Src/Public/Invoke-AsBuiltReport.Microsoft.Windows.ps1 @@ -235,7 +235,7 @@ function Invoke-AsBuiltReport.Microsoft.Windows { if ($SMBShares) { Section -Style Heading2 "File Server Configuration" { Paragraph 'The following table details the File Server settings' - BlankLine + BlankLine # SMB Server Configuration Get-AbrWinSMBSummary # SMB Server Network Interface @@ -349,17 +349,15 @@ function Invoke-AsBuiltReport.Microsoft.Windows { if ($SQLServer) { Section -Style Heading2 "SQL Server Configuration" { Paragraph 'The following table details the SQL Server Settings' - Blankline + BlankLine # Failover Cluster Server Configuration Get-AbrWinSQLBuild } - } - else { + } else { Write-PScriboMessage -IsWarning "Unable to connect to SQL Instance $($Options.Instance)" } - } - catch { - Write-PscriboMessage -IsWarning $_.Exception.Message + } catch { + Write-PScriboMessage -IsWarning $_.Exception.Message } } else { Write-PScriboMessage "No SQL Server service detected. Disabling SQL Server section" From d853f7d132e7c5fd8a944393d360783032a5e252 Mon Sep 17 00:00:00 2001 From: Jonathan Colon Date: Sun, 18 Feb 2024 00:20:30 -0400 Subject: [PATCH 4/7] Initial SQL Server Support --- .github/workflows/Release.yml | 4 + AsBuiltReport.Microsoft.Windows.psd1 | 4 +- CHANGELOG.md | 6 + Src/Private/Get-AbrWinSQLDatabase.ps1 | 193 ++++++++++++++---- ...Invoke-AsBuiltReport.Microsoft.Windows.ps1 | 1 + 5 files changed, 162 insertions(+), 46 deletions(-) diff --git a/.github/workflows/Release.yml b/.github/workflows/Release.yml index fa7a814..7cfe12e 100644 --- a/.github/workflows/Release.yml +++ b/.github/workflows/Release.yml @@ -13,6 +13,10 @@ jobs: shell: pwsh run: | Set-PSRepository -Name PSGallery -InstallationPolicy Trusted + - name: Install DBAtools module + shell: pwsh + run: | + Install-Module -Name dbatools -Repository PSGallery -Force - name: Install AsBuiltReport.Core module shell: pwsh run: | diff --git a/AsBuiltReport.Microsoft.Windows.psd1 b/AsBuiltReport.Microsoft.Windows.psd1 index 6676149..0ef3fb2 100644 --- a/AsBuiltReport.Microsoft.Windows.psd1 +++ b/AsBuiltReport.Microsoft.Windows.psd1 @@ -12,7 +12,7 @@ RootModule = 'AsBuiltReport.Microsoft.Windows.psm1' # Version number of this module. - ModuleVersion = '0.5.2' + ModuleVersion = '0.5.3' # Supported PSEditions # CompatiblePSEditions = @() @@ -58,7 +58,7 @@ }, @{ ModuleName = 'dbatools'; - ModuleVersion = '2.0.4' + ModuleVersion = '2.1.7' } ) diff --git a/CHANGELOG.md b/CHANGELOG.md index c79c564..5898047 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [0.5.3] - 2024-02-?? + +### Added + +- Add Initial SQL Server support + ## [0.5.2] - 2024-02-18 ### Added diff --git a/Src/Private/Get-AbrWinSQLDatabase.ps1 b/Src/Private/Get-AbrWinSQLDatabase.ps1 index 1548843..c9b8167 100644 --- a/Src/Private/Get-AbrWinSQLDatabase.ps1 +++ b/Src/Private/Get-AbrWinSQLDatabase.ps1 @@ -5,10 +5,9 @@ function Get-AbrWinSQLDatabase { .DESCRIPTION Documents the configuration of Microsoft Windows Server in Word/HTML/Text formats using PScribo. .NOTES - Version: 0.5.2 - Author: Andrew Ramsay - Editor: Jonathan Colon - Twitter: @asbuiltreport + Version: 0.5.3 + Author: Jonathan Colon + Twitter: @rebelinux Github: AsBuiltReport Credits: Iain Brighton (@iainbrighton) - PScribo module @@ -24,56 +23,162 @@ function Get-AbrWinSQLDatabase { } process { - $SQLDBs = Get-AzBastion | Sort-Object Name - if ($SQLDBs) { - Write-PscriboMessage "Collecting SQL Server databases information." - Section -Style Heading4 'Databases' { - $SQLDBInfo = @() - foreach ($SQLDB in $SQLDBs) { - $InObj = [Ordered]@{ - 'Name' = $AzBastion.Name - 'Resource Group' = $SQLDB.ResourceGroupName - 'Location' = $SQLDB."$($AzBastion.Location)" - 'Subscription' = "$($AzSubscriptionLookup.(($SQLDB.Id).split('/')[2]))" - 'Virtual Network / Subnet' = $SQLDB.IpConfigurations.subnet.id.split('/')[-1] - 'Public DNS Name' = $SQLDB.DnsName - 'Public IP Address' = $SQLDB.IpConfigurations.publicipaddress.id.split('/')[-1] - } - $SQLDBInfo += [PSCustomObject]$InObj - } + Section -Style Heading3 'Databases' { + $CompatibilityHash = @{ + 'Version60' = 'SQL Server 6.0' + 'Version65' = 'SQL Server 6.5' + 'Version70' = 'SQL Server 7.0' + 'Version80' = 'SQL Server 2000' + 'Version90' = 'SQL Server 2005' + 'Version100' = 'SQL Server 2008' + 'Version110' = 'SQL Server 2012' + 'Version120' = 'SQL Server 2014' + 'Version130' = 'SQL Server 2016' + 'Version140' = 'SQL Server 2017' + 'Version150' = 'SQL Server 2019' + 'Version160' = 'SQL Server 2022' - if ($InfoLevel.SQLServer -ge 2) { - Paragraph "The following sections detail the configuration of the databases within the sql server." - foreach ($SQLDB in $SQLDBInfo) { - Section -Style NOTOCHeading5 -ExcludeFromTOC "$($SQLDB.Name)" { - $TableParams = @{ - Name = "Database - $($SQLDB.Name)" - List = $true - ColumnWidths = 50, 50 + } + Write-PScriboMessage "Collecting SQL Server databases information." + $SQLDBs = Get-DbaDatabase -SqlInstance $SQLServer -ExcludeUser | Sort-Object -Property Name + if ($SQLDBs) { + Write-PScriboMessage "Collecting SQL Server system databases information." + Section -Style Heading4 'System Databases' { + $SQLDBInfo = @() + foreach ($SQLDB in $SQLDBs) { + try { + $InObj = [Ordered]@{ + 'Name' = $SQLDB.Name + 'Status' = $SQLDB.Status + 'Is Accessible?' = ConvertTo-TextYN $SQLDB.IsAccessible + 'Recovery Model' = $SQLDB.RecoveryModel + 'Size' = Switch ([string]::IsNullOrEmpty($SQLDB.SizeM)) { + $true { '--' } + $false { "$($SQLDB.SizeMB) MB" } + default { 'Unknown' } + } + 'Compatibility' = $CompatibilityHash[[string]$SQLDB.Compatibility] + 'Collation' = $SQLDB.Collation + 'Encrypted' = ConvertTo-TextYN $SQLDB.Encrypted + 'Last Full Backup' = Switch ($SQLDB.LastFullBackup) { + '01/01/0001 00:00:00' { "Never" } + $null { '--' } + default { $SQLDB.LastFullBackup } + } + 'Last Log Backup' = Switch ($SQLDB.LastLogBackup) { + '01/01/0001 00:00:00' { "Never" } + $null { '--' } + default { $SQLDB.LastLogBackup } + } + 'Owner' = $SQLDB.Owner } - if ($Report.ShowTableCaptions) { - $TableParams['Caption'] = "- $($TableParams.Name)" + $SQLDBInfo += [PSCustomObject]$InObj + } catch { + Write-PScriboMessage -IsWarning "SQL Server System Database Section: $($_.Exception.Message)" + } + } + + if ($InfoLevel.SQLServer -ge 2) { + Paragraph "The following sections detail the configuration of the system databases within the sql server." + foreach ($SQLDB in $SQLDBInfo) { + Section -Style NOTOCHeading5 -ExcludeFromTOC "$($SQLDB.Name)" { + $TableParams = @{ + Name = "System Database - $($SQLDB.Name)" + List = $true + ColumnWidths = 50, 50 + } + if ($Report.ShowTableCaptions) { + $TableParams['Caption'] = "- $($TableParams.Name)" + } + $SQLDB | Table @TableParams } - $AzBastion | Table @TableParams } + } else { + Paragraph "The following table summarises the configuration of the system databases within $($SQLDB.Name)." + BlankLine + $TableParams = @{ + Name = "System Databases" + List = $false + Columns = 'Name', 'Owner', 'Status', 'Recovery Model', 'Size' + ColumnWidths = 32, 32, 12, 12, 12 + } + if ($Report.ShowTableCaptions) { + $TableParams['Caption'] = "- $($TableParams.Name)" + } + $SQLDBInfo | Table @TableParams } - } else { - Paragraph "The following table summarises the configuration of the databases within $($SQLDB.Name)." - BlankLine - $TableParams = @{ - Name = "Bastions - $($AzSubscription.Name)" - List = $false - Columns = 'Name', 'Resource Group', 'Location', 'Public IP Address' - ColumnWidths = 25, 25, 25, 25 + } + } + $SQLDBs = Get-DbaDatabase -SqlInstance $SQLServer -ExcludeSystem | Sort-Object -Property Name + if ($SQLDBs) { + Write-PScriboMessage "Collecting SQL Server user databases information." + Section -Style Heading4 'User Databases' { + $SQLDBInfo = @() + foreach ($SQLDB in $SQLDBs) { + try { + $InObj = [Ordered]@{ + 'Name' = $SQLDB.Name + 'Status' = $SQLDB.Status + 'Is Accessible?' = ConvertTo-TextYN $SQLDB.IsAccessible + 'Recovery Model' = $SQLDB.RecoveryModel + 'Size' = Switch ([string]::IsNullOrEmpty($SQLDB.SizeM)) { + $true { '--' } + $false { "$($SQLDB.SizeMB) MB" } + default { 'Unknown' } + } + 'Compatibility' = $CompatibilityHash[[string]$SQLDB.Compatibility] + 'Collation' = $SQLDB.Collation + 'Encrypted' = ConvertTo-TextYN $SQLDB.Encrypted + 'Last Full Backup' = Switch ($SQLDB.LastFullBackup) { + '01/01/0001 00:00:00' { "Never" } + $null { '--' } + default { $SQLDB.LastFullBackup } + } + 'Last Log Backup' = Switch ($SQLDB.LastLogBackup) { + '01/01/0001 00:00:00' { "Never" } + $null { '--' } + default { $SQLDB.LastLogBackup } + } + 'Owner' = $SQLDB.Owner + } + $SQLDBInfo += [PSCustomObject]$InObj + } catch { + Write-PScriboMessage -IsWarning "SQL Server User Database Section: $($_.Exception.Message)" + } } - if ($Report.ShowTableCaptions) { - $TableParams['Caption'] = "- $($TableParams.Name)" + + if ($InfoLevel.SQLServer -ge 2) { + Paragraph "The following sections detail the configuration of the user databases within the sql server." + foreach ($SQLDB in $SQLDBInfo) { + Section -Style NOTOCHeading5 -ExcludeFromTOC "$($SQLDB.Name)" { + $TableParams = @{ + Name = "User Database - $($SQLDB.Name)" + List = $true + ColumnWidths = 50, 50 + } + if ($Report.ShowTableCaptions) { + $TableParams['Caption'] = "- $($TableParams.Name)" + } + $SQLDB | Table @TableParams + } + } + } else { + Paragraph "The following table summarises the configuration of the databases within $($SQLDB.Name)." + BlankLine + $TableParams = @{ + Name = "User Databases" + List = $false + Columns = 'Name', 'Owner', 'Status', 'Recovery Model', 'Size' + ColumnWidths = 32, 32, 12, 12, 12 + } + if ($Report.ShowTableCaptions) { + $TableParams['Caption'] = "- $($TableParams.Name)" + } + $SQLDBInfo | Table @TableParams } - $AzBastionInfo | Table @TableParams } } } } - end {} } \ No newline at end of file diff --git a/Src/Public/Invoke-AsBuiltReport.Microsoft.Windows.ps1 b/Src/Public/Invoke-AsBuiltReport.Microsoft.Windows.ps1 index e12b24e..152c61a 100644 --- a/Src/Public/Invoke-AsBuiltReport.Microsoft.Windows.ps1 +++ b/Src/Public/Invoke-AsBuiltReport.Microsoft.Windows.ps1 @@ -352,6 +352,7 @@ function Invoke-AsBuiltReport.Microsoft.Windows { BlankLine # Failover Cluster Server Configuration Get-AbrWinSQLBuild + Get-AbrWinSQLDatabase } } else { Write-PScriboMessage -IsWarning "Unable to connect to SQL Instance $($Options.Instance)" From 1ad3163e2702c438090ba11be4631c4f069ec343 Mon Sep 17 00:00:00 2001 From: Jonathan Colon Date: Sun, 18 Feb 2024 13:34:32 -0400 Subject: [PATCH 5/7] Added More SQL Server Sections --- AsBuiltReport.Microsoft.Windows.json | 4 +- CHANGELOG.md | 11 +- README.md | 4 + Src/Private/Get-AbrWinSQLBackupDevice.ps1 | 83 ++++++ Src/Private/Get-AbrWinSQLBuild.ps1 | 46 +++- Src/Private/Get-AbrWinSQLDatabase.ps1 | 248 +++++++++--------- Src/Private/Get-AbrWinSQLLogin.ps1 | 107 ++++++++ Src/Private/Get-AbrWinSQLRole.ps1 | 88 +++++++ ...Invoke-AsBuiltReport.Microsoft.Windows.ps1 | 20 +- 9 files changed, 473 insertions(+), 138 deletions(-) create mode 100644 Src/Private/Get-AbrWinSQLBackupDevice.ps1 create mode 100644 Src/Private/Get-AbrWinSQLLogin.ps1 create mode 100644 Src/Private/Get-AbrWinSQLRole.ps1 diff --git a/AsBuiltReport.Microsoft.Windows.json b/AsBuiltReport.Microsoft.Windows.json index 6324634..e9e6ec6 100644 --- a/AsBuiltReport.Microsoft.Windows.json +++ b/AsBuiltReport.Microsoft.Windows.json @@ -8,7 +8,9 @@ "ShowHeaderFooter": true, "ShowTableCaptions": true }, - "Options": {}, + "Options": { + "Instance": "MSSQLSERVER" + }, "InfoLevel": { "_comment_": "Please refer to the AsBuiltReport project contributing guide for information about how to define InfoLevels.", "_comment_": "0 = Disabled, 1 = Enabled, 2 = Adv Summary, 3 = Detailed, 4 = Adv Detailed, 5 = Comprehensive", diff --git a/CHANGELOG.md b/CHANGELOG.md index 5898047..96b864b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,7 +9,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added -- Add Initial SQL Server support +- Add SQL Server support + - General Information + - Security + - Login + - Roles + - Databases + - User + - System + - Server Objects + - Backup Devices ## [0.5.2] - 2024-02-18 diff --git a/README.md b/README.md index e053da5..3693996 100644 --- a/README.md +++ b/README.md @@ -185,6 +185,10 @@ The **Report** schema provides configuration of the Microsoft Windows report inf The **Options** schema allows certain options within the report to be toggled on or off. +| Sub-Schema | Setting | Default | Description | +| ---------- | ------------------- | ----------- | ------------------------------------------ | +| Instance | SQL Server Instance | MSSQLSERVER | Set the sql server instance to connect to. | + ### InfoLevel The **InfoLevel** schema allows configuration of each section of the report at a granular level. The following sections can be set. diff --git a/Src/Private/Get-AbrWinSQLBackupDevice.ps1 b/Src/Private/Get-AbrWinSQLBackupDevice.ps1 new file mode 100644 index 0000000..582cdb8 --- /dev/null +++ b/Src/Private/Get-AbrWinSQLBackupDevice.ps1 @@ -0,0 +1,83 @@ +function Get-AbrWinSQLBackupDevice { + <# + .SYNOPSIS + Used by As Built Report to retrieve Windows SQL Server backup device information. + .DESCRIPTION + Documents the configuration of Microsoft Windows Server in Word/HTML/Text formats using PScribo. + .NOTES + Version: 0.5.3 + Author: Jonathan Colon + Twitter: @rebelinux + Github: AsBuiltReport + Credits: Iain Brighton (@iainbrighton) - PScribo module + + .LINK + https://github.com/AsBuiltReport/AsBuiltReport.Microsoft.Windows + #> + [CmdletBinding()] + param ( + ) + + begin { + Write-PScriboMessage "SQL Server Backup Device InfoLevel set at $($InfoLevel.SQLServer)." + } + + process { + try { + Write-PScriboMessage "Collecting SQL Server Backup Device information." + $SQLBackUpDevices = Get-DbaBackupDevice -SqlInstance $SQLServer | Sort-Object -Property Name + if ($SQLBackUpDevices) { + Write-PScriboMessage "Collecting SQL Server Backup Device information." + Section -Style Heading4 'Backup Device' { + $ItemInfo = @() + foreach ($Item in $SQLBackUpDevices) { + try { + $InObj = [Ordered]@{ + 'Name' = $Item.Name + 'Backup Device Type' = $Item.BackupDeviceType + 'Physical Location' = $Item.PhysicalLocation + 'Skip Tape Label' = ConvertTo-TextYN $Item.SkipTapeLabel + } + $ItemInfo += [PSCustomObject]$InObj + } catch { + Write-PScriboMessage -IsWarning "SQL Server System Backup Device Section: $($_.Exception.Message)" + } + } + + if ($InfoLevel.SQLServer -ge 2) { + Paragraph "The following sections detail the configuration of the backup device $($SQLServer.Name)." + foreach ($Item in $ItemInfo) { + Section -Style NOTOCHeading5 -ExcludeFromTOC "$($Item.Name)" { + $TableParams = @{ + Name = "Backup Device - $($Item.Name)" + List = $true + ColumnWidths = 50, 50 + } + if ($Report.ShowTableCaptions) { + $TableParams['Caption'] = "- $($TableParams.Name)" + } + $Item | Table @TableParams + } + } + } else { + Paragraph "The following table summarises the configuration of the backup device within $($SQLServer.Name)." + BlankLine + $TableParams = @{ + Name = "Backup Devices" + List = $false + Columns = 'Name', 'Backup Device Type', 'Physical Location' + ColumnWidths = 25, 25, 50 + } + if ($Report.ShowTableCaptions) { + $TableParams['Caption'] = "- $($TableParams.Name)" + } + $ItemInfo | Table @TableParams + } + } + } + } catch { + Write-PScriboMessage -IsWarning "SQL Server Backup Device Section: $($_.Exception.Message)" + } + } + end {} +} \ No newline at end of file diff --git a/Src/Private/Get-AbrWinSQLBuild.ps1 b/Src/Private/Get-AbrWinSQLBuild.ps1 index 9aff25b..93f105b 100644 --- a/Src/Private/Get-AbrWinSQLBuild.ps1 +++ b/Src/Private/Get-AbrWinSQLBuild.ps1 @@ -1,7 +1,7 @@ function Get-AbrWinSQLBuild { <# .SYNOPSIS - Used by As Built Report to retrieve Windows SQL Server build information. + Used by As Built Report to retrieve Windows SQL Server Properties information. .DESCRIPTION Documents the configuration of Microsoft Windows Server in Word/HTML/Text formats using PScribo. .NOTES @@ -21,32 +21,51 @@ function Get-AbrWinSQLBuild { begin { Write-PScriboMessage "SQL Server InfoLevel set at $($InfoLevel.SQLServer)." - Write-PscriboMessage "Collecting SQL Server build information." + Write-PScriboMessage "Collecting SQL Server Properties information." } process { if ($InfoLevel.SQLServer -ge 1) { try { + $Properties = Get-DbaInstanceProperty -SqlInstance $SQLServer | ForEach-Object { @{$_.Name = $_.Value } } $Build = Get-DbaBuild -SqlInstance $SQLServer - if ($Build) { - Section -Style Heading3 'Build' { - Paragraph 'The following table details sql server build information' - Blankline + if ($Properties) { + Section -Style Heading3 'General Information' { + Paragraph 'The following table details sql server Properties information' + BlankLine [array]$SQLServerObjt = @() $TempSQLServerObjt = [PSCustomObject]@{ 'Instance Name' = $Build.SqlInstance - 'Build' = $Build.Build - 'Level' = $Build.NameLevel - 'Service Pack' = $Build.SPLevel + 'Fully Qualified Net Name' = $Properties.FullyQualifiedNetName + 'Supported Until' = $Build.SupportedUntil.ToShortDateString() + 'Edition' = $Properties.Edition + 'Level' = "Microsoft SQL Server $($Build.NameLevel)" + 'Build' = $Properties.VersionString + 'Service Pack' = $Properties.ProductLevel 'Comulative Update' = ConvertTo-EmptyToFiller $Build.CULevel 'KB Level' = $Build.KBLevel - 'Supported Until' = $Build.SupportedUntil.ToShortDateString() + 'Case Sensitive' = ConvertTo-TextYN $Properties.IsCaseSensitive + 'Full Text Installed' = ConvertTo-TextYN $Properties.IsFullTextInstalled + 'XTP Supported' = ConvertTo-TextYN $Properties.IsXTPSupported + 'Clustered' = ConvertTo-TextYN $Properties.IsClustered + 'Single User' = ConvertTo-TextYN $Properties.IsSingleUser + 'Language' = $Properties.Language + 'Collation' = $Properties.Collation + 'Sql CharSet Name' = $Properties.SqlCharSetName + 'Root Directory' = $Properties.RootDirectory + 'Master DB Path' = $Properties.MasterDBPath + 'Master DB Log Path' = $Properties.MasterDBLogPath + 'Backup Directory' = $Properties.BackupDirectory + 'Default File' = $Properties.DefaultFile + 'Default Log' = $Properties.DefaultLog + 'Login Mode' = $Properties.LoginMode + 'Mail Profile' = ConvertTo-EmptyToFiller $Properties.MailProfile 'Warning' = ConvertTo-EmptyToFiller $Build.Warning } $SQLServerObjt += $TempSQLServerObjt $TableParams = @{ - Name = "SQL Server Build" + Name = "General Information" List = $True ColumnWidths = 40, 60 } @@ -56,9 +75,8 @@ function Get-AbrWinSQLBuild { $SQLServerObjt | Table @TableParams } } - } - catch { - Write-PscriboMessage -IsWarning $_.Exception.Message + } catch { + Write-PScriboMessage -IsWarning $_.Exception.Message } } } diff --git a/Src/Private/Get-AbrWinSQLDatabase.ps1 b/Src/Private/Get-AbrWinSQLDatabase.ps1 index c9b8167..7617d36 100644 --- a/Src/Private/Get-AbrWinSQLDatabase.ps1 +++ b/Src/Private/Get-AbrWinSQLDatabase.ps1 @@ -1,7 +1,7 @@ function Get-AbrWinSQLDatabase { <# .SYNOPSIS - Used by As Built Report to retrieve Windows SQL Server build information. + Used by As Built Report to retrieve Windows SQL Server database information. .DESCRIPTION Documents the configuration of Microsoft Windows Server in Word/HTML/Text formats using PScribo. .NOTES @@ -39,144 +39,152 @@ function Get-AbrWinSQLDatabase { 'Version160' = 'SQL Server 2022' } - Write-PScriboMessage "Collecting SQL Server databases information." - $SQLDBs = Get-DbaDatabase -SqlInstance $SQLServer -ExcludeUser | Sort-Object -Property Name - if ($SQLDBs) { - Write-PScriboMessage "Collecting SQL Server system databases information." - Section -Style Heading4 'System Databases' { - $SQLDBInfo = @() - foreach ($SQLDB in $SQLDBs) { - try { - $InObj = [Ordered]@{ - 'Name' = $SQLDB.Name - 'Status' = $SQLDB.Status - 'Is Accessible?' = ConvertTo-TextYN $SQLDB.IsAccessible - 'Recovery Model' = $SQLDB.RecoveryModel - 'Size' = Switch ([string]::IsNullOrEmpty($SQLDB.SizeM)) { - $true { '--' } - $false { "$($SQLDB.SizeMB) MB" } - default { 'Unknown' } + try { + Write-PScriboMessage "Collecting SQL Server databases information." + $SQLDBs = Get-DbaDatabase -SqlInstance $SQLServer -ExcludeUser | Sort-Object -Property Name + if ($SQLDBs) { + Write-PScriboMessage "Collecting SQL Server system databases information." + Section -Style Heading4 'System Databases' { + $SQLDBInfo = @() + foreach ($SQLDB in $SQLDBs) { + try { + $InObj = [Ordered]@{ + 'Name' = $SQLDB.Name + 'Status' = $SQLDB.Status + 'Is Accessible?' = ConvertTo-TextYN $SQLDB.IsAccessible + 'Recovery Model' = $SQLDB.RecoveryModel + 'Size' = Switch ([string]::IsNullOrEmpty($SQLDB.SizeM)) { + $true { '--' } + $false { "$($SQLDB.SizeMB) MB" } + default { 'Unknown' } + } + 'Compatibility' = $CompatibilityHash[[string]$SQLDB.Compatibility] + 'Collation' = $SQLDB.Collation + 'Encrypted' = ConvertTo-TextYN $SQLDB.Encrypted + 'Last Full Backup' = Switch ($SQLDB.LastFullBackup) { + '01/01/0001 00:00:00' { "Never" } + $null { '--' } + default { $SQLDB.LastFullBackup } + } + 'Last Log Backup' = Switch ($SQLDB.LastLogBackup) { + '01/01/0001 00:00:00' { "Never" } + $null { '--' } + default { $SQLDB.LastLogBackup } + } + 'Owner' = $SQLDB.Owner } - 'Compatibility' = $CompatibilityHash[[string]$SQLDB.Compatibility] - 'Collation' = $SQLDB.Collation - 'Encrypted' = ConvertTo-TextYN $SQLDB.Encrypted - 'Last Full Backup' = Switch ($SQLDB.LastFullBackup) { - '01/01/0001 00:00:00' { "Never" } - $null { '--' } - default { $SQLDB.LastFullBackup } - } - 'Last Log Backup' = Switch ($SQLDB.LastLogBackup) { - '01/01/0001 00:00:00' { "Never" } - $null { '--' } - default { $SQLDB.LastLogBackup } - } - 'Owner' = $SQLDB.Owner + $SQLDBInfo += [PSCustomObject]$InObj + } catch { + Write-PScriboMessage -IsWarning "SQL Server System Database table: $($_.Exception.Message)" } - $SQLDBInfo += [PSCustomObject]$InObj - } catch { - Write-PScriboMessage -IsWarning "SQL Server System Database Section: $($_.Exception.Message)" } - } - if ($InfoLevel.SQLServer -ge 2) { - Paragraph "The following sections detail the configuration of the system databases within the sql server." - foreach ($SQLDB in $SQLDBInfo) { - Section -Style NOTOCHeading5 -ExcludeFromTOC "$($SQLDB.Name)" { - $TableParams = @{ - Name = "System Database - $($SQLDB.Name)" - List = $true - ColumnWidths = 50, 50 - } - if ($Report.ShowTableCaptions) { - $TableParams['Caption'] = "- $($TableParams.Name)" + if ($InfoLevel.SQLServer -ge 2) { + Paragraph "The following sections detail the configuration of the system databases within $($SQLServer.Name)." + foreach ($SQLDB in $SQLDBInfo) { + Section -Style NOTOCHeading5 -ExcludeFromTOC "$($SQLDB.Name)" { + $TableParams = @{ + Name = "System Database - $($SQLDB.Name)" + List = $true + ColumnWidths = 50, 50 + } + if ($Report.ShowTableCaptions) { + $TableParams['Caption'] = "- $($TableParams.Name)" + } + $SQLDB | Table @TableParams } - $SQLDB | Table @TableParams } + } else { + Paragraph "The following table summarises the configuration of the system databases within $($SQLServer.Name)." + BlankLine + $TableParams = @{ + Name = "System Databases" + List = $false + Columns = 'Name', 'Owner', 'Status', 'Recovery Model', 'Size' + ColumnWidths = 32, 32, 12, 12, 12 + } + if ($Report.ShowTableCaptions) { + $TableParams['Caption'] = "- $($TableParams.Name)" + } + $SQLDBInfo | Table @TableParams } - } else { - Paragraph "The following table summarises the configuration of the system databases within $($SQLDB.Name)." - BlankLine - $TableParams = @{ - Name = "System Databases" - List = $false - Columns = 'Name', 'Owner', 'Status', 'Recovery Model', 'Size' - ColumnWidths = 32, 32, 12, 12, 12 - } - if ($Report.ShowTableCaptions) { - $TableParams['Caption'] = "- $($TableParams.Name)" - } - $SQLDBInfo | Table @TableParams } } + } catch { + Write-PScriboMessage -IsWarning "SQL Server System Database Section: $($_.Exception.Message)" } - $SQLDBs = Get-DbaDatabase -SqlInstance $SQLServer -ExcludeSystem | Sort-Object -Property Name - if ($SQLDBs) { - Write-PScriboMessage "Collecting SQL Server user databases information." - Section -Style Heading4 'User Databases' { - $SQLDBInfo = @() - foreach ($SQLDB in $SQLDBs) { - try { - $InObj = [Ordered]@{ - 'Name' = $SQLDB.Name - 'Status' = $SQLDB.Status - 'Is Accessible?' = ConvertTo-TextYN $SQLDB.IsAccessible - 'Recovery Model' = $SQLDB.RecoveryModel - 'Size' = Switch ([string]::IsNullOrEmpty($SQLDB.SizeM)) { - $true { '--' } - $false { "$($SQLDB.SizeMB) MB" } - default { 'Unknown' } + try { + $SQLDBs = Get-DbaDatabase -SqlInstance $SQLServer -ExcludeSystem | Sort-Object -Property Name + if ($SQLDBs) { + Write-PScriboMessage "Collecting SQL Server user databases information." + Section -Style Heading4 'User Databases' { + $SQLDBInfo = @() + foreach ($SQLDB in $SQLDBs) { + try { + $InObj = [Ordered]@{ + 'Name' = $SQLDB.Name + 'Status' = $SQLDB.Status + 'Is Accessible?' = ConvertTo-TextYN $SQLDB.IsAccessible + 'Recovery Model' = $SQLDB.RecoveryModel + 'Size' = Switch ([string]::IsNullOrEmpty($SQLDB.SizeM)) { + $true { '--' } + $false { "$($SQLDB.SizeMB) MB" } + default { 'Unknown' } + } + 'Compatibility' = $CompatibilityHash[[string]$SQLDB.Compatibility] + 'Collation' = $SQLDB.Collation + 'Encrypted' = ConvertTo-TextYN $SQLDB.Encrypted + 'Last Full Backup' = Switch ($SQLDB.LastFullBackup) { + '01/01/0001 00:00:00' { "Never" } + $null { '--' } + default { $SQLDB.LastFullBackup } + } + 'Last Log Backup' = Switch ($SQLDB.LastLogBackup) { + '01/01/0001 00:00:00' { "Never" } + $null { '--' } + default { $SQLDB.LastLogBackup } + } + 'Owner' = $SQLDB.Owner } - 'Compatibility' = $CompatibilityHash[[string]$SQLDB.Compatibility] - 'Collation' = $SQLDB.Collation - 'Encrypted' = ConvertTo-TextYN $SQLDB.Encrypted - 'Last Full Backup' = Switch ($SQLDB.LastFullBackup) { - '01/01/0001 00:00:00' { "Never" } - $null { '--' } - default { $SQLDB.LastFullBackup } - } - 'Last Log Backup' = Switch ($SQLDB.LastLogBackup) { - '01/01/0001 00:00:00' { "Never" } - $null { '--' } - default { $SQLDB.LastLogBackup } - } - 'Owner' = $SQLDB.Owner + $SQLDBInfo += [PSCustomObject]$InObj + } catch { + Write-PScriboMessage -IsWarning "SQL Server User Database table: $($_.Exception.Message)" } - $SQLDBInfo += [PSCustomObject]$InObj - } catch { - Write-PScriboMessage -IsWarning "SQL Server User Database Section: $($_.Exception.Message)" } - } - if ($InfoLevel.SQLServer -ge 2) { - Paragraph "The following sections detail the configuration of the user databases within the sql server." - foreach ($SQLDB in $SQLDBInfo) { - Section -Style NOTOCHeading5 -ExcludeFromTOC "$($SQLDB.Name)" { - $TableParams = @{ - Name = "User Database - $($SQLDB.Name)" - List = $true - ColumnWidths = 50, 50 - } - if ($Report.ShowTableCaptions) { - $TableParams['Caption'] = "- $($TableParams.Name)" + if ($InfoLevel.SQLServer -ge 2) { + Paragraph "The following sections detail the configuration of the user databases within $($SQLServer.Name)." + foreach ($SQLDB in $SQLDBInfo) { + Section -Style NOTOCHeading5 -ExcludeFromTOC "$($SQLDB.Name)" { + $TableParams = @{ + Name = "User Database - $($SQLDB.Name)" + List = $true + ColumnWidths = 50, 50 + } + if ($Report.ShowTableCaptions) { + $TableParams['Caption'] = "- $($TableParams.Name)" + } + $SQLDB | Table @TableParams } - $SQLDB | Table @TableParams } + } else { + Paragraph "The following table summarises the configuration of the databases within $($SQLServer.Name)." + BlankLine + $TableParams = @{ + Name = "User Databases" + List = $false + Columns = 'Name', 'Owner', 'Status', 'Recovery Model', 'Size' + ColumnWidths = 32, 32, 12, 12, 12 + } + if ($Report.ShowTableCaptions) { + $TableParams['Caption'] = "- $($TableParams.Name)" + } + $SQLDBInfo | Table @TableParams } - } else { - Paragraph "The following table summarises the configuration of the databases within $($SQLDB.Name)." - BlankLine - $TableParams = @{ - Name = "User Databases" - List = $false - Columns = 'Name', 'Owner', 'Status', 'Recovery Model', 'Size' - ColumnWidths = 32, 32, 12, 12, 12 - } - if ($Report.ShowTableCaptions) { - $TableParams['Caption'] = "- $($TableParams.Name)" - } - $SQLDBInfo | Table @TableParams } } + } catch { + Write-PScriboMessage -IsWarning "SQL Server User Database Section: $($_.Exception.Message)" } } } diff --git a/Src/Private/Get-AbrWinSQLLogin.ps1 b/Src/Private/Get-AbrWinSQLLogin.ps1 new file mode 100644 index 0000000..6086e57 --- /dev/null +++ b/Src/Private/Get-AbrWinSQLLogin.ps1 @@ -0,0 +1,107 @@ +function Get-AbrWinSQLLogin { + <# + .SYNOPSIS + Used by As Built Report to retrieve Windows SQL Server login information. + .DESCRIPTION + Documents the configuration of Microsoft Windows Server in Word/HTML/Text formats using PScribo. + .NOTES + Version: 0.5.3 + Author: Jonathan Colon + Twitter: @rebelinux + Github: AsBuiltReport + Credits: Iain Brighton (@iainbrighton) - PScribo module + + .LINK + https://github.com/AsBuiltReport/AsBuiltReport.Microsoft.Windows + #> + [CmdletBinding()] + param ( + ) + + begin { + Write-PScriboMessage "SQL Server Logins InfoLevel set at $($InfoLevel.SQLServer)." + } + + process { + try { + Write-PScriboMessage "Collecting SQL Server logins information." + $SQLLogins = Get-DbaLogin -SqlInstance $SQLServer | Sort-Object -Property Name + if ($SQLLogins) { + Write-PScriboMessage "Collecting SQL Server logins information." + Section -Style Heading4 'Logins' { + $ItemInfo = @() + foreach ($Item in $SQLLogins) { + $ServerRoles = try {Get-DbaServerRoleMember -SqlInstance $SQLServer -Login $Item.Name} catch {Out-Null} + try { + $InObj = [Ordered]@{ + 'Name' = $Item.Name + 'Login Type' = $Item.LoginType -creplace '(?<=\w)([A-Z])', ' $1' + 'Server Roles' = Switch ([string]::IsNullOrEmpty($ServerRoles.Role)) { + $true { '--' } + $false { $ServerRoles.Role } + default { 'Unknown' } + } + 'Create Date' = $Item.CreateDate + 'Last Login' = Switch ([string]::IsNullOrEmpty($Item.LastLogin)) { + $true { 'Never' } + $false { $Item.LastLogin } + default { 'Unknown' } + } + 'Has Access?' = ConvertTo-TextYN $Item.HasAccess + 'Is Locked?' = Switch ([string]::IsNullOrEmpty($Item.IsLocked)) { + $true { 'No' } + $false { ConvertTo-TextYN $Item.IsLocked } + default { 'Unknown' } + } + 'Is Disabled?' = ConvertTo-TextYN $Item.IsDisabled + 'Must Change Password' = Switch ([string]::IsNullOrEmpty($Item.MustChangePassword)) { + $true { 'No' } + $false { ConvertTo-TextYN $Item.MustChangePassword } + default { 'Unknown' } + } + + + } + $ItemInfo += [PSCustomObject]$InObj + } catch { + Write-PScriboMessage -IsWarning "SQL Server System Login Section: $($_.Exception.Message)" + } + } + + if ($InfoLevel.SQLServer -ge 2) { + Paragraph "The following sections detail the configuration of the security login $($SQLServer.Name)." + foreach ($Item in $ItemInfo) { + Section -Style NOTOCHeading5 -ExcludeFromTOC "$($Item.Name)" { + $TableParams = @{ + Name = "Login - $($Item.Name)" + List = $true + ColumnWidths = 50, 50 + } + if ($Report.ShowTableCaptions) { + $TableParams['Caption'] = "- $($TableParams.Name)" + } + $Item | Table @TableParams + } + } + } else { + Paragraph "The following table summarises the configuration of the security login within $($SQLServer.Name)." + BlankLine + $TableParams = @{ + Name = "Logins" + List = $false + Columns = 'Name', 'Login Type', 'Server Roles', 'Has Access?', 'Is Locked?', 'Is Disabled?' + ColumnWidths = 30, 17, 17, 12, 12, 12 + } + if ($Report.ShowTableCaptions) { + $TableParams['Caption'] = "- $($TableParams.Name)" + } + $ItemInfo | Table @TableParams + } + } + } + } catch { + Write-PScriboMessage -IsWarning "SQL Server System Security Login Section: $($_.Exception.Message)" + } + } + end {} +} \ No newline at end of file diff --git a/Src/Private/Get-AbrWinSQLRole.ps1 b/Src/Private/Get-AbrWinSQLRole.ps1 new file mode 100644 index 0000000..b016bdb --- /dev/null +++ b/Src/Private/Get-AbrWinSQLRole.ps1 @@ -0,0 +1,88 @@ +function Get-AbrWinSQLRole { + <# + .SYNOPSIS + Used by As Built Report to retrieve Windows SQL Server roles information. + .DESCRIPTION + Documents the configuration of Microsoft Windows Server in Word/HTML/Text formats using PScribo. + .NOTES + Version: 0.5.3 + Author: Jonathan Colon + Twitter: @rebelinux + Github: AsBuiltReport + Credits: Iain Brighton (@iainbrighton) - PScribo module + + .LINK + https://github.com/AsBuiltReport/AsBuiltReport.Microsoft.Windows + #> + [CmdletBinding()] + param ( + ) + + begin { + Write-PScriboMessage "SQL Server Roles InfoLevel set at $($InfoLevel.SQLServer)." + } + + process { + try { + Write-PScriboMessage "Collecting SQL Server roles information." + $SQLRoles = Get-DbaServerRole -SqlInstance $SQLServer | Sort-Object -Property Role + if ($SQLRoles) { + Write-PScriboMessage "Collecting SQL Server roles information." + Section -Style Heading4 'Roles' { + $ItemInfo = @() + foreach ($Item in $SQLRoles) { + try { + $InObj = [Ordered]@{ + 'Name' = $Item.Role + 'Owner' = $Item.Owner + 'Login' = Switch ([string]::IsNullOrEmpty($Item.Login)) { + $true { '--' } + $false { $Item.Login } + default { 'Unknown' } + } + 'Fixed Role' = ConvertTo-TextYN $Item.IsFixedRole + 'Create Date' = $Item.DateCreated + } + $ItemInfo += [PSCustomObject]$InObj + } catch { + Write-PScriboMessage -IsWarning "SQL Server System Roles Section: $($_.Exception.Message)" + } + } + + if ($InfoLevel.SQLServer -ge 2) { + Paragraph "The following sections detail the configuration of the security roles $($SQLServer.Name)." + foreach ($Item in $ItemInfo) { + Section -Style NOTOCHeading5 -ExcludeFromTOC "$($Item.Name)" { + $TableParams = @{ + Name = "Role - $($Item.Name)" + List = $true + ColumnWidths = 50, 50 + } + if ($Report.ShowTableCaptions) { + $TableParams['Caption'] = "- $($TableParams.Name)" + } + $Item | Table @TableParams + } + } + } else { + Paragraph "The following table summarises the configuration of the security role within $($SQLServer.Name)." + BlankLine + $TableParams = @{ + Name = "Roles" + List = $false + Columns = 'Name', 'Owner', 'Login' + ColumnWidths = 25, 25, 50 + } + if ($Report.ShowTableCaptions) { + $TableParams['Caption'] = "- $($TableParams.Name)" + } + $ItemInfo | Table @TableParams + } + } + } + } catch { + Write-PScriboMessage -IsWarning "SQL Server Role Section: $($_.Exception.Message)" + } + } + end {} +} \ No newline at end of file diff --git a/Src/Public/Invoke-AsBuiltReport.Microsoft.Windows.ps1 b/Src/Public/Invoke-AsBuiltReport.Microsoft.Windows.ps1 index 152c61a..46d500f 100644 --- a/Src/Public/Invoke-AsBuiltReport.Microsoft.Windows.ps1 +++ b/Src/Public/Invoke-AsBuiltReport.Microsoft.Windows.ps1 @@ -348,11 +348,27 @@ function Invoke-AsBuiltReport.Microsoft.Windows { $script:SQLServer = Connect-DbaInstance -SqlInstance $System -TrustServerCertificate -SqlCredential $Credential if ($SQLServer) { Section -Style Heading2 "SQL Server Configuration" { - Paragraph 'The following table details the SQL Server Settings' + Paragraph 'The following table details the SQL Server settings' BlankLine - # Failover Cluster Server Configuration + # SQL Server Build Information Get-AbrWinSQLBuild + # SQL Server Security Information + Section -Style Heading3 "Security" { + Paragraph 'The following table details the SQL Server security settings' + BlankLine + # SQL Server Roles Information + Get-AbrWinSQLRole + # SQL Server Logins Information + Get-AbrWinSQLLogin + } + # SQL Server Database Information Get-AbrWinSQLDatabase + Section -Style Heading3 "Server Objects" { + Paragraph 'The following table details the SQL Server server objects settings' + BlankLine + # SQL Server Backup Devices Information + Get-AbrWinSQLBackupDevice + } } } else { Write-PScriboMessage -IsWarning "Unable to connect to SQL Instance $($Options.Instance)" From ac5fc931781c2bf851595365fb0cd66ce61a6384 Mon Sep 17 00:00:00 2001 From: Jonathan Colon Date: Sun, 18 Feb 2024 14:12:11 -0400 Subject: [PATCH 6/7] Added disconnect to the instance object --- ...Invoke-AsBuiltReport.Microsoft.Windows.ps1 | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/Src/Public/Invoke-AsBuiltReport.Microsoft.Windows.ps1 b/Src/Public/Invoke-AsBuiltReport.Microsoft.Windows.ps1 index 46d500f..4df8e61 100644 --- a/Src/Public/Invoke-AsBuiltReport.Microsoft.Windows.ps1 +++ b/Src/Public/Invoke-AsBuiltReport.Microsoft.Windows.ps1 @@ -5,7 +5,7 @@ function Invoke-AsBuiltReport.Microsoft.Windows { .DESCRIPTION Documents the configuration of Microsoft Windows Server in Word/HTML/Text formats using PScribo. .NOTES - Version: 0.5.2 + Version: 0.5.3 Author: Andrew Ramsay Editor: Jonathan Colon Twitter: @asbuiltreport @@ -363,13 +363,20 @@ function Invoke-AsBuiltReport.Microsoft.Windows { } # SQL Server Database Information Get-AbrWinSQLDatabase - Section -Style Heading3 "Server Objects" { - Paragraph 'The following table details the SQL Server server objects settings' - BlankLine - # SQL Server Backup Devices Information - Get-AbrWinSQLBackupDevice + # SQL Server Server Objects Information + $BackupDevices = Get-AbrWinSQLBackupDevice + if ($BackupDevices) { + Section -Style Heading3 "Server Objects" { + Paragraph 'The following table details the SQL Server server objects settings' + BlankLine + # SQL Server Backup Devices Information + $BackupDevices + } } } + # Disconnect SQL Instance + Write-PScriboMessage "Disconnecting SQL Instance $($Options.Instance)" + $SQLServer | Disconnect-DbaInstance | Out-Null } else { Write-PScriboMessage -IsWarning "Unable to connect to SQL Instance $($Options.Instance)" } From e328623e8ae708d2392a865e05dc0975a808ff55 Mon Sep 17 00:00:00 2001 From: Jonathan Colon Date: Sun, 18 Feb 2024 14:16:49 -0400 Subject: [PATCH 7/7] Fix Line has trailing whitespace --- Src/Private/SharedUtilsFunctions.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Src/Private/SharedUtilsFunctions.ps1 b/Src/Private/SharedUtilsFunctions.ps1 index 7eec124..3a75792 100644 --- a/Src/Private/SharedUtilsFunctions.ps1 +++ b/Src/Private/SharedUtilsFunctions.ps1 @@ -99,7 +99,7 @@ function ConvertTo-EmptyToFiller { [AllowEmptyString()] [string]$TEXT ) - + switch ([string]::IsNullOrEmpty($TEXT)) { $true { "--"; break } default { $TEXT }