-
Notifications
You must be signed in to change notification settings - Fork 10
/
vcenter-Api-working.ps1
58 lines (53 loc) · 2.13 KB
/
vcenter-Api-working.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# C# code class type for ignore self signed certificate
if (-not ([System.Management.Automation.PSTypeName]'ServerCertificateValidationCallback').Type)
{
$certCallback = @'
using System;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
public class ServerCertificateValidationCallback
{
public static void Ignore()
{
if (ServicePointManager.ServerCertificateValidationCallback == null)
{
ServicePointManager.ServerCertificateValidationCallback +=
delegate
(
Object Obj,
X509Certificate certificate,
X509Chain chain,
SslPolicyErrors errors
)
{
return true;
};
}
}
}
'@
Add-Type $certCallback
}
#execute c# code and ignore invalid certificate error
[ServerCertificateValidationCallback]::Ignore();
#Type credential and process to base 64
$credential = Get-Credential
$auth = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($credential.UserName+':'+$credential.GetNetworkCredential().Password))
$head = @{
Authorization = "Basic $auth"
}
$vCenter = 'vcsa.vcloud-lab.com'
#Authenticate against vCenter
$a = Invoke-WebRequest -Uri "https://$vCenter/rest/com/vmware/cis/session" -Method Post -Headers $head
$token = ConvertFrom-Json $a.Content | Select-Object -ExpandProperty Value
$session = @{'vmware-api-session-id' = $token}
#Get vm list from vcenter
$a1 = Invoke-WebRequest -Uri "https://$vCenter/rest/vcenter/vm" -Method Get -Headers $session
$vms = ConvertFrom-Json $a1.Content | Select-Object -ExpandProperty Value
$vms
#Get single VM list
$vmMof = 'vm-900'
$b1 = Invoke-WebRequest -Uri "https://$vCenter/rest/vcenter/vm/$vmMof" -Method Get -Headers $session
$test = ConvertFrom-Json $b1.Content | Select-Object -ExpandProperty Value
$test