Update function to enable password installation

This commit is contained in:
Wes Carroll
2020-04-02 10:27:20 -04:00
parent 2ea7092b91
commit bae9eb9b99
+140 -55
View File
@@ -1,84 +1,169 @@
<# .ExternalHelp ./en-us/ZertoApiWrapper-help.xml #> <# .ExternalHelp ./en-us/ZertoApiWrapper-help.xml #>
#TODO - Add ability to installed with root password, Move to Begin, Process, End Format
function Install-ZertoVra { function Install-ZertoVra {
[cmdletbinding( SupportsShouldProcess = $true )] [cmdletbinding( SupportsShouldProcess )]
param( param(
[Parameter( Mandatory = $true, HelpMessage = "Host name where the VRA is to be installed." )] [Parameter(
Mandatory,
HelpMessage = "Host name where the VRA is to be installed."
)]
[ValidateNotNullOrEmpty()] [ValidateNotNullOrEmpty()]
[string]$hostName, [string]$hostName,
[Parameter( Mandatory = $true, HelpMessage = "Datastore name where the VRA is to be installed." )] [Parameter(
Mandatory,
HelpMessage = "Datastore name where the VRA is to be installed."
)]
[ValidateNotNullOrEmpty()] [ValidateNotNullOrEmpty()]
[string]$datastoreName, [string]$datastoreName,
[Parameter( Mandatory = $true, HelpMessage = "Network name the VRA is to be assigned." )] [Parameter(
Mandatory,
HelpMessage = "Network name the VRA is to be assigned."
)]
[ValidateNotNullOrEmpty()] [ValidateNotNullOrEmpty()]
[string]$networkName, [string]$networkName,
[Parameter( HelpMessage = "Initial amount of memory to assign to the VRA in GB. Default is 3, Minimum is 1, Maximum is 16" )] [Parameter(
HelpMessage = "Initial amount of memory to assign to the VRA in GB. Default is 3, Minimum is 1, Maximum is 16"
)]
[ValidateRange(1, 16)] [ValidateRange(1, 16)]
[int]$memoryInGB = 3, [int]$memoryInGB = 3,
[Parameter( HelpMessage = "Bandwidth group to assign to the VRA. If unspecified will assign to the 'default_group'" )] [Parameter(
HelpMessage = "Bandwidth group to assign to the VRA. If unspecified will assign to the 'default_group'"
)]
[ValidateNotNullOrEmpty()] [ValidateNotNullOrEmpty()]
[string]$groupName, [string]$groupName,
[Parameter( ParameterSetName = "Dhcp", Mandatory = $true, HelpMessage = "Assign a DHCP address to the VRA." )] [Parameter(
ParameterSetName = "Dhcp",
Mandatory,
HelpMessage = "Assign a DHCP address to the VRA."
)]
[Parameter(
ParameterSetName = "DhcpWithRoot",
Mandatory,
HelpMessage = "Assign a DHCP address to the VRA."
)]
[switch]$Dhcp, [switch]$Dhcp,
[Parameter( ParameterSetName = "StaticIp", Mandatory = $true, HelpMessage = "Static IP address to assign to the VRA." )] [Parameter(
ParameterSetName = "StaticIp",
Mandatory,
HelpMessage = "Static IP address to assign to the VRA."
)]
[Parameter(
ParameterSetName = "StaticIpWithRoot",
Mandatory,
HelpMessage = "Static IP address to assign to the VRA."
)]
[ValidateScript( { $_ -match [IPAddress]$_ })] [ValidateScript( { $_ -match [IPAddress]$_ })]
[string]$vraIpAddress, [string]$vraIpAddress,
[Parameter( ParameterSetName = "StaticIp", Mandatory = $true, HelpMessage = "Default gateway to assign to the VRA" )] [Parameter(
ParameterSetName = "StaticIp",
Mandatory,
HelpMessage = "Default gateway to assign to the VRA"
)]
[Parameter(
ParameterSetName = "StaticIpWithRoot",
Mandatory,
HelpMessage = "Default gateway to assign to the VRA"
)]
[ValidateScript( { $_ -match [IPAddress]$_ })] [ValidateScript( { $_ -match [IPAddress]$_ })]
[string]$defaultGateway, [string]$defaultGateway,
[Parameter( ParameterSetName = "StaticIp", Mandatory = $true, HelpMessage = "Subnetmask to be assigned to the VRA" )] [Parameter(
ParameterSetName = "StaticIp",
Mandatory,
HelpMessage = "Subnetmask to be assigned to the VRA"
)]
[Parameter(
ParameterSetName = "StaticIpWithRoot",
Mandatory,
HelpMessage = "Subnetmask to be assigned to the VRA"
)]
[ValidateScript( { $_ -match [IPAddress]$_ })] [ValidateScript( { $_ -match [IPAddress]$_ })]
[string]$subnetMask [string]$subnetMask,
[Parameter(
ParameterSetName = "StaticIpWithRoot",
Mandatory,
HelpMessage = "Use this switch to install the VRA using the root password install method."
)]
[Parameter(
ParameterSetName = "DhcpWithRoot",
Mandatory,
HelpMessage = "Use this switch to install the VRA using the root password install method."
)]
[switch]$UseRootCredential,
[Parameter(
ParameterSetName = "StaticIpWithRoot",
Mandatory,
HelpMessage = "The password for the root user of the ESXi host where the VRA is to be installed."
)]
[Parameter(
ParameterSetName = "DhcpWithRoot",
Mandatory,
HelpMessage = "The password for the root user of the ESXi host where the VRA is to be installed."
)]
[ValidateNotNullOrEmpty()]
[securestring]$HostRootPassword
) )
# Build the VRA Name.
$vraName = "Z-VRA-{0}" -f $hostName begin {
# If the VRA does not exist, proceed with the installation. If it does exist, bypass and
if ( -not (Get-ZertoVra -vraName $vraName) ) { }
# Get identifiers for each item provided by name. Process {
$siteIdentifier = $script:zvmLocalInfo.SiteIdentifier # Build the VRA Name.
$hostIdentifier = Get-ZertoVirtualizationSite -siteIdentifier $siteIdentifier -hosts | Where-Object { $_.VirtualizationHostName -eq $hostName } | Select-Object hostIdentifier -ExpandProperty hostIdentifier $vraName = "Z-VRA-{0}" -f $hostName
$networkIdentifier = Get-ZertoVirtualizationSite -siteIdentifier $siteIdentifier -networks | Where-Object { $_.VirtualizationNetworkName -eq $networkName } | Select-Object NetworkIdentifier -ExpandProperty NetworkIdentifier # If the VRA does not exist, proceed with the installation. If it does exist, bypass and
$datastoreIdentifier = Get-ZertoVirtualizationSite -siteIdentifier $siteIdentifier -datastores | Where-Object { $_.DatastoreName -eq $datastoreName } | Select-Object DatastoreIdentifier -ExpandProperty DatastoreIdentifier if ( -not (Get-ZertoVra -vraName $vraName) ) {
if ($datastoreIdentifier.count -gt 1) { # Get identifiers for each item provided by name.
$hostDevices = Get-ZertoVirtualizationSite -siteIdentifier $siteIdentifier -devices -hostIdentifier $hostIdentifier $siteIdentifier = $script:zvmLocalInfo.SiteIdentifier
$datastoreIdentifier = foreach ($identifier in $datastoreIdentifier) { $hostIdentifier = Get-ZertoVirtualizationSite -siteIdentifier $siteIdentifier -hosts | Where-Object { $_.VirtualizationHostName -eq $hostName } | Select-Object hostIdentifier -ExpandProperty hostIdentifier
if ($identifier -in $hostDevices.DatastoreIdentifier) { $networkIdentifier = Get-ZertoVirtualizationSite -siteIdentifier $siteIdentifier -networks | Where-Object { $_.VirtualizationNetworkName -eq $networkName } | Select-Object NetworkIdentifier -ExpandProperty NetworkIdentifier
$identifier $datastoreIdentifier = Get-ZertoVirtualizationSite -siteIdentifier $siteIdentifier -datastores | Where-Object { $_.DatastoreName -eq $datastoreName } | Select-Object DatastoreIdentifier -ExpandProperty DatastoreIdentifier
if ($datastoreIdentifier.count -gt 1) {
$hostDevices = Get-ZertoVirtualizationSite -siteIdentifier $siteIdentifier -devices -hostIdentifier $hostIdentifier
$datastoreIdentifier = foreach ($identifier in $datastoreIdentifier) {
if ($identifier -in $hostDevices.DatastoreIdentifier) {
$identifier
}
}
if ($datastoreIdentifier.count -gt 1) {
Write-Error "Datastore $datastoreName has more than one identifier associated with it on the specified host. Please review and try again."
Break
} }
} }
if ($datastoreIdentifier.count -gt 1) {
Write-Error "Datastore $datastoreName has more than one identifier associated with it on the specified host. Please review and try again." # Build the JSON object through an Ordered Hashtable.
Break $vraBasic = [ordered]@{ }
$vraBasic['DatastoreIdentifier'] = $datastoreIdentifier.toString()
if ($PSBoundParameters.ContainsKey('groupName')) {
$vraBasic['GroupName'] = $groupName
}
$vraBasic['HostIdentifier'] = $hostIdentifier.toString()
$vraBasic['MemoryInGB'] = $memoryInGB
$vraBasic['NetworkIdentifier'] = $networkIdentifier.toString()
$vraBasic['UsePublicKeyInsteadOfCredentials'] = $true
$vraBasicNetwork = [ordered]@{ }
if ( $PSCmdlet.ParameterSetName -eq "StaticIp" -or $PSCmdlet.ParameterSetName -eq "StaticIpWithRoot") {
$vraBasicNetwork['DefaultGateway'] = $defaultGateway.toString()
$vraBasicNetwork['SubnetMask'] = $subnetMask.toString()
$vraBasicNetwork['VraIPAddress'] = $vraIpAddress.toString()
$vraBasicNetwork['VraIPConfigurationTypeApi'] = "Static"
} else {
$vraBasicNetwork['VraIPConfigurationTypeApi'] = "Dhcp"
}
$vraBasic['VraNetworkDataApi'] = $vraBasicNetwork
if ($PSCmdlet.ParameterSetName -eq "StaticIpWithRoot" -or $PSCmdlet.ParameterSetName -eq "DhcpWithRoot") {
$HostRootCredential = [pscredential]::new('root', $HostRootPassword)
$vraBasic['UsePublicKeyInsteadOfCredentials'] = $false
$vraBasic['HostRootPassword'] = $HostRootCredential.GetNetworkCredential().Password
} }
}
# Build the JSON object through an Ordered Hashtable. # Leverage WhatIf functionality to see what might happen, if WhatIf is not specified, attempt to install.
$vraBasic = [ordered]@{ } if ($PSCmdlet.ShouldProcess($hostName)) {
$vraBasic['DatastoreIdentifier'] = $datastoreIdentifier.toString() Invoke-ZertoRestRequest -uri "vras" -method POST -body $($vraBasic | ConvertTo-Json)
if ($PSBoundParameters.ContainsKey('groupName')) { }
$vraBasic['GroupName'] = $groupName
}
$vraBasic['HostIdentifier'] = $hostIdentifier.toString()
$vraBasic['MemoryInGB'] = $memoryInGB
$vraBasic['NetworkIdentifier'] = $networkIdentifier.toString()
$vraBasic['UsePublicKeyInsteadOfCredentials'] = $true
$vraBasicNetwork = [ordered]@{ }
if ( $PSCmdlet.ParameterSetName -eq "StaticIp" ) {
$vraBasicNetwork['DefaultGateway'] = $defaultGateway.toString()
$vraBasicNetwork['SubnetMask'] = $subnetMask.toString()
$vraBasicNetwork['VraIPAddress'] = $vraIpAddress.toString()
$vraBasicNetwork['VraIPConfigurationTypeApi'] = "Static"
} else { } else {
$vraBasicNetwork['VraIPConfigurationTypeApi'] = "Dhcp" Write-Error "Host $hostName already has a VRA installed. Aborting Install Call"
} }
$vraBasic['VraNetworkDataApi'] = $vraBasicNetwork }
End {
# Leverage WhatIf functionality to see what might happen, if WhatIf is not specified, attempt to install.
if ($PSCmdlet.ShouldProcess("Preforming operation 'Install-Vra' on Host $hostName with the following data \n $($vraBasic | ConvertTo-Json)")) {
Invoke-ZertoRestRequest -uri "vras" -method POST -body $($vraBasic | ConvertTo-Json)
}
} else {
Write-Error "Host $hostName already has a VRA installed. Aborting Install Call"
} }
} }