这里发两个脚本,用于获取某个账号下的所有订阅中的虚拟机的磁盘信息,分ASM和ARM两部分。
ASM版本:
$Cred = New-Object System.Management.Automation.PSCredential("XXXXXXXXX@XXXXXXXXX.partner.onmschina.cn",(ConvertTo-SecureString "XXXXXXXXX" -AsPlainText -Force));
$AzureRMCred = Get-Credential -Credential $Cred;
Add-AzureAccount -Environment AzureChinaCloud -Credential $AzureRMCred;
Function WriteToXMLOutput($str)
{
$OutputfilePath = $PSScriptRoot + "\VM_and_Disks.xml";
$str | Out-File $OutputfilePath -Append;
}
Function BuildLevelSpace($level)
{
$levelSpace = " ";
$result = "";
for ($i = 1; $i -lt $level; $i++)
{
$result += $levelSpace;
}
return $result;
}
Function BuildSubscriptionsHead()
{
return (BuildLevelSpace 1) + "<subscriptions>";
}
Function BuildSubscriptionsEnd()
{
return (BuildLevelSpace 1) + "</subscriptions>";
}
Function BuildSubscriptionHead($subscriptionName)
{
return (BuildLevelSpace 2) + "<subscription name='$subscriptionName'>";
}
Function BuildSubscriptionEnd($subscriptionName)
{
return (BuildLevelSpace 2) + "</subscription>";
}
Function BuildVirtualMachinesHead()
{
return (BuildLevelSpace 3) + "<virtualmachines>";
}
Function BuildVirtualMachinesEnd()
{
return (BuildLevelSpace 3) + "</virtualmachines>";
}
Function BuildVirtualMachineHead($virtualmachineName, $OSType, $Size)
{
return (BuildLevelSpace 4) + "<virtualmachine name='$virtualmachineName' OS='$OSType' size='$Size'>";
}
Function BuildVirtualMachineEnd()
{
return (BuildLevelSpace 4) + "</virtualmachine>";
}
Function BuildOutputXml()
{
$subscriptions = Get-AzureSubscription;
WriteToXMLOutput ((BuildLevelSpace 1) + "<?xml version='1.0'?>");
WriteToXMLOutput (BuildSubscriptionsHead);
# loop all the subscriptions
foreach($subscription in $subscriptions)
{
$subscriptionName = $subscription.SubscriptionName;
Select-AzureSubscription -SubscriptionName $subscriptionName;
WriteToXMLOutput (BuildSubscriptionHead $subscriptionName);
$vms = Get-AzureVM;
if($vms.Count -gt 0)
{
WriteToXMLOutput (BuildVirtualMachinesHead);
foreach($vm in $vms)
{
WriteToXMLOutput (BuildVirtualMachineHead $vm.Name $vm.VM.OSVirtualHardDisk.OS $vm.VM.RoleSize);
# build disk output
WriteToXMLOutput ((BuildLevelSpace 5) + "<OSDisk>" + $vm.VM.OSVirtualHardDisk.MediaLink.AbsoluteUri + "</OSDisk>");
if($vm.VM.DataVirtualHardDisks.Count -gt 0)
{
WriteToXMLOutput ((BuildLevelSpace 5) + "<DataDisks>");
foreach($DataDisk in $vm.VM.DataVirtualHardDisks)
{
$size = $DataDisk.LogicalDiskSizeInGB;
$lun = $DataDisk.Lun;
$uri = $DataDisk.MediaLink.AbsoluteUri;
WriteToXMLOutput ((BuildLevelSpace 6) + "<DataDisk lun='$lun' size='$size'>$uri</DataDisk>");
}
WriteToXMLOutput ((BuildLevelSpace 5) + "</DataDisks>");
}
WriteToXMLOutput (BuildVirtualMachineEnd);
}
WriteToXMLOutput (BuildVirtualMachinesEnd);
}
WriteToXMLOutput (BuildSubscriptionEnd $subscriptionName);
}
WriteToXMLOutput (BuildSubscriptionsEnd);
}
BuildOutputXml
ARM版本:
$Cred = New-Object System.Management.Automation.PSCredential("XXXXXXXXX@XXXXXXXXX.partner.onmschina.cn",(ConvertTo-SecureString "XXXXXXXXX" -AsPlainText -Force));
$AzureRMCred = Get-Credential -Credential $Cred;
Login-AzureRmAccount -EnvironmentName AzureChinaCloud -Credential $AzureRMCred;
Function WriteToXMLOutput($str)
{
$OutputfilePath = $PSScriptRoot + "\VM_and_Disks.xml";
$str | Out-File $OutputfilePath -Append;
}
Function BuildLevelSpace($level)
{
$levelSpace = " ";
$result = "";
for ($i = 1; $i -lt $level; $i++)
{
$result += $levelSpace;
}
return $result;
}
Function BuildSubscriptionsHead()
{
return (BuildLevelSpace 1) + "<subscriptions>";
}
Function BuildSubscriptionsEnd()
{
return (BuildLevelSpace 1) + "</subscriptions>";
}
Function BuildSubscriptionHead($subscriptionName)
{
return (BuildLevelSpace 2) + "<subscription name='$subscriptionName'>";
}
Function BuildSubscriptionEnd($subscriptionName)
{
return (BuildLevelSpace 2) + "</subscription>";
}
Function BuildVirtualMachinesHead()
{
return (BuildLevelSpace 3) + "<virtualmachines>";
}
Function BuildVirtualMachinesEnd()
{
return (BuildLevelSpace 3) + "</virtualmachines>";
}
Function BuildVirtualMachineHead($virtualmachineName, $OSType, $Size, $Location)
{
return (BuildLevelSpace 4) + "<virtualmachine name='$virtualmachineName' OS='$OSType' size='$Size' location='$Location'>";
}
Function BuildVirtualMachineEnd()
{
return (BuildLevelSpace 4) + "</virtualmachine>";
}
Function BuildOutputXml()
{
$subscriptions = Get-AzureRmSubscription;
WriteToXMLOutput ((BuildLevelSpace 1) + "<?xml version='1.0'?>");
WriteToXMLOutput (BuildSubscriptionsHead);
# loop all the subscriptions
foreach($subscription in $subscriptions)
{
$subscriptionName = $subscription.SubscriptionName;
Select-AzureRmSubscription -SubscriptionName $subscriptionName;
WriteToXMLOutput (BuildSubscriptionHead $subscriptionName);
$vms = Get-AzureRmVM;
if($vms.Count -gt 0)
{
WriteToXMLOutput (BuildVirtualMachinesHead);
foreach($vm in $vms)
{
WriteToXMLOutput (BuildVirtualMachineHead $vm.Name $vm.StorageProfile.OsDisk.OsType $vm.HardwareProfile.VmSize $vm.Location);
# build disk output
WriteToXMLOutput ((BuildLevelSpace 5) + "<OSDisk>" + $vm.StorageProfile.OsDisk.Vhd.Uri + "</OSDisk>");
if($vm.StorageProfile.DataDisks.Count -gt 0)
{
WriteToXMLOutput ((BuildLevelSpace 5) + "<DataDisks>");
foreach($DataDisk in $vm.StorageProfile.DataDisks)
{
$size = $DataDisk.DiskSizeGB;
$lun = $DataDisk.Lun;
$uri = $DataDisk.Vhd.Uri;
WriteToXMLOutput ((BuildLevelSpace 6) + "<DataDisk lun='$lun' size='$size'>$uri</DataDisk>");
}
WriteToXMLOutput ((BuildLevelSpace 5) + "</DataDisks>");
}
WriteToXMLOutput (BuildVirtualMachineEnd);
}
WriteToXMLOutput (BuildVirtualMachinesEnd);
}
WriteToXMLOutput (BuildSubscriptionEnd $subscriptionName);
}
WriteToXMLOutput (BuildSubscriptionsEnd);
}
BuildOutputXml
输出示例: