/
Part1.ps1
184 lines (163 loc) · 5.21 KB
/
Part1.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
Class rOpenSdk
{
[String]$Name
[String]$User
[String]$SessionSecret
[String]$Auth
[String]$Uri
}
function Invoke-RestCall
{
[CmdletBinding()]
param (
[String]$Method,
[String]$Request,
[PSObject]$Body
)
Process
{
Write-Verbose -Message "$($MyInvocation.MyCommand.Name)"
Write-Verbose -Message "`t$($PSCmdlet.ParameterSetName)"
Write-Verbose -Message "`tCalled from $($stack = Get-PSCallStack; $stack[1].Command) at $($stack[1].Location)"
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add('Accept','application/json')
$sRest = @{
Uri = $script:DefaultRestServer.Uri,$Request -join '/'
Method = $Method
ContentType = 'application/json'
Headers = $local:headers
ErrorAction = 'Stop'
}
if(!$script:DefaultRestServer.SessionSecret){
$sRest.Headers.Add('Authorization',$script:DefaultRestServer.Auth)
$sRest.Add('SessionVariable','rSession')
}
else{
$sRest.Add('WebSession',$script:rSession)
}
if (Get-Process -Name fiddler -ErrorAction SilentlyContinue)
{$sRest.Add('Proxy', 'http://127.0.0.1:8888')}
# To handle nested properties the Depth parameter is used explicitely (default is 2)
if ($Body.Count -ne 0)
{
$sRest.Add('Body', ($Body | ConvertTo-Json -Depth 32 -Compress))
}
Write-Debug -Message "`tUri : $($sRest.Uri)"
Write-Debug -Message "`tMethod : $($sRest.Method)"
Write-Debug -Message "`tContentType : $($sRest.ContentType)"
Write-Debug -Message "`tHeaders"
$sRest.Headers.GetEnumerator() | ForEach-Object -Process {Write-Debug -Message "`t : $($_.Name)`t$($_.Value)"}
Write-Debug -Message "`tBody : $($sRest.Body)"
# The intermediate $result is used to avoid returning a PSMemberSet
Try
{
$result = Invoke-RestMethod @sRest
$script:DefaultRestServer.SessionSecret = $result.Value
$script:rSession = $rSession
}
Catch
{
$excpt = $_.Exception
Write-Debug -Message 'Exception'
Write-Debug -Message "`tERROR-CODE = $($excpt.Response.Headers['ERROR-CODE'])"
Write-Debug -Message "`tERROR-CODE = $($excpt.Response.Headers['ERROR-MESSAGE'])"
}
$result
Write-Debug -Message 'Leaving Invoke-hRavelloRest'
}
}
function Get-rAuthHeader
{
[CmdletBinding()]
param (
[String]$User,
[String]$Password
)
Process
{
Write-Verbose -Message "$($MyInvocation.MyCommand.Name)"
Write-Verbose -Message "`t$($PSCmdlet.ParameterSetName)"
Write-Verbose -Message "`tCalled from $($stack = Get-PSCallStack; $stack[1].Command) at $($stack[1].Location)"
$Encoded = [System.Text.Encoding]::UTF8.GetBytes(($User,$Password -Join ':'))
$EncodedPassword = [System.Convert]::ToBase64String($Encoded)
Write-Debug -Message "`tEncoded : $($EncodedPassword)"
"Basic $($EncodedPassword)"
}
}
function Connect-rViServer{
[CmdletBinding()]
param(
[String]$Server,
[String]$User,
[String]$Password
)
Write-Verbose -Message "$($MyInvocation.MyCommand.Name)"
Write-Verbose -Message "`t$($PSCmdlet.ParameterSetName)"
Write-Verbose -Message "`tCalled from $($stack = Get-PSCallStack; $stack[1].Command) at $($stack[1].Location)"
$obj = [rOpenSdk]::New()
$obj.Name = $Server
$obj.User = $User
$obj.Uri = "https://$($Server)/rest"
$obj.Auth = Get-rAuthHeader -User $User -Password $Password
$script:DefaultRestServer = $obj
$sRestCall = @{
Method = 'POST'
Request = 'com/vmware/cis/session'
Body = $null
}
Invoke-RestCall @sRestCall
}
function Get-rVMHost{
param(
[string[]]$Cluster,
[string[]]$Datacenter,
[string[]]$Folder,
[string[]]$VMhost,
[string[]]$Name,
[string[]]$ConnectionState,
[switch]$StandAlone = $false
)
Write-Verbose -Message "$($MyInvocation.MyCommand.Name)"
Write-Verbose -Message "`t$($PSCmdlet.ParameterSetName)"
Write-Verbose -Message "`tCalled from $($stack = Get-PSCallStack; $stack[1].Command) at $($stack[1].Location)"
$sRestCall = @{
Method = 'GET'
Request = 'vcenter/host'
}
$body = @{}
if($Cluster){
$body.Add('clusters',$Cluster)
}
if($Datacenter){
$body.Add('datacenters',$Datacenter)
}
if($Folder){
$body.Add('folders',$Folder)
}
if($VMHost){
$body.Add('hosts',$VMHost)
}
if($Name){
$body.Add('names',$Name)
}
if($ConnectionState){
$body.Add('connection_states',$ConnectionState)
}
if($StandAlone -ne $false){
$body.Add('standalone',$StandAlone)
}
if($body){
$sRestCall.Add('Body',$body)
}
Invoke-RestCall @sRestCall
}
function Get-rProxyConfig{
Write-Verbose -Message "$($MyInvocation.MyCommand.Name)"
Write-Verbose -Message "`t$($PSCmdlet.ParameterSetName)"
Write-Verbose -Message "`tCalled from $($stack = Get-PSCallStack; $stack[1].Command) at $($stack[1].Location)"
$sRestCall = @{
Method = 'GET'
Request = 'appliance/techpreview/networking/proxy'
}
Invoke-RestCall @sRestCall
}