Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions DiscoverSql.ps1
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@


# This function is to run on each VM to detect if SQL server is installed

#
# This script checks if SQL Server is installed on Windows
#
#
[bool] $SqlInstalled = $false
$regPath = 'HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server'
if (Test-Path $regPath) {
Expand Down
6 changes: 6 additions & 0 deletions DiscoverSql.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
if ! systemctl is-active --quiet mssql-server.service; then
echo "False"
exit
else
echo "True"
fi
11 changes: 11 additions & 0 deletions samples/manage/azure-hybrid-benefit/DiscoverSql.ps1
Original file line number Diff line number Diff line change
@@ -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
6 changes: 6 additions & 0 deletions samples/manage/azure-hybrid-benefit/DiscoverSql.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
if ! systemctl is-active --quiet mssql-server.service; then
echo "False"
exit
else
echo "True"
fi
2 changes: 2 additions & 0 deletions samples/manage/azure-hybrid-benefit/sql-license-usage.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
"Date","Time","Subscription Name","Subscription ID","AHB ECs","PAYG ECs","AHB Std vCores","AHB Ent vCores","PAYG Std vCores","PAYG Ent vCores","HADR Std vCores","HADR Ent vCores","Developer vCores","Express vCores","Unregistered vCores","Unknown vCores"
"2021-03-09","22:22:47","ADS TINA Engineering","a5082b19-8a6e-4bc5-8fdd-8ef39dfebc39","0","0","8","0","13","0","0","0","18","0","0","0"
112 changes: 82 additions & 30 deletions samples/manage/azure-hybrid-benefit/sql-license-usage.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,9 @@ function AddVCores {
}
}

function Discovery {
function DiscoveryOnWindows {

# This function is to run on each VM to detect if SQL server is installed
# This script checks if SQL Server is installed on Windows

[bool] $SqlInstalled = $false
$regPath = 'HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server'
Expand All @@ -193,6 +193,19 @@ function Discovery {
Write-Output $SqlInstalled
}

#
# This script checks if SQL Server is installed on Linux
#
#
$DiscoveryOnLinux =
'if ! systemctl is-active --quiet mssql-server.service; then
echo "False"
exit
else
echo "True"
fi'


#The following block is required for runbooks only
if ($UseInRunbook){

Expand Down Expand Up @@ -235,7 +248,8 @@ $GetVCoresDef = $function:GetVCores.ToString()
$AddVCoresDef = $function:AddVCores.ToString()

# Create a script file with the SQL server discovery logic
New-Item -ItemType file -path DiscoverSql.ps1 -value $function:Discovery.ToString() -Force | Out-Null
New-Item -ItemType file -path DiscoverSql.ps1 -value $function:DiscoveryOnWindows.ToString() -Force | Out-Null
New-Item -ItemType file -path DiscoverSql.sh -value $DiscoveryOnLinux -Force | Out-Null

# Subscriptions to scan

Expand Down Expand Up @@ -385,30 +399,12 @@ foreach ($sub in $subscriptions){

#Scan all VMs with SQL server installed using a parallel loop (up to 10 at a time). For that reason function AddVCores is not used
#NOTE: ForEach-Object -Parallel is not supported in Runbooks (requires PS v7.1)
if ($UseInRunbook){
Get-AzVM -Status | Where-Object { $_.powerstate -eq 'VM running' } | ForEach-Object {
$vCores = GetVCores -type 'virtualMachines' -name $_.HardwareProfile.VmSize
$sql_vm = Get-AzSqlVm -ResourceGroupName $_.ResourceGroupName -Name $_.Name -ErrorAction Ignore
if ($sql_vm) {
AddVCores -Tier $sql_vm.Sku -LicenseType $sql_vm.LicenseType -CoreCount $vCores
}
else {
try {
$out = Invoke-AzVMRunCommand -ResourceGroupName $_.ResourceGroupName -Name $_.Name -CommandId 'RunPowerShellScript' -ScriptPath 'DiscoverSql.ps1' -ErrorAction Stop
if ($out.Value[0].Message -contains 'True'){
$subtotal.unreg_sqlvm += $vCores
}
}
catch {
}
}
}
}
else {
Get-AzVM -Status | Where-Object { $_.powerstate -eq 'VM running' } | ForEach-Object -ThrottleLimit 10 -Parallel {
if ($PSVersionTable.PSVersion.Major -ge 7){
$vms = Get-AzVM -Status | Where-Object { $_.powerstate -eq 'VM running' } | ForEach-Object -ThrottleLimit 10 -Parallel {
$function:GetVCores = $using:GetVCoresDef
$vCores = GetVCores -type 'virtualMachines' -name $_.HardwareProfile.VmSize
$sql_vm = Get-AzSqlVm -ResourceGroupName $_.ResourceGroupName -Name $_.Name -ErrorAction Ignore

if ($sql_vm) {
switch ($sql_vm.Sku) {
"Enterprise" {
Expand All @@ -427,7 +423,7 @@ foreach ($sub in $subscriptions){
default {$($using:subtotal).payg_std += $vCores}
}
}
"Developer" {
"Developer" {
$($using:subtotal).developer += $vCores
}
"Express" {
Expand All @@ -436,16 +432,72 @@ foreach ($sub in $subscriptions){
}
}
else {
try {
$out = Invoke-AzVMRunCommand -ResourceGroupName $_.ResourceGroupName -Name $_.Name -CommandId 'RunPowerShellScript' -ScriptPath 'DiscoverSql.ps1' -ErrorAction Stop
if ($out.Value[0].Message -contains 'True'){
$($using:subtotal).unreg_sqlvm += $vCores
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"
}
}
}
}
}
else {
Get-AzVM -Status | Where-Object { $_.powerstate -eq 'VM running' } | ForEach-Object {
$vCores = GetVCores -type 'virtualMachines' -name $_.HardwareProfile.VmSize
$sql_vm = Get-AzSqlVm -ResourceGroupName $_.ResourceGroupName -Name $_.Name -ErrorAction Ignore
if ($sql_vm) {
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
}
}
catch {
write-host $params.Name "No acceaa"
}
}
}
}
[system.gc]::Collect()

Expand Down