Hi @Nares ,
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:
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:
Container Screenshot:
Hi @Nares ,
Please give more context on what errors you are getting. If it's related to not being able to send the csv file to blob storage then it's probably because you are not explicitly exporting the csv to temp folder as recommended here in the official Azure document. So, I would recommend you to export the csv file to $env:TEMP i.e., something like ($env:TEMP+"AutomationFile123.csv") and then use the file from $env:TEMP path in the same way while mentioning it in Set-AzureStorageBlobContent cmdlet.
Thanks for the response. Basically, my issue is the CSV file could not be able to capture the VM details mentioned in the script, when I run the script from Azure automation runbook. (Script can place the empty CSV file in blob storage)
But whereas, same script I can run from Azure cloud shell (PowerShell) which will capture the VM details and can export to the blob storage.
so basically, I need suggestion to get Azure VMs details (like CMDB information) in CSV format and export it to Azure blob storage using PowerShell script, and this PowerShell script should run from Azure Automation Runbook.