mikedopp icon

Powercli API Broke

mikedopp | PRO | 02/28/18 04:18:30 PM UTC | 0 ⭐ | 549 👁️ | Never ⏰ | []
PowerShell |

1.92 KB

|

None

|

0 👍

/

0 👎

#Powercli 6+ 
#and 10 are installed.
$admin = Import-PSCredentialFromJson -Path D:\secure\admin.json
#Get-Module –ListAvailable VM* | Import-Module
Import-Module VMware.VimAutomation.Core
##loaded this via this info: http://vmkdaily.ghost.io/using-the-vcenter-6-5-api-to-deploy-virtual-machines-with-powercli
Import-Module VMware.VimAutomation.Cis.Core /
Connect-VIServer durpvcenter -Credential $admin
Connect-CISServer durpvcenter -Credential $admin
 
 
$admin = Import-PSCredentialFromJson -Path D:\secure\admin.json
$Credential = $admin
$auth = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($Credential.UserName+':'+$Credential.GetNetworkCredential().Password))
$head = @{
  'Authorization' = "Basic $auth"
}
 
$r = Invoke-WebRequest -Uri https://durpvcenter/rest/com/vmware/cis/session -Method Post -Headers $head
$token = (ConvertFrom-Json $r.Content).value
$session = @{'vmware-api-session-id' = $token}
 
# This doesnt work. Always 400 bad request.
# Both webrequest and restmethod say the same thing. 
<# noticed the swagger apiexplorer give this:
This happens on PROD and Non-Prod VM's
{
  "type": "com.vmware.vapi.std.errors.not_found",
  "value": {
    "messages": [
      {
        "args": [],
        "default_message": "The object 'vim.ManagedEntity:WEBSVC5' has already been deleted or has not been completely created",
        "id": "vmsg.ManagedObjectNotFound.summary"
      }
    ]
  }
}
I assume this is why I am getting a 400 Bad Request. Somehow Vsphere is not finding machines.
#>
$r1 = Invoke-WebRequest -Uri https://durpcenter/rest/vcenter/vm -Method Get -Headers $session
$r1 = Invoke-RestMethod -Uri https://durpcenter/rest/vcenter/vm -Method GET -Headers $session
 
#Both Get 400 Bad Request. See above for more info
 
#This will display all the datastore info without issue.
(Invoke-RestMethod -Uri https://durp/rest/vcenter/datastore -Method Get -Headers $session ).value

Comments