I am running automation powershell runbook to get VM details in CSV file and export that CSV file to azure blob storage, but I am unable to capture the VM details through Azure automation powershell runbook

Naresh Babu 135 Reputation points
2023-03-15T02:18:18.2033333+00:00

//I am running this code to get the VM details in CSV file and store it to blob storage//this code working for me in Azure CLI powershell but getting errors in Azure automation powershell// can someone help me//thanks//

Connect-AzAccount -Identity
$subscriptionId = "mySubID"
$reportName = "myReport.csv"
Select-AzSubscription $subscriptionId
$report = @()
$vms = Get-AzVM
$publicIps = Get-AzPublicIpAddress 
$nics = Get-AzNetworkInterface | ?{ $_.VirtualMachine -NE $null} 
foreach ($nic in $nics) { 
    $info = "" | Select VmName, ResourceGroupName, Region, VmSize, VirtualNetwork, Subnet, PrivateIpAddress, OsType, PublicIPAddress, NicName, ApplicationSecurityGroup 
    $vm = $vms | ? -Property Id -eq $nic.VirtualMachine.id 
    foreach($publicIp in $publicIps) { 
        if($nic.IpConfigurations.id -eq $publicIp.ipconfiguration.Id) {
            $info.PublicIPAddress = $publicIp.ipaddress
            } 
        } 
        $info.OsType = $vm.StorageProfile.OsDisk.OsType 
        $info.VMName = $vm.Name 
        $info.ResourceGroupName = $vm.ResourceGroupName 
        $info.Region = $vm.Location 
        $info.VmSize = $vm.HardwareProfile.VmSize
        $info.VirtualNetwork = $nic.IpConfigurations.subnet.Id.Split("/")[-3] 
        $info.Subnet = $nic.IpConfigurations.subnet.Id.Split("/")[-1] 
        $info.PrivateIpAddress = $nic.IpConfigurations.PrivateIpAddress 
        $info.NicName = $nic.Name 
        $info.ApplicationSecurityGroup = $nic.IpConfigurations.ApplicationSecurityGroups.Id 
        $report+=$info 
    } 
$report | ft VmName, ResourceGroupName, Region, VmSize, VirtualNetwork, Subnet, PrivateIpAddress, OsType, PublicIPAddress, NicName, ApplicationSecurityGroup 
$report | Export-CSV "AutomationFile123.csv"
$Context = New-AzureStorageContext -StorageAccountName "storagename" -StorageAccountKey "storagekey"
Set-AzureStorageBlobContent -Context $Context -Container "conttainername" -File "AutomationFile123.csv" -Blob "AutomationFile123.csv"
Azure Automation
Azure Automation
An Azure service that is used to automate, configure, and install updates across hybrid environments.
1,133 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,102 questions
{count} votes

2 answers

Sort by: Most helpful
  1. tbgangav-MSFT 10,386 Reputation points
    2023-03-16T13:31:54.79+00:00

    Hi @Naresh Babu ,

    Please go through below recommendations.

    • As you mentioned that "issue is the CSV file could not be able to capture the VM details mentioned in the script", so as provided in below Script, to troubleshoot that part please use Get-Content command line after exporting data to csv to understand if csv file is really not able to capture the VM details by counting the number of rows in the csv file and cross verifying that count with count of report variable. This also helps us understand if report variable is able to capture the VM details or not.
    • As Az module is recommended over AzureRM module so I have replaced New-AzureStorageContext with New-AzStorageContext and Set-AzureStorageBlobContent with Set-AzStorageBlobContent cmdlet. Hence, I would also recommend you to import Az.Storage module and use the related Az cmdlets.
    • If you were getting the error show in below Error Screenshot, it's because AutomationFile123.csv file was already in the blob storage. To overwrite, use "Force" parameter in Set-AzStorageBlobContent command line.
    • As recommended here, export the csv file to $env:TEMP folder.
    • Script that is shared below has worked for me but if it doesn't work for you then I would recommend you to check version of the cmdlets used in below Output Screenshot to compare and see if issue is related to module versions.

    Error Screenshot:User's image

    Script:

    Connect-AzAccount -Identity
    $subscriptionId = "mySubID"
    $reportName = "myReport.csv"
    Select-AzSubscription $subscriptionId
    $report = @()
    $vms = Get-AzVM
    $publicIps = Get-AzPublicIpAddress 
    $nics = Get-AzNetworkInterface | ?{ $_.VirtualMachine -NE $null} 
    foreach ($nic in $nics) { 
        $info = "" | Select VmName, ResourceGroupName, Region, VmSize, VirtualNetwork, Subnet, PrivateIpAddress, OsType, PublicIPAddress, NicName, ApplicationSecurityGroup 
        $vm = $vms | ? -Property Id -eq $nic.VirtualMachine.id 
        foreach($publicIp in $publicIps) { 
            if($nic.IpConfigurations.id -eq $publicIp.ipconfiguration.Id) {
                $info.PublicIPAddress = $publicIp.ipaddress
                } 
            } 
            $info.OsType = $vm.StorageProfile.OsDisk.OsType 
            $info.VMName = $vm.Name 
            $info.ResourceGroupName = $vm.ResourceGroupName 
            $info.Region = $vm.Location 
            $info.VmSize = $vm.HardwareProfile.VmSize
            $info.VirtualNetwork = $nic.IpConfigurations.subnet.Id.Split("/")[-3] 
            $info.Subnet = $nic.IpConfigurations.subnet.Id.Split("/")[-1] 
            $info.PrivateIpAddress = $nic.IpConfigurations.PrivateIpAddress 
            $info.NicName = $nic.Name 
            $info.ApplicationSecurityGroup = $nic.IpConfigurations.ApplicationSecurityGroups.Id 
            $report+=$info 
        } 
    $report | ft VmName, ResourceGroupName, Region, VmSize, VirtualNetwork, Subnet, PrivateIpAddress, OsType, PublicIPAddress, NicName, ApplicationSecurityGroup 
    $report | Export-CSV ($env:TEMP+"AutomationFile123.csv") -Notype
    
    "get version and source of cmdlets used in this script"
    Get-Command Connect-AzAccount, Select-AzSubscription, Get-AzVM, Get-AzPublicIpAddress, Get-AzNetworkInterface, Export-CSV, Get-Content, New-AzStorageContext, Set-AzStorageBlobContent | ft Name, Version, Source
    "get number of rows in report variable"
    $reportlength = $report.length
    $reportlength
    "get number of rows of AutomationFile123.csv"
    $GetContentLength = (Get-Content ($env:TEMP+"AutomationFile123.csv")).length
    $GetContentLength
    
    "upload AutomationFile123.csv file to storage account"
    $Context = New-AzStorageContext -StorageAccountName "storagename" -StorageAccountKey "storagekey"
    Set-AzStorageBlobContent -Context $Context -Container "container1" -File ($env:TEMP+"AutomationFile123.csv") -Blob "AutomationFile123.csv" -Force
    
    

    Output Screenshot:User's image

    Container Screenshot:User's image

    1 person found this answer helpful.

  2. Lucas Antonio Dourado Dos Santos 0 Reputation points
    2023-06-22T17:52:21.73+00:00

    I tried using the command to send to the blob, it doesn't return an error but it doesn't save either

    Connect-AzAccount -Identity
    $WarningPreference = 'SilentlyContinue'
    $ErrorActionPreference = 'SilentlyContinue'
    
    
    0 comments No comments