@@ -180,9 +180,9 @@ function AddVCores {
180180 }
181181}
182182
183- function Discovery {
183+ function DiscoveryOnWindows {
184184
185- # This function is to run on each VM to detect if SQL server is installed
185+ # This script checks if SQL Server is installed on Windows
186186
187187 [bool ] $SqlInstalled = $false
188188 $regPath = ' HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server'
@@ -193,6 +193,19 @@ function Discovery {
193193 Write-Output $SqlInstalled
194194}
195195
196+ #
197+ # This script checks if SQL Server is installed on Linux
198+ #
199+ #
200+ $DiscoveryOnLinux =
201+ ' if ! systemctl is-active --quiet mssql-server.service; then
202+ echo "False"
203+ exit
204+ else
205+ echo "True"
206+ fi'
207+
208+
196209# The following block is required for runbooks only
197210if ($UseInRunbook ){
198211
@@ -235,7 +248,8 @@ $GetVCoresDef = $function:GetVCores.ToString()
235248$AddVCoresDef = $function: AddVCores.ToString ()
236249
237250# Create a script file with the SQL server discovery logic
238- New-Item - ItemType file - path DiscoverSql.ps1 - value $function: Discovery.ToString () - Force | Out-Null
251+ New-Item - ItemType file - path DiscoverSql.ps1 - value $function: DiscoveryOnWindows.ToString () - Force | Out-Null
252+ New-Item - ItemType file - path DiscoverSql.sh - value $DiscoveryOnLinux - Force | Out-Null
239253
240254# Subscriptions to scan
241255
@@ -385,30 +399,12 @@ foreach ($sub in $subscriptions){
385399
386400 # 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
387401 # NOTE: ForEach-Object -Parallel is not supported in Runbooks (requires PS v7.1)
388- if ($UseInRunbook ){
389- Get-AzVM - Status | Where-Object { $_.powerstate -eq ' VM running' } | ForEach-Object {
390- $vCores = GetVCores - type ' virtualMachines' - name $_.HardwareProfile.VmSize
391- $sql_vm = Get-AzSqlVm - ResourceGroupName $_.ResourceGroupName - Name $_.Name - ErrorAction Ignore
392- if ($sql_vm ) {
393- AddVCores - Tier $sql_vm.Sku - LicenseType $sql_vm.LicenseType - CoreCount $vCores
394- }
395- else {
396- try {
397- $out = Invoke-AzVMRunCommand - ResourceGroupName $_.ResourceGroupName - Name $_.Name - CommandId ' RunPowerShellScript' - ScriptPath ' DiscoverSql.ps1' - ErrorAction Stop
398- if ($out.Value [0 ].Message -contains ' True' ){
399- $subtotal.unreg_sqlvm += $vCores
400- }
401- }
402- catch {
403- }
404- }
405- }
406- }
407- else {
408- Get-AzVM - Status | Where-Object { $_.powerstate -eq ' VM running' } | ForEach-Object - ThrottleLimit 10 - Parallel {
402+ if ($PSVersionTable.PSVersion.Major -ge 7 ){
403+ $vms = Get-AzVM - Status | Where-Object { $_.powerstate -eq ' VM running' } | ForEach-Object - ThrottleLimit 10 - Parallel {
409404 $function: GetVCores = $using :GetVCoresDef
410405 $vCores = GetVCores - type ' virtualMachines' - name $_.HardwareProfile.VmSize
411406 $sql_vm = Get-AzSqlVm - ResourceGroupName $_.ResourceGroupName - Name $_.Name - ErrorAction Ignore
407+
412408 if ($sql_vm ) {
413409 switch ($sql_vm.Sku ) {
414410 " Enterprise" {
@@ -427,7 +423,7 @@ foreach ($sub in $subscriptions){
427423 default {$ ($using :subtotal ).payg_std += $vCores }
428424 }
429425 }
430- " Developer" {
426+ " Developer" {
431427 $ ($using :subtotal ).developer += $vCores
432428 }
433429 " Express" {
@@ -436,16 +432,72 @@ foreach ($sub in $subscriptions){
436432 }
437433 }
438434 else {
439- try {
440- $out = Invoke-AzVMRunCommand - ResourceGroupName $_.ResourceGroupName - Name $_.Name - CommandId ' RunPowerShellScript' - ScriptPath ' DiscoverSql.ps1' - ErrorAction Stop
441- if ($out.Value [0 ].Message -contains ' True' ){
442- $ ($using :subtotal ).unreg_sqlvm += $vCores
435+ if ($_.StorageProfile.OSDisk.OSType -eq " Windows" ){
436+ $params = @ {
437+ ResourceGroupName = $_.ResourceGroupName
438+ Name = $_.Name
439+ CommandId = ' RunPowerShellScript'
440+ ScriptPath = ' DiscoverSql.ps1'
441+ ErrorAction = ' Stop'
443442 }
444443 }
444+ else {
445+ $params = @ {
446+ ResourceGroupName = $_.ResourceGroupName
447+ Name = $_.Name
448+ CommandId = ' RunShellScript'
449+ ScriptPath = ' DiscoverSql.sh'
450+ ErrorAction = ' Stop'
451+ }
452+ }
453+ try {
454+ $out = Invoke-AzVMRunCommand @params
455+ if ($out.Value [0 ].Message.Contains(' True' )){
456+ $ ($using :subtotal ).unreg_sqlvm += $vCores
457+ }
458+ }
445459 catch {
460+ write-host $params.Name " No acceaa"
446461 }
447462 }
448- }
463+ }
464+ }
465+ else {
466+ Get-AzVM - Status | Where-Object { $_.powerstate -eq ' VM running' } | ForEach-Object {
467+ $vCores = GetVCores - type ' virtualMachines' - name $_.HardwareProfile.VmSize
468+ $sql_vm = Get-AzSqlVm - ResourceGroupName $_.ResourceGroupName - Name $_.Name - ErrorAction Ignore
469+ if ($sql_vm ) {
470+ AddVCores - Tier $sql_vm.Sku - LicenseType $sql_vm.LicenseType - CoreCount $vCores
471+ }
472+ else {
473+ if ($_.StorageProfile.OSDisk.OSType -eq " Windows" ){
474+ $params = @ {
475+ ResourceGroupName = $_.ResourceGroupName
476+ Name = $_.Name
477+ CommandId = ' RunPowerShellScript'
478+ ScriptPath = ' DiscoverSql.ps1'
479+ ErrorAction = ' Stop'
480+ }
481+ }
482+ else {
483+ $params = @ {
484+ ResourceGroupName = $_.ResourceGroupName
485+ Name = $_.Name
486+ CommandId = ' RunShellScript'
487+ ScriptPath = ' DiscoverSql.sh'
488+ ErrorAction = ' Stop'
489+ }
490+ }try {
491+ $out = Invoke-AzVMRunCommand @params
492+ if ($out.Value [0 ].Message.Contains(' True' )){
493+ $subtotal.unreg_sqlvm += $vCores
494+ }
495+ }
496+ catch {
497+ write-host $params.Name " No acceaa"
498+ }
499+ }
500+ }
449501 }
450502 [system.gc ]::Collect()
451503
0 commit comments