我正在尝试参数化一个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 : 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
1条答案
按热度按时间mrfwxfqh1#
你有没有尝试过将JSON值 Package 在单个qoutes和键中?
例如,在构建$jsonHeader时,在$applicationname和$rooturl周围放置单引号?
示例
原因
我这么说是因为你的错误表明Web服务正在接收'w'而不是ApplicationName的另一个预期值。看看你的原始代码为JSON头生成的值,你可以清楚地看到/w是字符串中的第一个未加引号的字符(来自/webiznet_dev),并且很可能会破坏你的Web服务。