locked
csv export not working when stored in temp variable RRS feed

  • Question

  • Hi Guys,

    We are using below script to export data to temp table save it blog storage; but receive below error

      
    $storageAccountName ="XXXX"
    $storageAccountKey ="XXX"
    $context = New-AzureStorageContext -StorageAccountName $storageAccountName -StorageAccountKey $storageAccountKey

    $Output | Export-Csv  -path  $Env:temp+"/SavedFile.csv"

          #$Output  | Out-File -FilePath $Env:temp+"/SavedFile.csv" -Append
          $Env:temp

            Set-AzureStorageBlobContent -File "C:\Users\Client\Temp\SavedFile.csv" -Container "xxxx" -BlobType "Block" -Context $context  -Verbose

    ERROR:

    Performing the operation "Set" on target "".
    Set-AzureStorageBlobContent : Can not find the specified file 'C:\Users\Client\Temp\SavedFile.csv'.
    At line:69 char:9
    +         Set-AzureStorageBlobContent -File "C:\Users\Client\Temp\Saved ...
    +         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidArgument: (:) [Set-AzureStorageBlobContent], ArgumentException
        + FullyQualifiedErrorId : ArgumentException,Microsoft.WindowsAzure.Commands.Storage.Blob.SetAzureBlobContentCommand
     
    Transfer Summary
    --------------------------------
    Total:  0.
    Successful: 0.
    Failed: 0.

    Regards,

    Navin


    Navin.D http://dnavin.wordpress.com


    • Edited by Navind Sunday, April 26, 2020 8:08 PM
    Sunday, April 26, 2020 7:52 PM

Answers

  • Hi Navin,

    It failed because there was no "C:\Users\Client\Temp\SavedFile.csv" file at all because Export-Csv line didn't create a file as the path of the file is not enclosed within braces as shown below. It did try to create the file in "C:\Users\Client\Temp+" which actually didn't exist.



    To check, you may run the Export-Csv cmdlet along wth Verbose parameter as shown below.


    To double check, you may run the Export-Csv cmdlet in your local environment as shown below.


    So the runbook can be updated as shown below to resolve the error.


    Or you may even update the file path in Set-AzureStorageBlobContent line to ($Env:temp+"/SavedFile.csv") to avoid using hard-coded file path.


    Hope this helps! Cheers!

    Monday, April 27, 2020 2:58 AM
  • Hi Navind,

    You should use -Blob parameter along with the folder name. You may try the below command and let me know if it doesn't work.

    Set-AzureStorageBlobContent -File ($Env:temp+"\xyz" + (Get-Date -Format "yyyy-MM-dd")+".csv") -Container "TestInfo" -Blob ("FileStore"+"\xyz" + (Get-Date -Format "yyyy-MM-dd")+".csv") -BlobType "Block" -Context $context  -Force

    Friday, May 15, 2020 7:17 PM

All replies

  • Hi Navin,

    It failed because there was no "C:\Users\Client\Temp\SavedFile.csv" file at all because Export-Csv line didn't create a file as the path of the file is not enclosed within braces as shown below. It did try to create the file in "C:\Users\Client\Temp+" which actually didn't exist.



    To check, you may run the Export-Csv cmdlet along wth Verbose parameter as shown below.


    To double check, you may run the Export-Csv cmdlet in your local environment as shown below.


    So the runbook can be updated as shown below to resolve the error.


    Or you may even update the file path in Set-AzureStorageBlobContent line to ($Env:temp+"/SavedFile.csv") to avoid using hard-coded file path.


    Hope this helps! Cheers!

    Monday, April 27, 2020 2:58 AM
  • it works, but my output is as below,does not have actual data, any specific reason?

    #TYPE System.String
    "Length"
    "9"

    but by changing the export-csv to output, was able to view actual data

    $Output  | Out-File -FilePath ($Env:temp+"/SavedFile.csv") -Append


    Navin.D http://dnavin.wordpress.com


    • Edited by Navind Monday, April 27, 2020 1:30 PM
    Monday, April 27, 2020 1:18 PM
  • Hi Navin,

    Export-Csv exports a table of object properties and their values. Since your script is producing string objects, and the only property they have is length, that's what you got. If you just want to save the list, use Out-File or Set-Content instead of Export-Csv.
    This is the reference of the above comment. 
    Hope this helps! Cheers!
    Monday, April 27, 2020 5:06 PM
  • one last query, when i use below query it automatically saves on the root of blob, i want to save to an directory within it, is it possible?

      Set-AzureStorageBlobContent -File ($Env:temp+"\xyz" + (Get-Date -Format "yyyy-MM-dd")+".csv") -Container "TestInfo" -BlobType "Block" -Context $context  -Force

    Container : TestInfo

    Folder: FileStoe

    TestInfo/Filestore/xyz2020-05-15.csv


    Navin.D http://dnavin.wordpress.com


    • Edited by Navind Friday, May 15, 2020 4:48 PM
    Friday, May 15, 2020 4:48 PM
  • Hi Navind,

    You should use -Blob parameter along with the folder name. You may try the below command and let me know if it doesn't work.

    Set-AzureStorageBlobContent -File ($Env:temp+"\xyz" + (Get-Date -Format "yyyy-MM-dd")+".csv") -Container "TestInfo" -Blob ("FileStore"+"\xyz" + (Get-Date -Format "yyyy-MM-dd")+".csv") -BlobType "Block" -Context $context  -Force

    Friday, May 15, 2020 7:17 PM