gpt4 book ai didi

azure - PowerShell 列出所有 Azure VM 数据磁盘大小困惑

转载 作者:行者123 更新时间:2023-12-03 00:54:27 26 4
gpt4 key购买 nike

基于 Microsoft Azure 的此知识库文章:https://learn.microsoft.com/en-us/azure/virtual-machines/windows/faq#how-much-storage-can-i-use-with-a-virtual-machine-

我们可以将多个数据磁盘附加到 Azure VM。

但是,我们如何在 PowerShell 查询中动态显示它?

Get-AzVM | ForEach-Object {
$size = $_.HardwareProfile.VmSize
$vmsize = Get-AzVMSize -vmname $_.Name -ResourceGroupName $_.ResourceGroupName | Where-Object { $_.name -eq $size }
$nic = $_.NetworkProfile.NetworkInterfaces.id.split('/') | Select-Object -Last 1

# Implicitly outputs an object with the given properties
[pscustomobject]@{
Location = $_.Location
Name = $_.Name
osdiskingb = $_.StorageProfile.OsDisk.DiskSizeGB
data1diskingb = ($_.StorageProfile.DataDisks[0].DiskSizeGB)
data2diskingb = ($_.StorageProfile.DataDisks[1].DiskSizeGB)
data3diskingb = ($_.StorageProfile.DataDisks[2].DiskSizeGB)
memory = [Math]::Round(($vmsize.MemoryInMB)/1024, 1)
cpu = $vmsize.NumberOfCores
nic = $nic
ip = (Get-AzNetworkInterface -Name $nic).ipconfigurations.privateipaddress
VMTags = $_.Tags
VMStatus = $_.StatusCode
State = $_.ProvisioningState
}
} | ogv

使用 Zett42 创建的上述静态 PowerShell 查询:Optimize PowerShell code to avoid calling the cmdlet multiple times inside calculated properties?我只能手动复制粘贴/重复下面这一行:

data1diskingb = ($_.StorageProfile.DataDisks[0].DiskSizeGB)
data2diskingb = ($_.StorageProfile.DataDisks[1].DiskSizeGB)
data3diskingb = ($_.StorageProfile.DataDisks[2].DiskSizeGB)

如果Azure VM有超过5-10个数据磁盘,我必须相应地复制并粘贴多次。

所以我需要更新脚本,以便它可以动态显示所有数据磁盘。

最佳答案

$vm                 = Get-AzVM -Name <VM-Name> -ResourceGroupName <RG-Name>
$os_disk_size = $vm.StorageProfile.OsDisk.DiskSizeGB
$total_disk_size = 0
$data_disk_size = 0
$data_disks = (Get-AzVM -Name $vm_name).StorageProfile.DataDisks
foreach ($data_disk in $data_disks) {
$data_disk_size = $data_disk_size + $data_disk.DiskSizeGB
}
$total_disk_size = $os_disk_size + $data_disk_size
Write-Host "OS Disk size in GB: $os_disk_size"
Write-Host "Total Data disk($data_disks.count) size in GB: $data_disk_size"
Write-Host "Total disk size allocated to $vm.name: $total_disk_size"

您应该能够从中获得很多详细信息。

关于azure - PowerShell 列出所有 Azure VM 数据磁盘大小困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73403990/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com