-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlistAllVMsWithDiskSize.ps1
More file actions
50 lines (40 loc) · 1.44 KB
/
listAllVMsWithDiskSize.ps1
File metadata and controls
50 lines (40 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# Connect to Azure account
Connect-AzAccount
# Get all subscriptions in the tenant
$subscriptions = Get-AzSubscription
# Create an array to store VM details
$vmDetails = @()
foreach ($subscription in $subscriptions) {
# Set the current subscription context
Set-AzContext -SubscriptionId $subscription.Id
# Get the list of VMs in the subscription
$vms = Get-AzVM
foreach ($vm in $vms) {
# Get VM properties
$vmName = $vm.Name
$vmSize = $vm.HardwareProfile.VmSize
$vmLocation = $vm.Location
$vmResourceGroup = $vm.ResourceGroupName
# Get Disks associated with the VM
$disks = Get-AzDisk | Where-Object { $_.ManagedBy -eq $vm.Id }
foreach ($disk in $disks) {
$diskName = $disk.Name
$diskSize = $disk.DiskSizeGB
$diskSku = $disk.Sku.Name
# Create a custom object to hold the details
$vmDetail = [PSCustomObject]@{
VMName = $vmName
VMSize = $vmSize
DiskName = $diskName
DiskSize = $diskSize
DiskSKU = $diskSku
Region = $vmLocation
ResourceGroup = $vmResourceGroup
}
# Add the custom object to the array
$vmDetails += $vmDetail
}
}
}
# Output the VM details in a table format
$vmDetails | Format-Table -AutoSize