We have a function, that helps post back to an application we run when called. This works perfectly when a client is running PowerShell v3.0 or higher, however we have a hybrid estate and there is no feasibility to upgrade PowerShell just now. I have seen you can use .NET to post xml in the same way, however i cannot get this to work.
Here is the version that works:
function SetTaskRealTimeStatus()
{
param($pRealTimeStatusId, $pResourceGuid, $pNewStatus)
$url = 'http://server001/myappname/api/resource/';
$url = $url + $pResourceGuid + '/task/' + $realTimeStatusId + '/status/'+ $pNewStatus;
Write-Host ("Sending request: $url") ;
# Returns Guid of task history record that is created. Empty guid means db call failed
# Invalid inputs will return BadRequest response
Invoke-RestMethod -Uri $URL -Method Post
}
Here is my version that does not work:
function SetTaskRealTimeStatus()
{
param($pRealTimeStatusId, $pResourceGuid, $pNewStatus)
$url = 'http://server001/myappname/api/resource/';
$urlCombined = $url + $pResourceGuid + '/task/' + $pRealTimeStatusId + '/status/'+ $pNewStatus;
Write-Host ("Sending request: $urlCombined") ;
# Returns Guid of task history record that is created. Empty guid means db call failed
# Invalid inputs will return BadRequest response
#Used for PowerShell version 2.0
$WebRequest = [System.Net.WebRequest]::Create("$urlCombined")
$WebRequest.Method = "POST"
$WebRequest.ContentType = "application/xml"
$WebRequest.UseDefaultCredentials = $true
$Response = $WebRequest.GetResponse()
$ResponseStream = $Response.GetResponseStream()
$ReadStream = New-Object System.IO.StreamReader $ResponseStream
$Data=$ReadStream.ReadToEnd()
$Request.Timeout = 10000;
#Invoke-RestMethod -Uri $urlCombined -Method Post
}
The parameters past to the function work fine, as they work with the first version.
I managed to get the function working as required:
function SetTaskRealTimeStatus()
{
param($setID, $pResourceGuid, $pNewStatus)
$url = 'http://server001/myapplication/api/resource/';
$urlCombined = $url + $pResourceGuid + '/task/' + $setID + '/status/' + $pNewStatus;
Write-Host ("Sending request: $urlCombined") ;
# Returns Guid of task history record that is created. Empty guid means db call failed
# Invalid inputs will return BadRequest response
#Used for PowerShell version 2.0
$WebRequest = [System.Net.WebRequest]::Create("$urlCombined")
$body = "$pNewStatus"
$encodedContent = [System.Text.Encoding]::UTF8.GetBytes($body)
$WebRequest.Method = "POST"
$WebRequest.ContentType = "application/xml"
$WebRequest.UseDefaultCredentials = $true
$requestStream = $webRequest.GetRequestStream()
$requestStream.Write($encodedContent, 0, $encodedContent.length)
[System.Net.WebResponse] $response = $webRequest.GetResponse();
$response.close()
$requestStream.Close()
#used for PowerShell Version 3.0 and above
#Invoke-RestMethod -Uri $urlCombined -Method Post
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.