VMware Solutions Discussions

Snapcenter vshpere plugin 4.5 - powershell

Nirsh
1,042 Views

Dear team,

 

I'm trying to generate a token for api queries with powershell, but can't find the right way to do it...

 

How can I generate a token with powrrshell or create a permanent token?

 

 

2 REPLIES 2

aladd
991 Views

What kind of queries are you attempting to run?

TonyPatton
769 Views

This is how I did it, not necessarily the best or tidiest way.  I was trying to learn how to do REST APIs in PowerShell, so it's probably a mixture of multiple methods.  I never did go back and finish the script I was writing (for backup reports).

 

$RESTAPIServer = "<snapcenter appliance fqdn/ip>:8144"

# Get credentials
try {
    $credentials = Import-Clixml <file with encrypted credentials>.xml -ErrorAction:Stop
} catch {
    write-host "Could not open credentials." -ForegroundColor Red
    exit (1)
}
$REST_API_User = $credentials.UserName
$REST_API_Password = $credentials.GetNetworkCredential().Password

$loginRequest = @{
    username = $REST_API_User
    password = $REST_API_Password
} | ConvertTo-Json

# Set REST authentication headers
$baseURL = "https://" + $RESTAPIServer + "/api/4.1"
$SnapcenterSessionURL = $baseURL + "/auth/login"
$Type = "application/json"

$params = @{
    Uri = $SnapcenterSessionURL
    Body = $loginRequest
    ContentType = 'application/json'
    Method = 'POST'
}

$authResponse = Invoke-RestMethod @params
if ($authResponse.statusCode -eq 200) {
    write-host "Authentication successful"
    $authToken = $authResponse.response.token
}

 

The credentials are encrypted in an XML file, only decryptable by the account & on the machine that they were encrypted on.

Public