title: converting a variable to a byte without using Get-content
body: <p>I need to convert the $var to a byte array to push it to <code>[convert]::ToBase64String($var)</code> for upload to a sql DB.  All without using Get-Content -file -encoding Byte, as this script is running in Azure without access to any filesystem. </p>
  
  <p>Header contains Auuth bearer token.</p>
  
  <pre><code>$var = (invoke-restmethod -uri ...800x800.png -method -get -headers $head)
  </code></pre>
  
  <p>Data stream looks like this:</p>
  
  <p>PNG</p>
  
  <p>IHDR         Ûph   gAMA  ±üa    cHRM  z&amp;    ú   è  u0  ê`  :  pºQ&lt;   bKGD ÿ ÿ ÿ ½§   tIMEã
       5Þ   IDATxÚìý[°e[}ß?Æ\kgÌçä¹:UÝÕ¥Ôh!]@Rè ÆÌÕ°ÍÃ?øÁðà'¿áðG¶#ÆÆ1!ä ²
  ...</p>
  
  <pre><code> [System.Array]$myarray = (invoke-restmethod...
   $bytes = [System.Text.Encoding]::UTF8.GetBytes($myarray)
   $encoded_array = [System.Convert]::ToBase64String($bytes)
  </code></pre>
  
  <p>Upon decoding of the base64 string, it is sensed as a application/octet instead of an image octet and is broken. </p>
  
  <p>However, running the usual </p>
  
  <pre><code>invoke-restmethod ... ... -outfile c:\test.png
  [System.Convert]::ToBase64String (get-content c\test.png -encoding byte)
  </code></pre>
  
  <p>works, as it should.   </p>
  
  <p>To reproduce:</p>
  
  <pre><code>(invoke-restmethod -uri "URI to .png" -method -get )
  </code></pre>
  
  <p>then play with it till you can get it into an encoded base 64 string.</p>
  
  <p>Wanted result is a base64 string that is the image.<br>
  Current result is a base 64 string that is ???  and isn't decoded into anything useful.</p>