need help with formatting and export output to csv

Hi guys,

below script will get the boot diag storage account details VMs provided in the csv input, however when I export the result it only gets the last storage account details in the csv

 

$csv = Import-Csv 'C:\diag.csv'
$result=foreach ($obj in $csv) {

$vm=Get-AzureRmVM –Name $obj.Name –ResourceGroupName $obj.ResourceGroup 
$azResourceParams = @{
'ResourceName' = $obj.Name
'ResourceType' = 'Microsoft.Compute/virtualMachines'
    'ResourceGroupName' = $obj.ResourceGroup
}
$vmResource = Get-AzureRmResource @azResourceParams
$vmId = $vmResource.Properties.VmId

$diagSa = [regex]::match($vm.DiagnosticsProfile.BootDiagnostics.StorageUri, '^http[s]?://(.+?)\.').groups[1].value
$diagContainerName = ('bootdiagnostics-{0}-{1}' -f $vm.Name.ToLower().Substring(0, 9), $vmId)
$diagSaRg = (Get-AzureRmStorageAccount | where { $_.StorageAccountName -eq $diagSa }).ResourceGroupName
$saParams = @{
'ResourceGroupName' = $diagSaRg
'Name' = $diagSa

}            

$diagSa.Length
if ($diagSa.Length -ne 0)
{

$results+= Get-AzureRmStorageAccount @saParams | Get-AzureStorageContainer | Select-Object -Property Name, @{L = 'VM'; E = {$obj.Name}},@{L = 'stg'; E = {$diagSa}} 

}
else
{


Write-Host couldnt find boot diag stg account for $obj.Name



}

}

$results+= $csvboot

$csvboot | Export-Csv C:\bootdiag.csv

 

also how can I export below script output into excel/csv ?

THank you!!

else

{


Write-Host couldnt find boot diag stg account for $obj.Name



}

$CSVBoot is not getting set anywhere and Why would you need the output messages to be written in a CSV file.

Hi Prasoon,

I tried $results| export it didnt work either.

the reason why I want to export the output msg in csv/txt file so that I know which vms are not configured with boot diag stg accounts…

define $results as an array before using it.

$results = @()