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:
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: