Out .csv and overwrite the existing file RRS feed

  • Question

  • Hi There, just seeking some guidance on a issue,

    1) ive created a script to out the state of running machines virtual machines in azure,

    2) this then pipes out the contents to a .csv file

    however i would like to the script to run and overwrite the existing file, however the switch i have researched don't seem to work,

    for example Export-csv -Force

    the script is below:

    Retrieves stats from Azure of VM's and uploads a CSV file with data. Data is consumed in PowerBI
    function Get-VMStatus {
    $VM | % {
        $Status = (Get-AzureRMVM -Name $_.Name -ResourceGroupName $_.ResourceGroupName -Status).Statuses[1].DisplayStatus
        $output = @{
                Name = $_.Name
                Status = $Status
                ResourceGroup = $_.ResourceGroupName
         $returnObj = New-Object -TypeName PSObject -Property $output
         Write-Output $returnObj
    #Setup Connection
    $connectionName = "AzureRunAsConnection"
    $servicePrincipalConnection = Get-AutomationConnection -Name $connectionName         
        "Logging in to Azure..."
        Add-AzureRmAccount `
            -ServicePrincipal `
            -TenantId $servicePrincipalConnection.TenantId `
            -ApplicationId $servicePrincipalConnection.ApplicationId `
            -CertificateThumbprint   $servicePrincipalConnection.CertificateThumbprint 
    #Get VMs
    $VM = Get-AzureRMVM | ? {$_.Name -like 'dcs*'}
    #write file locally
    $filename = "ServerCheckOutput"
    ($StatusObj = Get-VMStatus) | Export-csv -NoTypeInformation -Force -Path "$($filename).csv"
    #Setup Storage account and upload
    $StorageAccountName = "xxxxxxxxxxxxxxxxx"
    $StorageAccountKey = "xxxxxxxxxxxxxxxw=="
    $Ctx = New-AzureStorageContext -StorageAccountName $StorageAccountName -StorageAccountKey $StorageAccountKey
    Set-AzureStorageBlobContent -File "$($filename).csv" -Container "xxxxxx" -Context $ctx 

    and the Error i'm receiving in azure is below:

    Any ideas or approach how to solve this?.

    much appreciated 

    Logging in to Azure...
    Environments                                                                                           Context          
    ------------                                                                                           -------          
    {[AzureCloud, AzureCloud], [AzureChinaCloud, AzureChinaCloud], [AzureUSGovernment, AzureUSGovernment]} Microsoft.Azur...
    Set-AzureStorageBlobContent : One or more errors occurred.
    At line:48 char:1
    + Set-AzureStorageBlobContent -File "$($filename).csv" -Container "dcsv ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : CloseError: (:) [Set-AzureStorageBlobContent], AggregateException
        + FullyQualifiedErrorId : AggregateException,Microsoft.WindowsAzure.Commands.Storage.Blob.SetAzureBlobContentCommand


    • Edited by BenAhm Thursday, August 17, 2017 12:22 PM
    Thursday, August 17, 2017 12:21 PM

All replies

  • This needs to be fixed:

    ($StatusObj = Get-VMStatus) | Export-csv -NoTypeInformation -Force -Path "$($filename).csv"

    to this

    Get-VMStatus | Export-csv -NoTypeInformation -Force -Path "$filename.csv"

    Export-Csv ALWAYS overwrites the file unless "Append" is specified.

    You issue is that you are outputting nothing until the export line is corrected.


    Thursday, August 17, 2017 12:29 PM