From 46dcf9f7c4e9d291f25b5f5e9bf36df1983a4826 Mon Sep 17 00:00:00 2001 From: anosov1960 Date: Mon, 15 Mar 2021 11:27:50 -0700 Subject: [PATCH] Made reporting self-hosted SQL servers optional to save time --- .../azure-hybrid-benefit/DiscoverSql.ps1 | 11 ++ .../azure-hybrid-benefit/DiscoverSql.sh | 6 + samples/manage/azure-hybrid-benefit/README.md | 3 +- .../sql-license-usage.ps1 | 112 ++++++++++-------- 4 files changed, 79 insertions(+), 53 deletions(-) create mode 100644 samples/manage/azure-hybrid-benefit/DiscoverSql.ps1 create mode 100644 samples/manage/azure-hybrid-benefit/DiscoverSql.sh diff --git a/samples/manage/azure-hybrid-benefit/DiscoverSql.ps1 b/samples/manage/azure-hybrid-benefit/DiscoverSql.ps1 new file mode 100644 index 00000000..874a4d5c --- /dev/null +++ b/samples/manage/azure-hybrid-benefit/DiscoverSql.ps1 @@ -0,0 +1,11 @@ + + +# This script checks if SQL Server is installed on Windows + + [bool] $SqlInstalled = $false + $regPath = 'HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server' + if (Test-Path $regPath) { + $inst = (get-itemproperty $regPath).InstalledInstances + $SqlInstalled = ($inst.Count -gt 0) + } + Write-Output $SqlInstalled diff --git a/samples/manage/azure-hybrid-benefit/DiscoverSql.sh b/samples/manage/azure-hybrid-benefit/DiscoverSql.sh new file mode 100644 index 00000000..5b430e3a --- /dev/null +++ b/samples/manage/azure-hybrid-benefit/DiscoverSql.sh @@ -0,0 +1,6 @@ +if ! systemctl is-active --quiet mssql-server.service; then + echo "False" + exit + else + echo "True" + fi \ No newline at end of file diff --git a/samples/manage/azure-hybrid-benefit/README.md b/samples/manage/azure-hybrid-benefit/README.md index f1c4602c..99344ff4 100644 --- a/samples/manage/azure-hybrid-benefit/README.md +++ b/samples/manage/azure-hybrid-benefit/README.md @@ -49,7 +49,7 @@ The following resources are in scope for the license utilization analysis: The script accepts the following command line parameters: -| **Parameter**                                 | **Value**                                                                       | **Description** | +| **Parameter**                                         | **Value**                                                                       | **Description** | |:--|:--|:--| |-SubId|subscription_id *or* a file_name|Optional: subscription id or a .csv file with the list of subscriptions1| |-UseInRunbook| \$True or \$False (default) |Optional: must be $True when executed as a Runbook| @@ -57,6 +57,7 @@ The script accepts the following command line parameters: |-Database|database_name|Optional: database name where data will be saved.
Must be accompanied by -Server and -Cred| |-Cred|credential_object|Optional: value of type PSCredential to securely pass database user and password| |-FilePath|csv_file_name|Optional: filename where the data will be saved in a .csv format. Ignored if database parameters are specified| +|-ShowUnregistered|\$True or \$False (default)|Optional: causes the script to report the total size of VMs that self-host SQL server instance that is unregistered with the IaaS SQL extension| 1You can create a .csv file using the following command and then edit to remove the subscriptions you don't want to scan. ```PowerShell diff --git a/samples/manage/azure-hybrid-benefit/sql-license-usage.ps1 b/samples/manage/azure-hybrid-benefit/sql-license-usage.ps1 index dd822afd..d32e4bc4 100644 --- a/samples/manage/azure-hybrid-benefit/sql-license-usage.ps1 +++ b/samples/manage/azure-hybrid-benefit/sql-license-usage.ps1 @@ -23,11 +23,12 @@ # -Cred [credential_object] (Required to save data to the database) # -FilePath [csv_file_name] (Required to save data in a .csv format. Ignored if database parameters are specified) # -UseInRunbook [True] | [False] (Required when executed as a Runbook) +# -ShowUnregistered [True] | [False] (Optional. If specified, checks every VM if SQL server is installed) # param ( [Parameter (Mandatory= $false)] - [string] $SubId, + [string] $SubId = '4f6d3845-d3e3-4c31-bdf0-c73464aaff0e', [Parameter (Mandatory= $false)] [string] $Server, [Parameter (Mandatory= $false)] @@ -39,7 +40,10 @@ param ( [Parameter (Mandatory= $false)] [bool] $UseInRunbook = $false, [Parameter (Mandatory= $false)] - [bool] $ShowEC = $false + [bool] $ShowEC = $false, + [Parameter (Mandatory= $false)] + [bool] $ShowUnregistered = $false + ) function CheckModule ($m) { @@ -418,32 +422,34 @@ foreach ($sub in $subscriptions){ } } else { - if ($_.StorageProfile.OSDisk.OSType -eq "Windows"){ - $params =@{ - ResourceGroupName = $_.ResourceGroupName - Name = $_.Name - CommandId = 'RunPowerShellScript' - ScriptPath = 'DiscoverSql.ps1' - ErrorAction = 'Stop' - } - } - else { - $params =@{ - ResourceGroupName = $_.ResourceGroupName - Name = $_.Name - CommandId = 'RunShellScript' - ScriptPath = 'DiscoverSql.sh' - ErrorAction = 'Stop' - } - } - try { - $out = Invoke-AzVMRunCommand @params - if ($out.Value[0].Message.Contains('True')){ - $($using:subtotal).unreg_sqlvm += $vCores - } - } - catch { - write-host $params.Name "No acceaa" + if ($($using:ShowUnregistered)){ + if ($_.StorageProfile.OSDisk.OSType -eq "Windows"){ + $params =@{ + ResourceGroupName = $_.ResourceGroupName + Name = $_.Name + CommandId = 'RunPowerShellScript' + ScriptPath = 'DiscoverSql.ps1' + ErrorAction = 'Stop' + } + } + else { + $params =@{ + ResourceGroupName = $_.ResourceGroupName + Name = $_.Name + CommandId = 'RunShellScript' + ScriptPath = 'DiscoverSql.sh' + ErrorAction = 'Stop' + } + } + try { + $out = Invoke-AzVMRunCommand @params + if ($out.Value[0].Message.Contains('True')){ + $($using:subtotal).unreg_sqlvm += $vCores + } + } + catch { + write-host $params.Name "No acceaa" + } } } } @@ -456,31 +462,33 @@ foreach ($sub in $subscriptions){ AddVCores -Tier $sql_vm.Sku -LicenseType $sql_vm.LicenseType -CoreCount $vCores } else { - if ($_.StorageProfile.OSDisk.OSType -eq "Windows"){ - $params =@{ - ResourceGroupName = $_.ResourceGroupName - Name = $_.Name - CommandId = 'RunPowerShellScript' - ScriptPath = 'DiscoverSql.ps1' - ErrorAction = 'Stop' - } - } - else { - $params =@{ - ResourceGroupName = $_.ResourceGroupName - Name = $_.Name - CommandId = 'RunShellScript' - ScriptPath = 'DiscoverSql.sh' - ErrorAction = 'Stop' - } - }try { - $out = Invoke-AzVMRunCommand @params - if ($out.Value[0].Message.Contains('True')){ - $subtotal.unreg_sqlvm += $vCores + if ($ShowUnregistered){ + if ($_.StorageProfile.OSDisk.OSType -eq "Windows"){ + $params =@{ + ResourceGroupName = $_.ResourceGroupName + Name = $_.Name + CommandId = 'RunPowerShellScript' + ScriptPath = 'DiscoverSql.ps1' + ErrorAction = 'Stop' + } + } + else { + $params =@{ + ResourceGroupName = $_.ResourceGroupName + Name = $_.Name + CommandId = 'RunShellScript' + ScriptPath = 'DiscoverSql.sh' + ErrorAction = 'Stop' + } + }try { + $out = Invoke-AzVMRunCommand @params + if ($out.Value[0].Message.Contains('True')){ + $subtotal.unreg_sqlvm += $vCores + } + } + catch { + write-host $params.Name "No acceaa" } - } - catch { - write-host $params.Name "No acceaa" } } }