String problem in Powershell

I am trying to parameterize a powershell script. The only problem is, for some reason, when I call the invoke-restmode method with -uri $ url, it seems to choke. Actually everywhere I try to use it, it suffocates. I believe that maybe I will try to do it wrong. Is there a better or more direct way to parameterize 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 received errors

Invoke-RestMethod: Parse error. Expected: *, received w. Path 'ApplicationName', line 1, position 31. (error code: c2d09f7a-f31a-4db1-a448-8214b6ab65ed) In C: \ inetpub \ wwwroot \ WebIZ_Shane \ CustomerSQLScripts \ Powershell \ 20150522_Scheduled_Jobs_API_Calls. 10 charts1 = 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

+3


source to share


1 answer


Have you tried wrapping your JSON values ​​in single Qoutes as well as keys?

For example - placing single quotes around $ applicationname and $ rooturl when creating $ jsonHeader?

Example



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

Cause

I say that because your error indicates that the web service is getting "w" instead of another expected value for ApplicationName. Which is looking at the values ​​your source code generates for the JSON header, you can clearly see that / w is the first unquoted character in the string (from / webiznet_dev) and is most likely breaking your web service.
 {{'UserId' :, 'ApplicationName': / webiznet_dev , 'RootUrl': http: // c3po: 140 / webiznet_dev , 'ApplicationInsightsGUID': '8773f299-9fed- 4431-ab34-888888888888', 'DisableEmail': 'true '}, token}

+1


source







All Articles