0

我想将 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 地址,但这比我预期的要困难得多,因为 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 "..."
4

1 回答 1

1

如果我误解了你,请纠正我。

假设您引用的 sku 是帐户存储的 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 中的所有 vm,找到相关的 storage account 和 sku,然后为 $vmoutput 添加 value:

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"
}
}
于 2019-02-25T07:24:49.860 回答