From d5c850e3400920f8b8e26957847e03f166c094c0 Mon Sep 17 00:00:00 2001 From: Dylan Prins Date: Fri, 6 Dec 2024 12:02:21 +0100 Subject: [PATCH] Update Invoke-DeallocateVm.ps1 --- Invoke-DeallocateVm.ps1 | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/Invoke-DeallocateVm.ps1 b/Invoke-DeallocateVm.ps1 index 95896cb..4eecaea 100644 --- a/Invoke-DeallocateVm.ps1 +++ b/Invoke-DeallocateVm.ps1 @@ -1,15 +1,34 @@ +[CmdletBinding()] +param ( + [Parameter(Mandatory)] + [string] + $SubscriptionId, + + [Parameter()] + [string] + $ResourceGroupName = '' +) + # Import the required modules Import-Module Az.Compute Import-Module Az.Accounts # Authenticate to Azure -# This script assumes you have set up a Run As account in your Azure Automation account -$Connection = Get-AutomationConnection -Name "AzureRunAsConnection" -Connect-AzAccount -ServicePrincipal -TenantId $Connection.TenantId -ApplicationId $Connection.ApplicationId -CertificateThumbprint $Connection.CertificateThumbprint +try { + "Logging in to Azure..." + Connect-AzAccount -Identity +} catch { + Write-Error -Message $_.Exception + throw $_.Exception +} + # Define the resource group and the list of VMs -$resourceGroupName = "YourResourceGroupName" -$vmNames = @("VM1", "VM2", "VM3", ...) # Add all your VM names here +if ([string]::IsNullOrEmpty($ResourceGroupName)) { + Get-AzVM | Where-Object {$_.tags.EnablePrivateNetworkGC -eq "TRUE"} +} else { + Get-AzVM -ResourceGroupName $ResourceGroupName | Where-Object {$_.tags.EnablePrivateNetworkGC -eq "TRUE"} +} # Function to get the last logon time of a VM function Get-LastLogonTime { @@ -45,6 +64,6 @@ if ($idleTime -gt $idleTimeThreshold) { Write-Output "VM $vmName is not running. No action required." } } catch { - Write-Error "An error occurred while processing VM $vmName: $_" + Write-Error "An error occurred while processing VM $($vmName): $_" } }