DeadlyChambers
DeadlyChambers

Reputation: 5275

String Building Issue in Powershell

I am trying to parameterize a powershell script. The only issue is that for some reason when I am calling the invoke-restmethod with a -uri of $url it seems to be choking. In fact everywhere I am trying to use this it is choking. I believe I might be trying to do this incorrectly. Is there a better, or more straight forward way to accomplish the parameterization of this script.

 #Variables that will probably need to change depeneding on environment
$server = "c3po:140"
$applicationName = "/webiznet_dev"
$applicationPath = "webiz_serviceapi"
$protocol = "http:"

#Variables that probably won't need to change
$userName = "PowerShellUser"
$auth = "token "
$rootUrl = '{0}//{1}{2}' -f $protocol, $server, $applicationName
$userId = 0

#Decrypting PWord 
#Might need to change $PSScriptRoot to where you have the txt file 
$securePassword = Get-Content "$PSScriptRoot\password.txt" | ConvertTo-SecureString
$BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($securePassword)
$password = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)
$login = @{"Username"=$userName; "Password"=$password; }

#Login and Get Token 
$fullApplicationPath = "{0}//{1}/{2}" -f $protocol, $server, $applicationPath
$url = "{0}/job/login" -f $fullApplicationPath
$jsonHeaders = "{'UserId':$userId,'ApplicationName':$applicationName,'RootUrl':$rootUrl,'ApplicationInsightsGUID':'8773f299-9fed-4431-ab34-888888888888','DisableEmail':'true'}";
$HeaderWrap = @{"Authorization"=$auth};
$HeaderWrap.Add("x-webiz-app-info",$jsonHeaders);
$token = Invoke-RestMethod -Uri $url -Method Post -Body $login -Headers $HeaderWrap;


#Write out url which works fine
 Write-Host $url

#Load values for api calls as the token is now populated so the appInfo can get properly populated
$encryptedString = $token.EncryptedString
$userId = $token.UserId
$auth = "token $encryptedString"
$jsonHeaders = "{'UserId':$userId,'ApplicationName':$applicationName,'RootUrl':$rootUrl,'ApplicationInsightsGUID':'8773f299-9fed-4431-ab34-888888888888','DisableEmail':'true'}";
$HeaderWrap = @{"Authorization"=$auth};
$HeaderWrap.Add("x-webiz-app-info",$jsonHeaders);

#Scripts to call scheduled notification jobs in 
$url = '{0}/Job/RunNotificationReminders' -f $fullApplicationPath
Invoke-RestMethod  $url -Method Post -Headers $HeaderWrap
$url = '{0}/Job/RunAddressChanges' -f $fullApplicationPath
Invoke-RestMethod $url -Method Post -Headers $HeaderWrap;
$url = '{0}/Job/RunStorageUnitTemperatureReadingDueAlerts' -f $fullApplicationPath
Invoke-RestMethod $url -Method Post -Headers $HeaderWrap;
$url = '{0}/Job/RunThermometerCalibrationDueAlerts' -f $fullApplicationPath
Invoke-RestMethod $url -Method Post -Headers $HeaderWrap;

One of the errors received:

Invoke-RestMethod : Error parsing comment. Expected: *, got w. Path 'ApplicationName', line 1, position 31. (error code: c2d09f7a-f31a-4db1-a448-8214b6ab65ed)
At C:\inetpub\wwwroot\WebIZ_Shane\CustomerSQLScripts\Powershell\20150522_Scheduled_Jobs_API_Calls.ps1:25 char:10
+ $token = Invoke-RestMethod -Uri $url -Method Post -Body $login -Headers $HeaderW ...
+          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

Upvotes: 3

Views: 476

Answers (1)

Jonathan H
Jonathan H

Reputation: 388

Have you tried wrapping your JSON values in single qoutes as well as the keys?

For example - placing single-quotes around the $applicationname and $rooturl when you build the $jsonHeader?

Example

$jsonHeaders = "{'UserId':$userId,'ApplicationName':'$applicationName','RootUrl':'$rootUrl','ApplicationInsightsGUID':'8773f299-9fed-4431-ab34-888888888888','DisableEmail':'true'}";

Reason

I say that because your error is indicating the web service is receiving 'w' instead of another expected value for ApplicationName. Which looking at the values your original code generates for the JSON header, you can clearly see /w is the first unquoted character in the string (from /webiznet_dev), and most likely breaking your webservice.

{{'UserId':,**'ApplicationName':/webiznet_dev**,'RootUrl':http://c3po:140/webiznet_dev,'ApplicationInsightsGUID':'8773f299-9fed-
                 4431-ab34-888888888888','DisableEmail':'true'}, token }

Upvotes: 1

Related Questions