Merge pull request #63 from ZertoPublic/ExportNicUpdate

Update Export and Import VpgNicSetting functions
This commit is contained in:
Wes Carroll
2019-12-22 11:01:27 -05:00
committed by GitHub
4 changed files with 76 additions and 59 deletions
+2
View File
@@ -3,3 +3,5 @@
### Zerto Virtual Manager ### Zerto Virtual Manager
* Addressed a reported [issue](https://github.com/ZertoPublic/ZertoApiWrapper/issues/60) in the `Get-ZertoRecoveryReport` function where the `-VpgIdentifier` parameter was not working. This parameter is not accepted by the API as a valid filter and is ignored. This parameter has been removed from the function. * Addressed a reported [issue](https://github.com/ZertoPublic/ZertoApiWrapper/issues/60) in the `Get-ZertoRecoveryReport` function where the `-VpgIdentifier` parameter was not working. This parameter is not accepted by the API as a valid filter and is ignored. This parameter has been removed from the function.
* Addressed a reported [issue](https://github.com/ZertoPublic/ZertoApiWrapper/issues/60) where the `Export-ZertoVpgNicSetting` function would not properly execute when run against a VM with no NICs attached.
* In reviewing the `Export-ZertoVpgNicSetting`, a review of the `Import-ZertoVpgNicSetting` was completed and it was determined to update the import logic based on various test cases. Along with this, it is now possible to reset the NIC settings to the default state with the `Import-ZertoVpgNicSetting` command. Please review the [Import-ZertoVpgNicSetting help](https://github.com/ZertoPublic/ZertoApiWrapper/blob/master/docs/Import-ZertoVmNicSetting.md) to review the updated options and import logic.
@@ -45,33 +45,37 @@ function Export-ZertoVmNicSetting {
$null = Remove-ZertoVpgSettingsIdentifier -vpgSettingsIdentifier $settingsId $null = Remove-ZertoVpgSettingsIdentifier -vpgSettingsIdentifier $settingsId
$networkMap = Get-Map -InputObject $networks -key "NetworkIdentifier" -value "VirtualizationNetworkName" $networkMap = Get-Map -InputObject $networks -key "NetworkIdentifier" -value "VirtualizationNetworkName"
foreach ($vm in $vmSettings) { foreach ($vm in $vmSettings) {
$nicInfo = [PSCustomObject]@{ if ($vm.nics.count -gt 0) {
VPGName = $group.VPGName foreach ($nic in $vm.nics) {
VMName = $vmMap[$($vm.vmIdentifier)] $nicInfo = [PSCustomObject]@{
NicIdentifier = $vm.nics.NicIdentifier VPGName = $group.VPGName
LiveNetwork = $networkMap[$vm.nics.failover.Hypervisor.NetworkIdentifier] VMName = $vmMap[$vm.vmIdentifier]
LiveShouldReplaceMac = $vm.nics.failover.Hypervisor.ShouldReplaceMacAddress NicIdentifier = $nic.NicIdentifier
LiveIsDHCP = $vm.Nics.failover.Hypervisor.IpConfig.IsDhcp LiveNetwork = $networkMap[$nic.failover.Hypervisor.NetworkIdentifier]
LiveIpAddress = $vm.nics.failover.Hypervisor.IpConfig.StaticIp LiveShouldReplaceMac = $nic.failover.Hypervisor.ShouldReplaceMacAddress
LiveIpSubnetMask = $vm.nics.failover.Hypervisor.IpConfig.SubnetMask LiveIsDHCP = $nic.failover.Hypervisor.IpConfig.IsDhcp
LiveIpDefaultGateway = $vm.nics.failover.Hypervisor.IpConfig.Gateway LiveIpAddress = $nic.failover.Hypervisor.IpConfig.StaticIp
LivePrimaryDns = $vm.nics.failover.Hypervisor.IpConfig.PrimaryDns LiveIpSubnetMask = $nic.failover.Hypervisor.IpConfig.SubnetMask
LiveSecondayDns = $vm.nics.failover.Hypervisor.IpConfig.SecondaryDns LiveIpDefaultGateway = $nic.failover.Hypervisor.IpConfig.Gateway
LiveDnsSuffix = $vm.nics.failover.Hypervisor.DnsSuffix LivePrimaryDns = $nic.failover.Hypervisor.IpConfig.PrimaryDns
TestNetwork = $networkMap[$vm.nics.failoverTest.Hypervisor.NetworkIdentifier] LiveSecondayDns = $nic.failover.Hypervisor.IpConfig.SecondaryDns
TestShouldReplaceMac = $vm.nics.failoverTest.Hypervisor.ShouldReplaceMacAddress LiveDnsSuffix = $nic.failover.Hypervisor.DnsSuffix
TestIsDHCP = $vm.Nics.failoverTest.Hypervisor.IpConfig.IsDhcp TestNetwork = $networkMap[$nic.failoverTest.Hypervisor.NetworkIdentifier]
TestIpAddress = $vm.nics.failoverTest.Hypervisor.IpConfig.StaticIp TestShouldReplaceMac = $nic.failoverTest.Hypervisor.ShouldReplaceMacAddress
TestIpSubnetMask = $vm.nics.failoverTest.Hypervisor.IpConfig.SubnetMask TestIsDHCP = $nic.failoverTest.Hypervisor.IpConfig.IsDhcp
TestIpDefaultGateway = $vm.nics.failoverTest.Hypervisor.IpConfig.Gateway TestIpAddress = $nic.failoverTest.Hypervisor.IpConfig.StaticIp
TestPrimaryDns = $vm.nics.failoverTest.Hypervisor.IpConfig.PrimaryDns TestIpSubnetMask = $nic.failoverTest.Hypervisor.IpConfig.SubnetMask
TestSecondayDns = $vm.nics.failoverTest.Hypervisor.IpConfig.SecondaryDns TestIpDefaultGateway = $nic.failoverTest.Hypervisor.IpConfig.Gateway
TestDnsSuffix = $vm.nics.failoverTest.Hypervisor.DnsSuffix TestPrimaryDns = $nic.failoverTest.Hypervisor.IpConfig.PrimaryDns
TestSecondayDns = $nic.failoverTest.Hypervisor.IpConfig.SecondaryDns
TestDnsSuffix = $nic.failoverTest.Hypervisor.DnsSuffix
}
$nicInfo
}
} }
$nicInfo
} }
} }
$nicSettings | Export-Csv -Path $OutputFile $nicSettings | Export-Csv -Path $OutputFile -NoTypeInformation
} }
end { end {
@@ -44,15 +44,28 @@ function Import-ZertoVmNicSetting {
[string]::IsNullOrWhiteSpace($Vm.LiveShouldReplaceMac) -or [string]::IsNullOrWhiteSpace($Vm.LiveShouldReplaceMac) -or
[string]::IsNullOrWhiteSpace($Vm.TestNetwork) -or [string]::IsNullOrWhiteSpace($Vm.TestNetwork) -or
[string]::IsNullOrWhiteSpace($Vm.TestShouldReplaceMac)) { [string]::IsNullOrWhiteSpace($Vm.TestShouldReplaceMac)) {
Write-Error "$($Vm.VMName) does not contain all the required data. Please check the CSV entry for this item and try again." -ErrorAction Continue Write-Error "$($Vm.VMName) does not contain all the required data. Please check the CSV entry for this item and try again. You are required to provide the VPGName, VMName, NicIdentifier, LiveNetwork, and ShouldReplaceMacAddress for each Nic." -ErrorAction Continue
} else { } else {
$uri = "vpgSettings/{0}/vms/{1}" -f $vpgSettingsId, $vmMap[$vm.VMName] $uri = "vpgSettings/{0}/vms/{1}" -f $vpgSettingsId, $vmMap[$vm.VMName]
$VmNicSettings = Get-ZertoVpgSetting -vpgSettingsIdentifier $vpgSettingsId -vmIdentifier $vmMap[$vm.VMName] $VmNicSettings = Get-ZertoVpgSetting -vpgSettingsIdentifier $vpgSettingsId -vmIdentifier $vmMap[$vm.VMName]
foreach ($nic in $VmNicSettings.nics) { foreach ($nic in $VmNicSettings.nics) {
if ($nic.NicIdentifier -eq $vm.NicIdentifier) { if ($nic.NicIdentifier -eq $vm.NicIdentifier) {
$nic.failover.Hypervisor.NetworkIdentifier = $NetworkMap[$vm.LiveNetwork] $NicUri = "{0}/nics/{1}" -f $uri, $nic.NicIdentifier
$nic.failover.Hypervisor.ShouldReplaceMacAddress = $vm.LiveShouldReplaceMac Invoke-ZertoRestRequest -uri $NicUri -Method "DELETE" > $null
if ($null -eq $nic.failover.Hypervisor.IpConfig -and ($null -ne $vm.LiveIsDHCP -or $null -ne $vm.LiveIpAddress)) { $nicSettings = Invoke-ZertoRestRequest -uri $NicUri -Method "GET"
$nicSettings.failover.Hypervisor.NetworkIdentifier = $NetworkMap[$vm.LiveNetwork]
$nicSettings.failover.Hypervisor.ShouldReplaceMacAddress = $vm.LiveShouldReplaceMac
if ($vm.LiveIsDHCP -imatch "true") {
$IpConfig = [PSCustomObject]@{
IsDhcp = $vm.LiveIsDHCP
PrimaryDns = $vm.LivePrimaryDns
SecondaryDns = $vm.LiveSecondayDns
}
$nicSettings.failover.Hypervisor.IpConfig = $IpConfig
$nicSettings.failover.Hypervisor.DnsSuffix = $vm.LiveDnsSuffix
} elseif (($vm.LiveIsDHCP -imatch "false" -or
[string]::IsNullOrWhiteSpace($vm.LiveIsDHCP)) -and
-not [string]::IsNullOrWhiteSpace($vm.LiveIpAddress)) {
$IpConfig = [PSCustomObject]@{ $IpConfig = [PSCustomObject]@{
IsDhcp = $vm.LiveIsDHCP IsDhcp = $vm.LiveIsDHCP
StaticIp = $vm.LiveIpAddress StaticIp = $vm.LiveIpAddress
@@ -61,21 +74,22 @@ function Import-ZertoVmNicSetting {
PrimaryDns = $vm.LivePrimaryDns PrimaryDns = $vm.LivePrimaryDns
SecondaryDns = $vm.LiveSecondayDns SecondaryDns = $vm.LiveSecondayDns
} }
$nic.failover.Hypervisor.IpConfig = $IpConfig $nicSettings.failover.Hypervisor.IpConfig = $IpConfig
} elseif ($null -eq $nic.failover.Hypervisor.IpConfig -and $null -eq $vm.LiveIsDHCP -and $null -eq $vm.LiveIpAddress) { $nicSettings.failover.Hypervisor.DnsSuffix = $vm.LiveDnsSuffix
$nic.failover.Hypervisor.IpConfig = $null
} else {
$nic.failover.Hypervisor.IpConfig.IsDhcp = $vm.LiveIsDHCP
$nic.failover.Hypervisor.IpConfig.StaticIp = $vm.LiveIpAddress
$nic.failover.Hypervisor.IpConfig.SubnetMask = $vm.LiveIpSubnetMask
$nic.failover.Hypervisor.IpConfig.Gateway = $vm.LiveIpDefaultGateway
$nic.failover.Hypervisor.IpConfig.PrimaryDns = $vm.LivePrimaryDns
$nic.failover.Hypervisor.IpConfig.SecondaryDns = $vm.LiveSecondayDns
} }
$nic.failover.Hypervisor.DnsSuffix = $vm.LiveDnsSuffix $nicSettings.failoverTest.Hypervisor.NetworkIdentifier = $NetworkMap[$vm.TestNetwork]
$nic.failoverTest.Hypervisor.NetworkIdentifier = $NetworkMap[$vm.TestNetwork] $nicSettings.failoverTest.Hypervisor.ShouldReplaceMacAddress = $vm.TestShouldReplaceMac
$nic.failoverTest.Hypervisor.ShouldReplaceMacAddress = $vm.TestShouldReplaceMac if ($vm.TestIsDHCP -imatch "true" ) {
if ($null -eq $nic.failoverTest.Hypervisor.IpConfig -and ($null -ne $vm.TestIsDHCP -or $null -ne $vm.TestIpAddress)) { $IpConfig = [PsCustomObject]@{
IsDhcp = $vm.TestIsDHCP
PrimaryDns = $vm.TestPrimaryDns
SecondaryDns = $vm.TestSecondayDns
}
$nicSettings.failoverTest.Hypervisor.IpConfig = $IpConfig
$nicSettings.failoverTest.Hypervisor.DnsSuffix = $vm.TestDnsSuffix
} elseif (($vm.TestIsDHCP -imatch "false" -or
[string]::IsNullOrWhiteSpace($vm.TestIsDHCP)) -and
-not [string]::IsNullOrWhiteSpace($vm.TestIpAddress)) {
$IpConfig = [PsCustomObject]@{ $IpConfig = [PsCustomObject]@{
IsDhcp = $vm.TestIsDHCP IsDhcp = $vm.TestIsDHCP
StaticIp = $vm.TestIpAddress StaticIp = $vm.TestIpAddress
@@ -84,24 +98,15 @@ function Import-ZertoVmNicSetting {
PrimaryDns = $vm.TestPrimaryDns PrimaryDns = $vm.TestPrimaryDns
SecondaryDns = $vm.TestSecondayDns SecondaryDns = $vm.TestSecondayDns
} }
$nic.failoverTest.Hypervisor.IpConfig = $IpConfig $nicSettings.failoverTest.Hypervisor.IpConfig = $IpConfig
} elseif ($null -eq $nic.failoverTest.Hypervisor.IpConfig -and $null -eq $vm.TestIsDHCP -and $null -eq $vm.TestIpAddress) { $nicSettings.failoverTest.Hypervisor.DnsSuffix = $vm.TestDnsSuffix
$nic.failoverTest.Hypervisor.IpConfig = $null }
} else { Write-Verbose "Putting Updated Config for VM: $($vm.name), NIC: $($nic.nicidentifier) in VPG: $Vpg"
$nic.failoverTest.Hypervisor.IpConfig.IsDhcp = $vm.TestIsDHCP if ($PSCmdlet.ShouldProcess($vm.NicIdentifier, "Updating Nic")) {
$nic.failoverTest.Hypervisor.IpConfig.StaticIp = $vm.TestIpAddress Invoke-ZertoRestRequest -uri $NicUri -Method "PUT" -Body ($nicSettings | ConvertTo-Json -Depth 10) > $null
$nic.failoverTest.Hypervisor.IpConfig.SubnetMask = $vm.TestIpSubnetMask
$nic.failoverTest.Hypervisor.IpConfig.Gateway = $vm.TestIpDefaultGateway
$nic.failoverTest.Hypervisor.IpConfig.PrimaryDns = $vm.TestPrimaryDns
$nic.failoverTest.Hypervisor.IpConfig.SecondaryDns = $vm.TestSecondayDns
} }
$nic.failoverTest.Hypervisor.DnsSuffix = $vm.TestDnsSuffix
} }
} }
Write-Verbose "Putting Updated Config for VM: $($vm.vmname) in Vpg: $Vpg"
if ($PSCmdlet.ShouldProcess($vm.NicIdentifier, "Updating Nic")) {
Invoke-ZertoRestRequest -uri $uri -Method "PUT" -Body ($VmNicSettings | ConvertTo-Json -Depth 10) > $null
}
} }
} }
Write-Verbose "Saving updated configuration for VPG: $Vpg" Write-Verbose "Saving updated configuration for VPG: $Vpg"
+8 -2
View File
@@ -1,7 +1,7 @@
--- ---
external help file: ZertoApiWrapper-help.xml external help file: ZertoApiWrapper-help.xml
Module Name: ZertoApiWrapper Module Name: ZertoApiWrapper
online version: https://github.com/ZertoPublic/ZertoApiWrapper/blob/master/docs/Get-ZertoZsspSession.md online version: https://github.com/ZertoPublic/ZertoApiWrapper/blob/master/docs/Import-ZertoVmNicSetting.md
schema: 2.0.0 schema: 2.0.0
--- ---
@@ -19,7 +19,13 @@ Import-ZertoVmNicSetting [-InputFile] <String> [-WhatIf] [-Confirm] [<CommonPara
## DESCRIPTION ## DESCRIPTION
Using a CSV file, will import updated Live and Test network settings for protected virtual machines. This function will read the provided CSV file and only update VMs defined in the file. Using a CSV file, will import updated Live and Test network settings for protected virtual machines. This function will read the provided CSV file and only update VMs defined in the file.
It should be noted, due to current API limitations once a NIC is defined to update to either a Static IP address or use a DHCP address, it is not possible to remove that configuration via the API. Should you require this functionality, you will need to manually update via the GUI to set the option to "No" Each entry in the CSV will be for one VM and a single NIC attached to that VM. Each entry MUST contain the following information: VPG Name, VM Name, Network Adapter Name, Mac Address Update for both Live and Test, and Network Name for both Live and Test. All other information is optional, but will be applied based on the following logic:
* If 'IsDhcp' is set to 'TRUE', the NIC setting will be set to DHCP and Primary DNS, Secondary DNS, and Search Domain settings will be applied along with the mandatory fields.
* If 'IsDhcp' is set to 'FALSE' or not set and there is an IP Address defined, the IP Address, Subnet Mask, Default Gateway, Primary DNS, Secondary DNS, and Search Domain settings will be applied along with the mandatory fields.
* if 'IsDhcp' is set to 'FALSE' or not set and there is NOT an IP Address defined, the current NIC settings will be removed and ONLY the mandatory fields will be applied.
This logic is applied to both the Live and Test settings individually. It is recommended to use the `Export-ZertoVpgNicSetting` to dump all current settings and then modify a copy of the exported file with only the settings you wish to update. Import the copied file and if you need to revert, Import the original.
## EXAMPLES ## EXAMPLES