3

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
7
  • Maybe the REST methods are too long to execute, so your Powershell client waits for REST API return. Commented Jul 22, 2015 at 14:59
  • @Vesper The error I am getting sounds more like it doesn't like what I have put into the uri Commented Jul 22, 2015 at 15:00
  • Debug the script and break. Dump the $url parameter and see what the string actually is. Commented Jul 22, 2015 at 15:08
  • 1
    I'm not a PowerShell expert, but I think it is considered more idiomatic to use interpolated strings: $fullApplicationPath = "{0}//{1}/{2}" -f $protocol, $server, $applicationPath; becomes: $fullApplicationPath = "$protocol//$server/$applicationPath"; The double quotes are important here, single quoted strings won't expand variables. Commented Jul 22, 2015 at 15:09
  • @Mark No, the string is expected to form correctly. Just copypasted OP's code in Powershell, and $url is formed properly. While yes, this way the strings look more pretty. Commented Jul 22, 2015 at 15:15

1 Answer 1

1

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 }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.