Set-AzStorageBlobContent deleting blob contents instead of saving new contents in file on container in Azure Runbooks?

DeHaven Graham 106 Reputation points
2022-06-13T15:52:40.233+00:00

Hi,
I'm attempting to run the below script in Runbooks for reformatting a csv file that works perfectly fine from my local machine but when I add the Set-AzStorageBlobContent in my script it saved the blob file but erases all the contents of the file instead of reformatting it? Any ideas?

$CsvFile = Get-AzStorageBlobContent -Blob 'StorageAccSettings.csv' -Container $ContainerName -Destination $r -Context $sa.Context -Force
$CsvFile

$old = Import-Csv -Path $CsvFile
$old

Create empty variable to add lines to

$new = @()

Iterate through each entry

$old | ForEach-Object {
$current = $_

Split the Problem and Impact fields by newlines

$splitProblem = $.Problem -Split "\n"
$splitSolution = $
.Solution -Split "\n"
$splitImpact = $_.Impact -Split "\n"

Iterate through each of the Problems

for (($i = 0); ($i -lt $splitProblem.Count - 1); ($i++)) {

Here we build a new object for each Problem and include all the other properties

$currentLine = $current | Select-Object -Property Name, ResourceGroup, Location, VmSize, OsType, Compliance
$currentLine | Add-Member -MemberType NoteProperty -Name Impact -Value $splitImpact[$i]
$currentLine | Add-Member -MemberType NoteProperty -Name Problem -Value $splitProblem[$i]
$currentLine | Add-Member -MemberType NoteProperty -Name Solution -Value $splitSolution[$i]

Add each entry to a new variable

$new += $currentLine

    }
}

$new | Export-Csv StorageAccSettings.csv
Set-AzStorageBlobContent -Context $sa.Context -Container $ContainerName -File StorageAccSettings.csv -Blob StorageAccSettings.csv

Azure Blob Storage
Azure Blob Storage
An Azure service that stores unstructured data in the cloud as blobs.
3,192 questions
Azure Automation
Azure Automation
An Azure service that is used to automate, configure, and install updates across hybrid environments.
1,366 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. AnuragSingh-MSFT 21,546 Reputation points Moderator
    2022-06-16T07:26:37.637+00:00

    Hi @DeHaven Graham

    Thank you for reaching out to Microsoft Q&A for your question.

    I reviewed the scripts and I have a few suggestions/findings that I wanted to share with you based on it:

    1. I think that the main reason for getting a blank file uplaoded to Azure Storage container is the inner for loop that you have:
    for (($i = 0); ($i -lt $splitProblem.Count - 1); ($i++)) {

    I tried adding a newline ("\n") to a column in csv file (by using Alt + Enter in Microsoft Excel) but it is getting treated as a linefeed( LF ) instead of newline ( CRLF ). When I do a split on one such property, the returned object is a string (and not an array) with count = 1

    Therefore, it looks like the inner for loop above has the condition set to 0 < 0 ( False ), causing the iteration to not even run once. Since the array $new is getting set in this loop, it always remains empty.

    I would suggest changing the conditional expression in this loop to either of the following and see if it works:

    $i -lt $splitProblem.Count OR $i -le $splitProblem.Count - 1

    Note that this suggestion is based on the csv file I generated for test. In case the csv file you have does contain a newline in a cell, I would suggest sharing a sample with your data filtered out here, so that I can test the script with that.

    2. I feel there is a typo in the script shared above where pipeline variable ( $_ ) is not used correctly when splitting Problem and Solution field (although not related to the issue, but wanted to point out)

    3. An efficient way to test the runbook in Azure Automation would be to use the write-output to print debug logs and using the Test pane in Azure portal when editing the runbook as shown below. This would help you get immediate feedback on the script run in Azure Automation along with the capability to edit it to solve the issue.

    211957-image.png

    Please let me know if you have any questions. In case the suggestions above don't help, please share a sample csv file (with dummy data) to help test the script further.

    ---
    Please 'Accept as answer' and ‘Upvote’ if it helped so that it can help others in the community looking for help on similar topics.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.