From e6bfbe7fa78a0ab735fc608ffebe1fceca3ebd12 Mon Sep 17 00:00:00 2001 From: Christopher C <37060219+cavonac@users.noreply.github.com> Date: Fri, 15 Jan 2021 12:03:48 -0800 Subject: [PATCH] Cavonac/sqlassessment (#13900) * Update sql-server-assessment.ipynb code to query the assessment results removed the registered servers to avoid confusion * update link --- .../Assessments/sql-server-assessment.ipynb | 143 +++++------------- 1 file changed, 42 insertions(+), 101 deletions(-) diff --git a/extensions/azurehybridtoolkit/notebooks/hybridbook/content/Assessments/sql-server-assessment.ipynb b/extensions/azurehybridtoolkit/notebooks/hybridbook/content/Assessments/sql-server-assessment.ipynb index 66365f7ba0..de9438aaa7 100644 --- a/extensions/azurehybridtoolkit/notebooks/hybridbook/content/Assessments/sql-server-assessment.ipynb +++ b/extensions/azurehybridtoolkit/notebooks/hybridbook/content/Assessments/sql-server-assessment.ipynb @@ -2,7 +2,8 @@ "metadata": { "kernelspec": { "name": "powershell", - "display_name": "PowerShell" + "display_name": "PowerShell", + "language": "powershell" }, "language_info": { "name": "powershell", @@ -19,16 +20,15 @@ "source": [ "# SQL Server Assessment Tool\n", "\n", - "Unlike other notebooks, **do not execute all cells of this notebook!** \n", - "\n", - "A single assessment may take awhile so fill out the variables and execute the cell that matches the desired environment to perform the assessment needed. Only one of these cells needs to be executed after the variables are defined.\n", + "Performs a best-practices assessment on a local SQL Server Instance. A single assessment may take some time, so fill out the variables and execute the cell that matches the desired environment to perform the assessment needed.\n", "\n", "## Notebook Variables\n", "\n", - "| Line | Variable | Description |\n", - "| ---- | -------- | ----------- |\n", - "| 1 | ServerInstance | Name of the SQL Server instance |\n", - "| 2 | Group | (Optional) Name of the server group, if known | " + "| Line | Variable | Description | Example |\n", + "| --- | --- | --- | --- |\n", + "| 1 | ServerInstance | Name of the SQL Server instance | MSSQLSERVER |\n", + "| 2 | OutputDb | New or existing database to place assessment results in | DB1 |\n", + "| 3 | OutputTable | Target table to place assessment results (for a clean assessment, use a new table name) | AssessmentResults |" ], "metadata": { "azdata_cell_guid": "86ecfb01-8c38-4a99-92a8-687d8ec7f4b0" @@ -38,7 +38,8 @@ "cell_type": "code", "source": [ "$ServerInstance = \"\"\r\n", - "$Group = \"\"" + "$OutputDb = \"\"\r\n", + "$OutputTable = \"\"" ], "metadata": { "azdata_cell_guid": "db21129e-9bda-4db9-8d61-d2b264a3cad8" @@ -49,12 +50,12 @@ { "cell_type": "markdown", "source": [ - "## Notebook Steps\r\n", - "1. Ensure that the proper APIs and modules are installed per the prerequisites notebook\r\n", - "2. Define a service instance and group corresponding to the SQL Server instances to be assessed\r\n", - "3. Choose an example below that corresponds to the appropriate task\r\n", - "4. Execute only that example's code block and wait for results\r\n", - "5. Fix any recommended issues and rerun Assessment API until clear" + "## Notebook Steps\n", + "\n", + "1. Ensure that the proper APIs and modules are installed per the prerequisites notebook\n", + "2. Define above variables corresponding to the SQL Server instance to be assessed\n", + "3. Run the notebook to list the Assessment rules being considered, perform the assessment, and list the results\n", + "4. Fix any warnings and rerun Assessment API until clear by reviewing the results table" ], "metadata": { "azdata_cell_guid": "541f6806-f8d2-4fc5-a8fb-6d42947d1a64" @@ -63,8 +64,9 @@ { "cell_type": "markdown", "source": [ - "### Example 1 Get-SqlInstance\r\n", - "Pipe the output of the Get-SqlInstance cmdlet to the _Get-SqlAssessmentItem_ cmdlet to pass the instance object to it to perform an assessment on the entire instance. " + "### Retrieve Checks for SQL Instance\n", + "\n", + "Pipe the output of the Get-SqlInstance cmdlet to the _Get-SqlAssessmentItem_ cmdlet to get the list of checks and their status. These results ought to display a list of rules for a default assessment." ], "metadata": { "azdata_cell_guid": "c6f94c36-0566-4963-acb8-4a419758d26e" @@ -84,49 +86,8 @@ { "cell_type": "markdown", "source": [ - "### Example 2 - Get-Item\r\n", - "Get a list of available databases in the instance using the _Get-Item_ cmdlet and pipe it to _Get-SqlAssessmentItem_. " - ], - "metadata": { - "azdata_cell_guid": "30bcf8ed-cfd4-4e3b-b634-acdafab13437" - } - }, - { - "cell_type": "code", - "source": [ - "Get-Item SQLSERVER:\\SQL\\$serverInstance\\default | Get-SqlAssessmentItem" - ], - "metadata": { - "azdata_cell_guid": "4525cbe7-719a-4cc3-8ebd-5279731c3979" - }, - "outputs": [], - "execution_count": null - }, - { - "cell_type": "markdown", - "source": [ - "### Example 3 - Get-SqlDatabase\r\n", - "Or, use the _Get-SqlDatabase_ cmdlet without specifying a name to get a full list of database objects." - ], - "metadata": { - "azdata_cell_guid": "def37aca-fe6e-48ad-9794-09fd9ba77740" - } - }, - { - "cell_type": "code", - "source": [ - "Get-SqlDatabase -ServerInstance $serverInstance | Get-SqlAssessmentItem" - ], - "metadata": { - "azdata_cell_guid": "da57fa74-86b1-4d2f-a419-4035c10f0d3a" - }, - "outputs": [], - "execution_count": null - }, - { - "cell_type": "markdown", - "source": [ - "### Example 4 - Export Assessment to SQL Table\r\n", + "### Export Assessment to SQL Table\n", + "\n", "Invoke assessment for the instance and save the results to a SQL table by piping the output of the _Get-SqlInstance_ cmdlet to the _Invoke-SqlAssessment_ cmdlet. The results are piped to the _Write-SqlTableData_ cmdlet. The _Invoke-Assessment_ cmdlet is run with the -**FlattenOutput** parameter in this example. This parameter makes the output suitable for the _Write-SqlTableData_ cmdlet. The latter raises an error if the parameter is omitted." ], "metadata": { @@ -138,7 +99,7 @@ "source": [ "Get-SqlInstance -ServerInstance $serverInstance |\r\n", "Invoke-SqlAssessment -FlattenOutput |\r\n", - "Write-SqlTableData -ServerInstance $serverInstance -DatabaseName SQLAssessmentDemo -SchemaName Assessment -TableName Results -Force" + "Write-SqlTableData -ServerInstance $serverInstance -DatabaseName $outputDb -SchemaName Assessment -TableName $OutputTable -Force" ], "metadata": { "azdata_cell_guid": "28ce8df2-1da8-4462-8e91-62646642d4b1" @@ -149,56 +110,36 @@ { "cell_type": "markdown", "source": [ - "### Example 5 - SQL Server (SSMS) Locally Registered Servers\r\n", - "To gather locally saved SQL registered servers, use the SQL Server PowerShell module installed from the [Pre-requisites notebook](..\\prereqs.ipynb). " + "### Display the Results\n", + "\n", + "Use _Invoke-SqlCmd_ cmdlet to execute a short query to display the assessment results in this notebook in a custom format." ], "metadata": { - "azdata_cell_guid": "3a14a231-2ab3-45db-9afa-fbd369f4ec0f" + "azdata_cell_guid": "44bb8b3f-b456-43b4-ba2f-ae22c715a984" } }, { "cell_type": "code", "source": [ - "Import-Module SqlServer\r\n", - "$RegisteredServers = Get-ChildItem SQLSERVER:\\SQLRegistration -Recurse | Where-Object {$_.IsLocal -eq $True}\r\n", - "$RegisteredServers.Refresh() \r\n", - "$RegisteredServers | Format-List -Property *" - ], - "metadata": { - "azdata_cell_guid": "c373d5c0-3eaa-4196-8cd4-2e7d9d1896a1", - "tags": [] - }, - "outputs": [], - "execution_count": null - }, - { - "cell_type": "markdown", - "source": [ - "### Example 5 - Specifying a Central Management Server\r\n", - "Many large organizations use [Registered Servers](https://docs.microsoft.com/en-us/sql/ssms/register-servers/register-servers) to organize their networks within editors like SSMS or ADS. To take advantage of running the SQL Server Assessment API against all or a specific Registered Server Group, specify the CMS instance below. \r\n", - "\r\n", - "![](CMS.png)\r\n", - "\r\n", - "Take advantage of the [dbatools CMS module](https://dbatools.io/cms/) for this task. " - ], - "metadata": { - "azdata_cell_guid": "cd3b800e-003f-432c-88aa-5738aba11378" - } - }, - { - "cell_type": "code", - "source": [ - "# Get a list of all servers stored on the CMS instance \r\n", - "# Note that this list is pipable to all dbatools commands\r\n", - "\r\n", - "if($SqlInstance -And $Group){\r\n", - " Get-DbaRegServer -SqlInstance $SqlInstance -Group $Group\r\n", - "}else{\r\n", - " Get-DbaRegServer -SqlInstance $SqlInstance\r\n", + "$q = \"SELECT TOP (1000) * FROM [$outputDb].[Assessment].[$OutputTable]\"\r\n", + "$results = Invoke-SqlCmd -Query $q -ServerInstance $ServerInstance\r\n", + "foreach ($result in $results)\r\n", + "{\r\n", + " Write-Output \"----------------------------------------------------------\"\r\n", + " Write-Output $result.CheckName\r\n", + " if ($result.Severity -ne \"Information\")\r\n", + " {\r\n", + " Write-Warning $result.Message\r\n", + " }\r\n", + " else \r\n", + " {\r\n", + " Write-Output $result.Message\r\n", + " }\r\n", + " Write-Output $result.HelpLink\r\n", "}" ], "metadata": { - "azdata_cell_guid": "4a7bd412-5099-47b4-a737-455e01b0c172" + "azdata_cell_guid": "5bab466f-2ea0-4c3a-9ac1-923ac1548dd3" }, "outputs": [], "execution_count": null