PowerShell – Convert Base 64 to Bytes

$Bytes=[Convert]::FromBase64String($String)

[IO.File]::WriteAllBytes($Filename, $bytes)

Advertisements

PowerShell – HTTP Authentication

#Authentication Parameters
#Credential used after initial communication
$Credentials=Get-Credential

#Building Authorisation Header for initial communication not used afterwards
$Base64Auth=[System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($Credentials.GetNetworkCredential().username + ":" + $Credentials.GetNetworkCredential().password ))
$BasicCredentials = "Basic " + $Base64Auth

$headers = @{"tenant-code" = $tenantAPIKey; "Authorization"= $BasicCredentials}

$Output = Invoke-RestMethod -Method Post -Credential $Credentials -Uri $URI -Body $Body -ContentType $ContentType -Headers $headers 

PowerShell – Non latin language with REST

#HTTP Headers building
$ContentType= 'application/json; charset=utf-8'
$headers = @{"tenant-code" = $tenantAPIKey; "Authorization"= $BasicCredentials}  

$Message = @{
  MessageBody = "Привет от Камиллы"
  MessageType = "String"
}

#Convert to JSON Depth at 10 ensure correct convertion for long text
$MessageInJSON = $Message | ConvertTo-Json -Depth 10 

#Convert Message to UTF-8 encoding
$Body = [System.Text.Encoding]::UTf8.GetBytes($MessageInJSON)

#Sending Message using Rest
$Output = Invoke-RestMethod -Method Post -Credential $Credentials -Uri $URI -Body $Body -ContentType $ContentType -Headers $headers