Get logged in user details from Azure VM

Here is a PowerShell script that can be used to retrieve the logged-in user names on Azure virtual machines:


$VMs = Get-AzVM foreach ($VM in $VMs) { $VMName = $VM.Name Write-Host "Getting logged in users for VM: $VMName" $Session = New-PSSession -ComputerName $VMName $Users = Invoke-Command -Session $Session -ScriptBlock { query user } $Users | Select-Object @{n='VM Name';e={$VMName}}, UserName }


Note: You'll need to have the Azure PowerShell module installed and authenticated to your Azure account before running this script.

This script uses the Get-AzVM cmdlet to retrieve a list of all virtual machines in the Azure subscription.

 The foreach loop is used to iterate over each virtual machine, and the Write-Output cmdlet is used to display the username for each virtual machine. 


No comments:

Post a Comment