need help with formatting and export output to csv

Welcome Forums General PowerShell Q&A need help with formatting and export output to csv

This topic contains 3 replies, has 2 voices, and was last updated by

 
Participant
3 weeks, 6 days ago.

  • Author
    Posts
  • #124911

    Participant
    Points: 76
    Rank: Member

    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?://(.+?)\.').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
    
    
    
    }
  • #124916

    Participant
    Points: 832
    Helping Hand
    Rank: Major Contributor

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

    • #124917

      Participant
      Points: 76
      Rank: Member

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

  • #124938

    Participant
    Points: 832
    Helping Hand
    Rank: Major Contributor

    define $results as an array before using it.

    $results = @()

You must be logged in to reply to this topic.