title: Powershell script for AzCopy with settings file
body: <p>I want to automate download from an Azure VM to my local machine using AzCopy. Manually, I did not get any issue whatsoever. I even succeeded to do an automic script to do that. However, now my script doesn't work and used to work... Maybe I missed something or maybe I got lucky. </p>
  
  <p>So here's what I have: 
  settings.txt that contains 3 settings, such as the output folder, source (azure storage url) and the sourcekey from Azure storage.
  myscript.ps1 that basically get the content from the settings.txt and start AzCopy</p>
  
  <p>My issue is on the sourcekey from Azure, if the key is not directly in myscript.ps1, it doesn't work and I get an error. </p>
  
  <p>My script that works: </p>
  
  <pre><code>    Get-Content "C:\ProgramData\NAME\SOFTWARE\Azure\settings\settings.txt" | foreach-object -begin {$h=@{}} -process { $k = [regex]::split($_,'='); if(($k[0].CompareTo("") -ne 0) -and ($k[0].StartsWith("[") -ne $True)) { $h.Add($k[0], $k[1]) } }
  
  $filename="LOG_" + (Get-Date -Format "yyyy-MM-dd-HH-mm-ss") + ".log"
  $log_path="C:\ProgramData\NAME\SOFTWARE\Azure\logs"
  $Source=$h.Source
  $Key="BLABLABLAMYWEIRDKEY"
  $output_path = $h.Output
  
  cd "C:\Program Files (x86)\Microsoft SDKs\Azure\AzCopy"
  
  .\AzCopy.exe /Source:$Source /Dest:$output_path /SourceKey:$Key /Y /S /V /XO /XN &gt;$log_path\$filename
  </code></pre>
  
  <p>This one works with this settings.txt: </p>
  
  <pre><code>[SETTINGS]
  Output="C:\Output\DATA\"
  Source="https://blablabla.file.core.windows.net/blabla/"
  Key="BLABLABLAMYWEIRDKEY"
  </code></pre>
  
  <p>So I tried to do that:</p>
  
  <pre><code>    Get-Content "C:\ProgramData\NAME\SOFTWARE\Azure\settings\settings.txt" | foreach-object -begin {$h=@{}} -process { $k = [regex]::split($_,'='); if(($k[0].CompareTo("") -ne 0) -and ($k[0].StartsWith("[") -ne $True)) { $h.Add($k[0], $k[1]) } }
  
  $filename="LOG_" + (Get-Date -Format "yyyy-MM-dd-HH-mm-ss") + ".log"
  $log_path="C:\ProgramData\NAME\SOFTWARE\Azure\logs"
  $Source=$h.Source
  $Key=$h.Key
  $output_path = $h.Output
  
  cd "C:\Program Files (x86)\Microsoft SDKs\Azure\AzCopy"
  
  .\AzCopy.exe /Source:$Source /Dest:$output_path /SourceKey:$Key /Y /S /V /XO /XN &gt;$log_path\$filename
  </code></pre>
  
  <p>but i got this error: </p>
  
  <pre><code>.\AzCopy.exe : [2018/06/08 17:10:09][ERROR] The syntax of the command is incorrect. The supplied storage key (SourceKey) is not a valid Base64 string.
  At F:\Desktop\Azure_DL.ps1:29 char:1
  + .\AzCopy.exe /Source:$Source /Dest:$output_path /SourceKey:$Key /Y /S ...
  + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo          : NotSpecified: ([2018/06/08 17:... Base64 string.:String) [], RemoteException
      + FullyQualifiedErrorId : NativeCommandError
  </code></pre>
  
  <p>Any idea ? I really need to have the key information store in a txt file.</p>