Here is a sample PowerShell script that can be used to generate a report on Azure virtual machine usage:
# Login to Azure account Connect-AzAccount # Select the subscription where the virtual machines are located $subscriptionId = (Get-AzSubscription | Out-GridView -Title "Select a Subscription" -PassThru).SubscriptionId Select-AzSubscription -SubscriptionId $subscriptionId # Get a list of all virtual machines in the subscription $vms = Get-AzVM # Initialize a table to store the report data $report = @() # Loop through each virtual machine and gather information foreach ($vm in $vms) { $row = [ordered]@{ "VM Name" = $vm.Name "OS Type" = $vm.StorageProfile.OsDisk.OperatingSystemType "Size" = $vm.HardwareProfile.VmSize "Power State" = $vm.PowerState "Resource Group" = $vm.ResourceGroupName "Location" = $vm.Location } $report += New-Object PSObject -Property $row } # Export the report to a CSV file $report | Export-Csv -Path "Azure VM Usage Report.csv" -NoTypeInformation # Display the report $report | Out-GridView -Title "Azure VM Usage Report"
This script uses the Azure PowerShell module to connect to your Azure account
and gather information about all virtual machines in the selected subscription.
It then creates a table to store the report data and loops through each virtual
machine to gather information such as the name, operating system type, size,
power state, resource group, and location. Finally, the report is exported to
a CSV file and displayed in a grid view. Note: This script assumes that you have already installed the Azure PowerShell module
and logged in to your Azure account. If you haven't already installed the module, you can do
so by running the following command in an elevated PowerShell window: Install-Module AzureRM.
Here's a another PowerShell script to get an Azure virtual machine usage report:
# Login to Azure Login-AzureRmAccount # Get all virtual machines in the subscription $vms = Get-AzureRmVM # Define an array to store the usage report data $usageReport = @() # Loop through each virtual machine foreach ($vm in $vms) { # Get the virtual machine details $vmName = $vm.Name $location = $vm.Location $size = $vm.HardwareProfile.VmSize $osType = $vm.StorageProfile.OsDisk.OperatingSystemType $created = $vm.Tags["Created"] # Add the virtual machine details to the usage report array $usageReport += [PSCustomObject]@{ "VM Name" = $vmName "Location" = $location "Size" = $size "OS Type" = $osType "Created" = $created } } # Export the usage report to a CSV file $usageReport | Export-Csv -Path "AzureVirtualMachineUsageReport.csv" -NoTypeInformation
This script logs in to Azure, retrieves a list of all virtual machines
in the subscription, and loops through each virtual machine to gather
the following information: VM Name Location Size OS Type Created (Note: "Created" is assumed to be a custom tag that was added to each virtual machine) Finally, the script exports the usage report data to a CSV file named "AzureVirtualMachineUsageReport.csv".
No comments:
Post a Comment