Advertisement
mikedopp

Powercli API Broke

Feb 28th, 2018
318
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #Powercli 6+
  2. #and 10 are installed.
  3. $admin = Import-PSCredentialFromJson -Path D:\secure\admin.json
  4. #Get-Module –ListAvailable VM* | Import-Module
  5. Import-Module VMware.VimAutomation.Core
  6. ##loaded this via this info: http://vmkdaily.ghost.io/using-the-vcenter-6-5-api-to-deploy-virtual-machines-with-powercli
  7. Import-Module VMware.VimAutomation.Cis.Core /
  8. Connect-VIServer durpvcenter -Credential $admin
  9. Connect-CISServer durpvcenter -Credential $admin
  10.  
  11.  
  12. $admin = Import-PSCredentialFromJson -Path D:\secure\admin.json
  13. $Credential = $admin
  14. $auth = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($Credential.UserName+':'+$Credential.GetNetworkCredential().Password))
  15. $head = @{
  16.   'Authorization' = "Basic $auth"
  17. }
  18.  
  19. $r = Invoke-WebRequest -Uri https://durpvcenter/rest/com/vmware/cis/session -Method Post -Headers $head
  20. $token = (ConvertFrom-Json $r.Content).value
  21. $session = @{'vmware-api-session-id' = $token}
  22.  
  23. # This doesnt work. Always 400 bad request.
  24. # Both webrequest and restmethod say the same thing.
  25. <# noticed the swagger apiexplorer give this:
  26. This happens on PROD and Non-Prod VM's
  27. {
  28.   "type": "com.vmware.vapi.std.errors.not_found",
  29.   "value": {
  30.     "messages": [
  31.       {
  32.         "args": [],
  33.         "default_message": "The object 'vim.ManagedEntity:WEBSVC5' has already been deleted or has not been completely created",
  34.         "id": "vmsg.ManagedObjectNotFound.summary"
  35.       }
  36.     ]
  37.   }
  38. }
  39. I assume this is why I am getting a 400 Bad Request. Somehow Vsphere is not finding machines.
  40. #>
  41. $r1 = Invoke-WebRequest -Uri https://durpcenter/rest/vcenter/vm -Method Get -Headers $session
  42. $r1 = Invoke-RestMethod -Uri https://durpcenter/rest/vcenter/vm -Method GET -Headers $session
  43.  
  44. #Both Get 400 Bad Request. See above for more info
  45.  
  46. #This will display all the datastore info without issue.
  47. (Invoke-RestMethod -Uri https://durp/rest/vcenter/datastore -Method Get -Headers $session ).value
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement