Open mtboren opened 6 years ago
I updated the functions to allow passing credentials. Here's the code:
Function newGet-ZertoAuthToken { [CmdletBinding()] param( [Parameter(Mandatory=$false, HelpMessage = 'Zerto Server or ENV:\ZertoServer')] [string] $ZertoServer = ( Get-EnvZertoServer ) , [Parameter(Mandatory=$false, HelpMessage = 'Zerto Server URL Port')] [string] $ZertoPort = ( Get-EnvZertoPort ), [Parameter( HelpMessage = 'Credentials to connect to Zerto')] $ZertoCredentials )
Set-SSLCertByPass
if ([String]::IsNullOrEmpty($ZertoServer) ) {
throw "Missing Zerto Server"
}
$baseURL = "https://" + $ZertoServer + ":"+$ZertoPort+"/v1/"
$FullURL = $baseURL + "session/add"
$TypeJSON = "application/json"
Write-Verbose $FullURL
if ([String]::IsNullOrEmpty($ZertoCredentials) )
{
$ZertoCredentials = Get-Credential -Message "Enter your Zerto credentials for '$ZertoServer'"
}
If ($ZertoCredentials -NE $null) {
#Remove our Zerto Version
Remove-Item ENV:ZertoToken -Force -ErrorAction Ignore
Remove-Item ENV:ZertoVersion -Force -ErrorAction Ignore
# Authenticating with Zerto APIs - Basic AUTH over SSL
$authInfo = ("{0}\{1}:{2}" -f $ZertoCredentials.GetNetworkCredential().domain , $ZertoCredentials.GetNetworkCredential().UserName, $ZertoCredentials.GetNetworkCredential().Password )
$authInfo = [System.Text.Encoding]::UTF8.GetBytes($authInfo)
$authInfo = [System.Convert]::ToBase64String($authInfo)
$headers = @{Authorization=("Basic {0}" -f $authInfo)}
$sessionBody = '{"AuthenticationMethod": "1"}'
#Need to check our Response.
try {
$xZertoSessionResponse = Invoke-WebRequest -Uri $FullURL -Headers $headers -Method POST -Body $sessionBody -ContentType $TypeJSON
} catch {
$xZertoSessionResponse = $_.Exception.Response
}
if ($xZertoSessionResponse -eq $null ) {
Throw "Zerto Server ${ZertoServer}:${ZertoPort} not responding."
} elseif ($xZertoSessionResponse.StatusCode -eq "200") {
$xZertoSession = $xZertoSessionResponse.headers.get_item("x-zerto-session")
$ZertoSessionHeader = @{"x-zerto-session"=$xZertoSession}
return $ZertoSessionHeader
} else {
if ($xZertoSessionResponse.StatusCode.value__ -eq "401") {
Throw "User $ZertoUser not authorized or invalid password."
}
return $null
}
} else {
return $null
}
}
Function newConnect-ZertoZVM { [CmdletBinding()] param( [Parameter(Mandatory=$true, HelpMessage = 'Zerto Server or ENV:\ZertoServer')] [string] $ZertoServer , [Parameter(Mandatory=$false, HelpMessage = 'Zerto Server URL Port')] [string] $ZertoPort = 9669 , [Parameter(Mandatory=$false, HelpMessage = 'Credentials to connect to Zerto')] $ZertoCredentials )
Set-Item ENV:ZertoServer $ZertoServer
Set-Item ENV:ZertoPort $ZertoPort
Set-Item ENV:ZertoToken ((newGet-ZertoAuthToken -ZertoServer $ZertoServer -ZertoPort $ZertoPort -ZertoCredentials $ZertoCredentials) | ConvertTo-Json -Compress)
Set-Item ENV:ZertoVersion (Get-ZertoLocalSite).version
}
Still enjoying the module, thanks!
To allow for some non-interactive (or less interactive) connection to a Zerto ZVM, it would be useful to have the
-Credential
parameter on theConnect-ZertoZVM
(and, subsequently, theGet-ZertoAuthToken
) cmdlets.Then, a consumer could, say, import some encrypted creds into a PSCredential object, pass them as the value to
-Credential
, and not need to enter the creds at every Connect operation.I made a quick commit with some code to this effect at the fork at https://github.com/mtboren/ZertoModule_fork/tree/dev. The Compare & Pull Request comparison view shows the updates nicely.
How does such an enhancement sound to you?
Cheers