Hello,
I am trying to query a vCO workflow by name using the REST API from PowerShell based on this article:
http://www.vcoteam.info/articles/learn-vco/268-how-to-use-the-rest-api-to-start-a-workflow.html
$username = 'XXXXXX'
$upassword = 'XXXXXX'
$auth = $username + ':' + $upassword
$Encoded = [System.Text.Encoding]::UTF8.GetBytes($auth)
$EncodedPassword = [System.Convert]::ToBase64String($Encoded)
$headers = @{"Authorization"="Basic $($EncodedPassword)";}
$body = "<execution-context xmlns='http://www.vmware.com/vco'></execution-context>"
#query for workflow named "donothing"
Invoke-RestMethod -uri https://XXXX:8281/api/workflows/?conditions=name=donothing -Headers $headers -Body $body -ContentType "application/xml" -Method Get
Invoke-RestMethod : Cannot send a content-body with this verb-type.
At line:1 char:1
+ Invoke-RestMethod -uri https://XXXX:8281/api/workflows/?conditions=name=donoth ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Invoke-RestMethod], ProtocolViolationException
+ FullyQualifiedErrorId : System.Net.ProtocolViolationException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
The query succeeds using the REST client on Firefox, but I cannot get it to work on PowerShell. (NOTE: execution of workflows from PowerShell works fine)
Any help is appreciated.
Thanks,
Juan.
Ok, I got it to work by removing
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
And adding this at the beginning of the script
add-type @"
using System.Net;
using System.Security.Cryptography.X509Certificates;
public class TrustAllCertsPolicy : ICertificatePolicy {
public bool CheckValidationResult(
ServicePoint srvPoint, X509Certificate certificate,
WebRequest request, int certificateProblem) {
return true;
}
}
"@
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
Now I can use the GET method:
$ret = Invoke-WebRequest -uri $URL -Headers $headers -ContentType "application/xml" -Method Get
I switched to Invoke-WebRequest so I can obtain the Web request return.
Juan.
You don't need to send a body for this query -- remove the "-Body $body" from your Invoke-RestMethod line and let me know if that works.
I removed the body part and I got this:
Invoke-RestMethod : The underlying connection was closed: An unexpected error occurred on a send.
At line:1 char:1
+ Invoke-RestMethod -uri https://XXXX:8281/api/workflows/?conditions=name=donoth ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
Thanks,
Darn, different error than me.. Unfortunately, my PowerShell skills are very far behind my vCO skill so I won't be able to help any further on this one.. hopefully someone more versed with PS will chime in on the topic.
No worries, thanks for your time.
The "The underlying connection was closed: An unexpected error occurred on a send" error may be caused by the SSL certificate you are using. If you can try the same with unencrypted http to see if you also get the error (I guess not). If you did a wireshark trace you should also see an unsuccessful SSL connection initialisation.
You can work around by different means; if you have PowerCLI tools installed the easiest might be:
Add-PSSnapin VMware.VimAutomation.Core
Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Scope:Session -Confirm:$false
(takes some time to load).
Regards, Ronald
Thanks Roland, I did try your suggestion but unfortunately I got the same error. Even before I was using this statement to get rid of the certificate errors:
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
I'll try with another instance of vCO that has a CA signed certificate.
Regards,
Juan.
Ok, I got it to work by removing
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
And adding this at the beginning of the script
add-type @"
using System.Net;
using System.Security.Cryptography.X509Certificates;
public class TrustAllCertsPolicy : ICertificatePolicy {
public bool CheckValidationResult(
ServicePoint srvPoint, X509Certificate certificate,
WebRequest request, int certificateProblem) {
return true;
}
}
"@
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
Now I can use the GET method:
$ret = Invoke-WebRequest -uri $URL -Headers $headers -ContentType "application/xml" -Method Get
I switched to Invoke-WebRequest so I can obtain the Web request return.
Juan.