我正在尝试参数化PowerShell脚本。唯一的问题是,由于某种原因,当我用$ url的-uri调用invoke-restmethod时,它似乎很窒息。事实上,无论我在哪里尝试使用它,都会让人感到窒息。我相信我可能会错误地尝试这样做。是否有更好或更直接的方法来完成此脚本的参数化。
#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;
收到的错误之一
Invoke-RestMethod:解析评论时出错。预期:*,得到了w。 Path' ApplicationName',第1行,第31位。(错误代码:c2d09f7a-f31a-4db1-a448-8214b6ab65ed) 在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
答案 0 :(得分:1)
您是否尝试将JSON值包装在单个qoutes和键中?
例如 - 在构建$ jsonHeader时在$ applicationname和$ rooturl周围放置单引号?
示例强>
$ jsonHeaders =“{'UserId':$ userId,'ApplicationName':'$ applicationName','RootUrl':'$ rootUrl','ApplicationInsightsGUID':'8773f299-9fed-4431-ab34-888888888888',' DisableEmail ':' 真'}“;
<强>原因强>
我这样说是因为您的错误表明Web服务正在接收'w'而不是ApplicationName的另一个预期值。在查看原始代码为JSON标头生成的值时,您可以清楚地看到/ w是字符串中的第一个未加引号的字符(来自/ webiznet_dev),并且很可能会破坏您的Web服务。
{{ '用户ID':,的 '应用程序名':/ webiznet_dev 下, '使用rootUrl':http://c3po:140/webiznet_dev, 'ApplicationInsightsGUID':'8773f299-9fed-
4431-ab34-888888888888','DisableEmail':'true'},令牌}