-
Notifications
You must be signed in to change notification settings - Fork 2
/
PowervROps-Auth.psm1
78 lines (67 loc) · 2.61 KB
/
PowervROps-Auth.psm1
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
function acquireToken {
<#
.SYNOPSIS
Acquire a token to perform REST API calls.
.DESCRIPTION
Performing this request would yield a response object that includes token and its validity.
.EXAMPLE
acquireToken -resthost 'fqdn of vROps instance' -accept json -username 'admin' -password 'somepassword' -authSource 'local'
.PARAMETER resthost
FQDN of the vROps instance or cluster to operate against.
.PARAMETER accept
Analogous to the header parameter 'Accept' used in REST calls, valid values are xml or json.
However, the module has only been tested against json.
.PARAMETER username
Analogous to the header parameter 'Accept' used in REST calls, valid values are xml or json.
However, the module has only been tested against json.
.PARAMETER authSource
Analogous to the header parameter 'Accept' used in REST calls, valid values are xml or json.
However, the module has only been tested against json.
.PARAMETER password
Analogous to the header parameter 'Accept' used in REST calls, valid values are xml or json.
However, the module has only been tested against json.
.PARAMETER contenttype
Analogous to the header parameter 'Content-Type' used in REST calls, valid values are xml or json.
However, the module has only been tested against json.
.NOTES
Added in version 0.2
#>
Param (
[parameter(Mandatory=$true)][String]$resthost,
[parameter(Mandatory=$false)][ValidateSet('xml','json')][string]$accept = 'json',
[parameter(Mandatory=$false)][ValidateSet('xml','json')][string]$contenttype = 'json',
[parameter(Mandatory=$false)][string]$username,
[parameter(Mandatory=$false)][string]$authSource,
[parameter(Mandatory=$false)][string]$password
)
Process {
$restheaders = @{}
$restheaders.Add('Accept','application/'+$accept)
$restheaders.Add('Content-Type','application/'+$contenttype)
$url = 'https://' + $resthost + '/suite-api/api/auth/token/acquire'
if ($contenttype -eq 'json') {
$body = @{
'username' = $username
'authSource' = $authSource
'password' = $password
'others' = @()
'otherAttributes' = @{}
} | convertto-json
}
Write-Verbose ("URL: " + $url)
Try {
$response = Invoke-RestMethod -Method 'POST' -Uri $url -Headers $restheaders -body $body -ErrorAction Stop
return $response.token
}
Catch {
Write-Host ("ERROR: ") -ForegroundColor red -nonewline; Write-Host 'Token not generated' -ForegroundColor White;
Write-Host $response
Write-Host $Error[0].Exception
}
}
}
function getAndy {
write-host "This is my function"
}
export-modulemember -function 'acquire*'
export-modulemember -function 'getAndy'