在Powershell中将存储帐户名称和SKU与VM名称合并

时间:2019-02-22 13:11:37

标签: azure powershell virtual-machine azure-storage azure-storage-account

我想将StorageAccountName及其SKU添加到我的报告中,这是我现在拥有的代码:

VMs = Get-AzureRmVM -Status

 $vmOutput = $VMs | ForEach-Object {
 [PSCustomObject]@{
 "VM Name" = $_.name
 "Private IP" =""
 "VM Type" = $_.StorageProfile.osDisk.osType
 "VM Profile" = $_.HardwareProfile.VmSize
 "Environment" = $_.Tags.Environment
 "Application" = $_.Tags.Application
 "Decommission Date" = $_.Tags.Decomission
 "OS Disk Size" = $_.StorageProfile.OsDisk.DiskSizeGB
 "Data Disks Total Size" = ($_.StorageProfile.DataDisks.DiskSizeGB | Measure -Sum).Sum
 "Data Disks Amount" = ($_.StorageProfile.DataDisks | Measure ).Count
 "Managed OS Disk" = ($_.StorageProfile.OSDisk.ManagedDisk | Measure).Count
 "Managed Data Disks" = ($_.StorageProfile.DataDisks.ManagedDisk | Measure).Count
 "Powerstate" = $_.PowerState
 }
 }

 $nics = get-azurermnetworkinterface | where VirtualMachine -NE $null
 $ips =@{}

 foreach($nic in $nics){
 $vm = $VMs | Where-Object -Property id -EQ $nic.VirtualMachine.id
 $prv = $nic.IpConfigurations | select-object -ExpandProperty PrivateIpAddress
 $ips.Add($vm.Name,$prv)
 }

 foreach($vm in $vmOutput)
 {
 if($ips.ContainsKey($vm."VM Name"))
 {
 $vm."Private IP"=$ips[$vm."VM Name"]
 }
 }

 $vmOutput | sort "Environment", "VM Type", "VM Profile", "Application" | export-csv VMReport.csv -delimiter ";" -force -notypeinformation

我尝试以更少的方式IP addresses were implemented into this code来完成此操作,但是它比我预期的要困难得多,因为Get-AzureRmStorageAccount cmdlet不像Get-AzureRMNetworkInterface cmdlet那样存储VirtualMachine.Id属性。

任何想法如何将其合并到一个表中?有什么钥匙可以同时加入这两个信息?我要实现的是将StorageAccountName和SKUName列添加到$ vmOutput表中。

更新:

$sat =@{}

$OutFile = "..."

#part1

foreach ($vmdetails in $VMs)
{
$ResourceGroupName=$vmdetails.ResourceGroupName
$VMName=$vmdetails.name
$storage=$vmdetails.StorageProfile.OsDisk.Vhd.Uri
$DataDiks=$vmdetails.StorageProfile.OsDisk.Name
$ss=$storage.split('/')[2]

$OSStorageAccountName=$ss.split('.')[0]
$DiskType="OSDisk"
$StroageAccountLocation=($StroageAccDetail | where  {$_.StorageAccountName -eq $OSStorageAccountName }).location
$StorageAccountType=($StroageAccDetail | where  {$_.StorageAccountName -eq $OSStorageAccountName }).AccountType
"$sub,$ResourceGroupName,$VMName,$DataDiks,$DiskType,$OSStorageAccountName" | Out-File -FilePath $OutFile -Append

$vmdatadisks=$vmdetails.StorageProfile.DataDisks

if($vmdatadisks -ne $null){
foreach($vmDatadsik in $vmdatadisks)
{
$vmDatadsikss=$vmDatadsik.vhd.uri
$DiskType="DataDisk"
$ss=$vmDatadsikss.split('/')[2]
$DataDiks=$vmDatadsik.Name
$dataStorageAccountName=$ss.split('.')[0]
"$sub,$ResourceGroupName,$VMName,$DataDiks,$DiskType,$OSStorageAccountName" | Out-File -FilePath $OutFile -Append
}
}
}

#part2

$VMs = Get-AzureRmVM -ResourceGroupName "..." -Name "..."
$storageAccountName = $VMs.StorageProfile.OsDisk.Vhd.Uri.Split("/")[2].Split(".")[0]
Get-AzureRmStorageAccount -StorageAccountName $storageAccountName -ResourceGroupName "..."

1 个答案:

答案 0 :(得分:1)

如果我误会你,请纠正我。

假设您引用的是帐户存储的sku,也可以在$ vmOutput中为SA名称和SKUName添加占位符。

 $VMs = Get-AzureRmVM -Status  

 $vmOutput = $VMs | ForEach-Object {
 [PSCustomObject]@{
 "VM Name" = $_.name
 "ResourceGroupName" =$_.ResourceGroupName #you must include this parameter
 "storage" = $_.StorageProfile.OsDisk.Vhd.Uri #you must include this parameter
 #your other property
 #add placeholder for SA Name and SKUName like below
 "StorageAccountName" =""
 "StorageAccountSKUName" =""
}
}

然后,迭代$ vmoutput中的所有虚拟机,找到相关的存储帐户和sku,然后将值添加到$ vmoutput中:

foreach($v in $vmOutput){
$resourceGroups = $v.ResourceGroupName
$storage=$v.storage

if($storage -ne $null)
{
$ss=$storage.split('/')[2]
$OSStorageAccountName=$ss.split('.')[0]

$s1 = Get-AzureRmStorageAccount -ResourceGroupName $resourceGroups -StorageAccountName $OSStorageAccountName

#add the value to $vmoutput
$v.StorageAccountName =$OSStorageAccountName
$v.StorageAccountSKUName=$s1.Sku.name
}
else
{
$v.StorageAccountName ="no value"
$v.StorageAccountSKUName="no value"
}
}