Powershell script to list the .Net core version in an Azure Virtual Machine using Invoke-AzVmRunCommand
Tasked with finding a script to list the installed .NET Core versions in an Azure VM within a locked down environment, we tried multitude option and ended up with the below provided script which helps to run a script using the Invoke-AzVMRunCommand. This script uses Azure PowerShell 7.3.1 or later and requires latest version of Az modules.
<#
# .NOTES
Name: get-NetCoreVersion.ps1
Author: Karthik Sankaran
Version: 1.0
DateCreated: 2023-Feb-14
#¡ .INPUTS
Mandatory
TenantId - Azure Tenant Id of the subscriptions targetted.
#¡ .EXAMPLE
Example using all parameters
./get-NetCoreVersion.ps1 -TenantId <tenantId>
#>
# Parameter binding
[CmdletBinding()]
param(
[String]$TenantId = ''
)
#! Set Variables
$DateTime = Get-Date -Format ddMMyyyy-HHmm
$Output_Path_CSV = "dotNetCoreVersion_Details_" + "$TenantId" + "_" + "$DateTime" + ".csv"
$Output_Path_HTML = "dotNetCoreVersion_Details_" + "$TenantId" + "_" + "$DateTime" + ".HTML"
$body = "<html>" `
+ "<body>" `
+ "<table>"
$emptyVM = ""
$i = 0
Try {
Write-Host "Connecting to Azure Cloud..."
Connect-AzAccount -TenantId $TenantId -ErrorAction Stop -WarningAction SilentlyContinue | Out-Null
}
Catch {
Write-Warning "Cannot connect to Azure Cloud. Please check your credentials. Exiting!"
Break
}
$Subscriptions = Get-AzSubscription -TenantId $TenantId
foreach ( $Subscription in $Subscriptions ) {
Try {
Write-Host "Setting Azure Context - Subscription Name: "($Subscription.Name) "..."
Set-AzContext -Subscription $Subscription.Name
$SubscriptionId = $Subscription.SubscriptionId
}
Catch {
Write-Warning "Cannot set Azure context. Please check your Azure subscription name. Exiting!"
Break
}
$VMResources = Get-AzVM #get all the VMs
$vmdetails = New-Object System.Collections.ArrayList #variable to hold vm details in arraylist
$singleVMDetails = [ordered]@{} #show the list in the order
Write-Host "###########################"
Write-Host "RGName, VMName, dotNetCoreVersion" "SubscriptionName" -ForegroundColor Green
Write-Host "###########################" -ForegroundColor White
if ([string]::IsNullorEmpty($VMResources)) {
$emptyVM += $subscriptionId + ", "
}
else {
$body += "<tr>" `
+ "<th colspan='5' style='background-color:SteelBlue; color: white;'>" `
+ $Subscription.Name `
+ "</th>" `
+ "</tr>" `
+ "<tr>" `
+ "<th style='background-color:LightSteelBlue; color: white; padding:5px;'>Server Name</th>" `
+ "<th style='background-color:LightSteelBlue; color: white; padding:5px;'>Resource Group Name</th>" `
+ "<th style='background-color:LightSteelBlue; color: white; padding:5px;'>.Net Core Version</th>" `
+ "</tr>"
$rowcount = 0
foreach($VMResource in $VMResources) #loop through each VM
{
#¡ Build the output object and add it to the master collection for final reporting
$singleVMDetails.'VMName' = $VMResource.Name
$netCoreVersion = Invoke-AzVMRunCommand -ResourceGroupName $VMResource.ResourceGroupName -VMName $VMResource.Name -CommandId 'RunPowerShellScript' -ScriptString "(dir (Get-Command dotnet).Path.Replace('dotnet.exe', 'shared\Microsoft.NETCore.App')).Name"
$singleVMDetails.'dotNetCoreVersion' = $netCoreVersion.Value[0].Message
$singleVMDetails.'RGName' = $VMResource.ResourceGroupName
$singleVMDetails.'SubscriptionName' = $SubscriptionId
if ($rowcount % 2 -eq 0 ) {
$body += "<tr>" `
+ "<td style='background-color:PaleTurquoise; color: black; padding:5px; text-align: center;'>" `
+ $VMResource.Name `
+ "</td>" `
+ "<td style='background-color:PaleTurquoise; color: black; padding:5px; text-align: center;'>" `
+ $VMResource.ResourceGroupName `
+ "</td>"
if ($netCoreVersion.Value[0].Message -like "6.*" -or $netCoreVersion.Value[0].Message -like "7.*") {
$body += "<td style='background-color:Lime; color: black; padding:5px; text-align: center;'>" `
+ $netCoreVersion.Value[0].Message `
+ "</td>"
}
elseif ($netCoreVersion.Value[0].Message -like "3.*") {
$body += "<td style='background-color:Moccasin; color: black; padding:5px; text-align: center;'>" `
+ $netCoreVersion.Value[0].Message `
+ "</td>"
}
elseif ($netCoreVersion.Value[0].Message -like "2.*") {
$body += "<td style='background-color:Red; color: black; padding:5px; text-align: center;'>" `
+ $netCoreVersion.Value[0].Message `
+ "</td>"
}
$rowcount++
}
else {
$body += "<tr>" `
+ "<td style='background-color:PaleTurquoise; color: black; padding:5px; text-align: center;'>" `
+ $VMResource.Name `
+ "</td>" `
+ "<td style='background-color:PaleTurquoise; color: black; padding:5px; text-align: center;'>" `
+ $VMResource.ResourceGroupName `
+ "</td>"
if ($netCoreVersion.Value[0].Message -like "6.*" -or $netCoreVersion.Value[0].Message -like "7.*") {
$body += "<td style='background-color:Lime; color: black; padding:5px; text-align: center;'>" `
+ $netCoreVersion.Value[0].Message `
+ "</td>"
}
elseif ($netCoreVersion.Value[0].Message -like "3.*") {
$body += "<td style='background-color:Moccasin; color: black; padding:5px; text-align: center;'>" `
+ $netCoreVersion.Value[0].Message `
+ "</td>"
}
elseif ($netCoreVersion.Value[0].Message -like "2.*") {
$body += "<td style='background-color:Red; color: black; padding:5px; text-align: center;'>" `
+ $netCoreVersion.Value[0].Message `
+ "</td>"
}
$rowcount++
}
$vmdetails.Add((New-object PSObject -Property $singleVMDetails)) | Out-Null
Write-Host $VMResource.ResourceGroupName "," $VMResource.Name "," $netCoreVersion.Value[0].Message " , " $SubscriptionId -ForegroundColor White #Display details on screen
}
}
}
$vmdetails | Export-Csv "$Output_Path_CSV" -NoTypeInformation -Encoding UTF8 -Delimiter ',' #Export to CSV
ConvertTo-HTML -Body $body | Out-File "$Output_Path_HTML"
