Azure VDI virtual machines

Santosh Savakar 0 Reputation points
2023-04-26T16:45:07.0966667+00:00

How do we automatically delete Azure VDI machine when a user has not logged in for 30days

Azure Virtual Machines
Azure Virtual Machines
An Azure service that is used to provision Windows and Linux virtual machines.
8,100 questions
Azure Virtual Desktop
Azure Virtual Desktop
A Microsoft desktop and app virtualization service that runs on Azure. Previously known as Windows Virtual Desktop.
1,595 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Prrudram-MSFT 26,591 Reputation points
    2023-04-28T16:25:49.61+00:00

    Hello @Santosh Savakar

    To automatically delete an Azure VDI machine when a user has not logged in for 30 days, you can use Azure Automation and a PowerShell script. Here are the high-level steps:

    Create an Azure Automation account in your Azure subscription.

    Create a new PowerShell runbook in the Azure Automation account.

    Write a PowerShell script that checks the last login time for each user on the VDI machine and deletes the machine if no user has logged in for 30 days.

    Schedule the PowerShell runbook to run on a regular basis (e.g., daily) using a recurring schedule in Azure Automation.

    Here is an example PowerShell script that you can use as a starting point:

    $vmName = "MyVDIMachine"
    $daysToKeep = 30
    $lastLogon = (Get-WmiObject -Class Win32_UserProfile -ComputerName $vmName | Select-Object -Property LastUseTime | Measure-Object -Property LastUseTime -Maximum).Maximum
    $daysSinceLastLogon = (New-TimeSpan -Start $lastLogon).Days
    if ($daysSinceLastLogon -gt $daysToKeep) {
        Remove-AzVM -ResourceGroupName "MyResourceGroup" -Name $vmName -Force
    }
    

    This script uses the Get-WmiObject cmdlet to retrieve the last login time for each user on the VDI machine, calculates the number of days since the last login, and deletes the machine if no user has logged in for 30 days.

    Note that you will need to modify the script to use the appropriate Azure PowerShell cmdlets for your environment and to specify the correct resource group and VM name.

    I hope this helps! Let me know if you have any other questions or if there is anything else I can assist you with.

    Please accept answer and upvote if the above information is helpful for the benefit of the community.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.