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.