0

Trying to use the following Powershell script which I've taken from Github to push data into a Ducksboard dashboard. The function works excellently however I need to feed in a variable as part of the data. Of the two calls to the function included below the function which pushes in the actual value 44 works fine, however if I substitute it for my variable $qtybord the function falls over. I've tried a number of options to overcome the var being within the single quotes but cannot get it to work - can anyone help me?

# Squirt data to Duscksboard
    function Execute-DucksboardApi
    {
      param(
        [string] $url = $null,
        [string] $data = $null,
        [string] $apikey = $null,
        [string] $contentType = "application/json",
        [string] $codePageName = "UTF-8",
        [string] $userAgent = $null
      );

      if ($url -and $data -and $apikey)
      {
        [System.Net.WebRequest]$webRequest = [System.Net.WebRequest]::Create($url);
        $webRequest.ServicePoint.Expect100Continue = $false;
        [System.Net.NetworkCredential]$credentials = New-Object System.Net.NetworkCredential($apikey, 'ignored');
        $webRequest.Credentials = $credentials.GetCredential($url, 'Basic');
        $webRequest.PreAuthenticate = $true;
        $webRequest.ContentType = $contentType;
        $webRequest.Method = "POST";
        if ( $userAgent )
        {
          $webRequest.UserAgent = $userAgent;
        }

        $enc = [System.Text.Encoding]::GetEncoding($codePageName);
        [byte[]]$bytes = $enc.GetBytes($data);
        $webRequest.ContentLength = $bytes.Length;
        [System.IO.Stream]$reqStream = $webRequest.GetRequestStream();
        $reqStream.Write($bytes, 0, $bytes.Length);
        $reqStream.Flush();

        $resp = $webRequest.GetResponse();
        $rs = $resp.GetResponseStream();
        [System.IO.StreamReader]$sr = New-Object System.IO.StreamReader -argumentList $rs;
        $sr.ReadToEnd();
      }
    }

    $qtybord = 44

    Execute-DucksboardApi -url 'https://push.ducksboard.com/v/123752/' -data '{"value": $qtybord}' -apikey 'tu2j3d3epqytWZD1haHnjJSJ1NqBrmvPe5SONc0VYge4BbIPi0'

Execute-DucksboardApi -url 'https://push.ducksboard.com/v/123752/' -data '{"value": 44}' -apikey 'tu2j3d3epqytWZD1haHnjJSJ1NqBrmvPe5SONc0VYge4BbIPi0'

*

0

2 Answers 2

0

try this:

-data "{`"value`": $qtybord}"

or

-data "{""value"": $qtybord}"

in single quote variable aren't expanded, in double quote you need to escape the double quote inside the string.

0
0

So make the -data parameter take $data instead and make $data = '{"value": ' + $qtyboard + '}' or just wrap that whole thing in parens after -data.

Also, if you're on PowerShell v3 you can play with something like this:

$data = New-Object -Type PSObject -Property @{
   value = $qtyboard
} | ConvertTo-JSON
1
  • Hi Chris, thanks for the response however $data = '{"value": ' + $qtybord + '}' still fails with errors.
    – Gebraffe
    Mar 20, 2013 at 16:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.