Powershell to export to csv file the VM Name, Vnet Name, Tags assigned to VM

$vm = Get-AzureRmVM
$op = @()
$vm | ForEach-Object{
$temp = New-Object -TypeName psobject

$temp | Add-Member -MemberType NoteProperty -Name "VMName" -Value $_.Name
$temp | Add-Member -MemberType NoteProperty -Name "OS Type" -Value $_.StorageProfile.OsDisk.OsType
$temp | Add-Member -MemberType NoteProperty -Name "OS Version" -Value $_.StorageProfile.ImageReference.Sku
$temp | Add-Member -MemberType NoteProperty -Name $key -Value $val

$op += $temp
}
$op | Export-Csv c:\\tags.csv

With the help of calculated properties,

$PropertyHash = @(
@{E={$_.Name};L='VMName'}
@{E={$_.StorageProfile.OsDisk.OsType};L='OS Type'}
@{E={$_.StorageProfile.ImageReference.Sku};L='OS Version'}
@{E={$Value};L=$key}
)
Get-AzureRmVM | Select-Object -Property $PropertyHash | Export-Csv c:\ags.csv -NoTypeInformation

How to export tags details along in this script? Can some1 plz help since i am new to PowerShell

kvprasoon
Thanks for your help. Anyway I got the tag values in a single. I like to get it in different columns.

Can some1 help me to get each tag value in different column?

Could you please explain a little more edetailed what you expect? You might post some example result data (formatted as code please).

My initial script i can able to pull vm name, os type, os version along with i need to pull tags that are assigned to VM.

output should be like 1st Column is vm name, 2nd column is os type, 3rd column is os version and from 4th ,5ht,6th cloumn it is should have tag key

All i need to export in csv file.

 

Do not explain it - show it!! (with some sanitized example data)

Maybe someone else here on the forum understand what exactly you expect - I don’t. Sorry. What tags what keys? Where do these data come from?

Install-Module AZSBTools,ImportExcel

$LoginName        = 'loginname@somedomain.com'
$SubscriptionName = 'Your subscription name here'

Report-AzureRMVM -LoginName $LoginName -SubscriptionName $SubscriptionName -OutputFile ".\$SubscriptionName-VMLReport.xlsx"
help Report-AzureRMVM -ShowWindow 
Synopsis
    Function to report on Azure VM population in a given Azure subscription

Description
    Function to report on Azure VM population in a given Azure subscription
    The report is saved to xlsx file
    This function uses ImportExcel PowerShell module available in the PowerShell gallery
    This function reports on Azure ARM VMs only (not classic ASM VMs)


Parameters
    -LoginName <String>
        The username required to authenticate to Azure
        Example: samb@mydomain.com

        Required?                    true
        Position?                    1
        Default value                
        Accept pipeline input?       false
        Accept wildcard characters?  false

    -SubscriptionName <String>
        The Azure subscription name such as 'My Dev EA subscription'

        Required?                    true
        Position?                    2
        Default value                
        Accept pipeline input?       false
        Accept wildcard characters?  false

    -OutputFile <String>
        Path to xlsx file, where the function will write its output

        Required?                    false
        Position?                    3
        Default value                ".\Report-AzureRMVM - $SubscriptionName - $(Get-Date -Format 'ddMMMMyyyy_hh-mm-ss_tt').xlsx"
        Accept pipeline input?       false
        Accept wildcard characters?  false

    -LogFile <String>
        This is an optional parameter that specifies the path to the log file where the script logs its progress
        This defaults to a file in the current folder where the script is running

        Required?                    false
        Position?                    4
        Default value                ".\Report-AzureRMVM - $SubscriptionName - $(Get-Date -Format 'ddMMMMyyyy_hh-mm-ss_tt').txt"
        Accept pipeline input?       false
        Accept wildcard characters?  false



Syntax
    Report-AzureRMVM [-LoginName] <String> [-SubscriptionName] <String> [[-OutputFile] <String>] [[-LogFile] <String>] [<CommonParameters>]


Outputs
    Array of PS Custom objects, one for each ARM VM found with the following properties/example:
      VMName          : AZ-abcBDEV-01
      ResourceGroup   : AZ-abcEV-RG
      Status          : VM running
      Subscription    : abc Enterprise Dev/Test
      Size            : Standard_D2s_v3
      Cores           : 2
      RAM(GB)         : 8
      HybridLicense   : False
      Location        : eastus
      MACAddress      : 00-0D-3A-1C-87-11
      IPv4Address     : 172.129.132.112
      AdminName       : cdabcadmin
      OperatingSystem : Windows
      OSDiskSize(GB)  : 127
      DataDisks       : (AZ-abcBDEV-01_SQLDATA, 1028 GB, LUN 0), (AZ-abcBDEV-01_SQLLOG, 1028 GB, LUN 1)

Notes
    Function by Sam Boutros
    v0.1 - 4  June 2018 - Initial release
    v0.2 - 14 June 2018 - Parameterized, added error handling and documentation
    v0.3 - 23 January 2019 - Added logfile parameter, 
              updated subscription login section, 
              added HybridLicense property to output
    v0.4 - 28 February 2019 - Added Status (running/deallocated)

Examples
    -------------------------- EXAMPLE 1 --------------------------
    PS C:\>Report-AzureRMVM -LoginName 'samb@mydomain.com' -SubscriptionName 'my azure subscription name here' -Verbose

Thank you @Sam, That perfectly helps to get all resource details from Azure portal includes Tags.

How should i found the hardcore script of the Modules “Report-Azurermvm” ? I want look up the entire powershell script for this module. Can you please help ?

I got your script as well @Sam. Thanks much.

@Olaf That’s Azure Resource Tags