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
* 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
$networkMap = Get-Map -InputObject $networks -key "NetworkIdentifier" -value "VirtualizationNetworkName"
foreach ($vm in $vmSettings) {
$nicInfo = [PSCustomObject]@{
VPGName = $group.VPGName
VMName = $vmMap[$($vm.vmIdentifier)]
NicIdentifier = $vm.nics.NicIdentifier
LiveNetwork = $networkMap[$vm.nics.failover.Hypervisor.NetworkIdentifier]
LiveShouldReplaceMac = $vm.nics.failover.Hypervisor.ShouldReplaceMacAddress
LiveIsDHCP = $vm.Nics.failover.Hypervisor.IpConfig.IsDhcp
LiveIpAddress = $vm.nics.failover.Hypervisor.IpConfig.StaticIp
LiveIpSubnetMask = $vm.nics.failover.Hypervisor.IpConfig.SubnetMask
LiveIpDefaultGateway = $vm.nics.failover.Hypervisor.IpConfig.Gateway
LivePrimaryDns = $vm.nics.failover.Hypervisor.IpConfig.PrimaryDns
LiveSecondayDns = $vm.nics.failover.Hypervisor.IpConfig.SecondaryDns
LiveDnsSuffix = $vm.nics.failover.Hypervisor.DnsSuffix
TestNetwork = $networkMap[$vm.nics.failoverTest.Hypervisor.NetworkIdentifier]
TestShouldReplaceMac = $vm.nics.failoverTest.Hypervisor.ShouldReplaceMacAddress
TestIsDHCP = $vm.Nics.failoverTest.Hypervisor.IpConfig.IsDhcp
TestIpAddress = $vm.nics.failoverTest.Hypervisor.IpConfig.StaticIp
TestIpSubnetMask = $vm.nics.failoverTest.Hypervisor.IpConfig.SubnetMask
TestIpDefaultGateway = $vm.nics.failoverTest.Hypervisor.IpConfig.Gateway
TestPrimaryDns = $vm.nics.failoverTest.Hypervisor.IpConfig.PrimaryDns
TestSecondayDns = $vm.nics.failoverTest.Hypervisor.IpConfig.SecondaryDns
TestDnsSuffix = $vm.nics.failoverTest.Hypervisor.DnsSuffix
if ($vm.nics.count -gt 0) {
foreach ($nic in $vm.nics) {
$nicInfo = [PSCustomObject]@{
VPGName = $group.VPGName
VMName = $vmMap[$vm.vmIdentifier]
NicIdentifier = $nic.NicIdentifier
LiveNetwork = $networkMap[$nic.failover.Hypervisor.NetworkIdentifier]
LiveShouldReplaceMac = $nic.failover.Hypervisor.ShouldReplaceMacAddress
LiveIsDHCP = $nic.failover.Hypervisor.IpConfig.IsDhcp
LiveIpAddress = $nic.failover.Hypervisor.IpConfig.StaticIp
LiveIpSubnetMask = $nic.failover.Hypervisor.IpConfig.SubnetMask
LiveIpDefaultGateway = $nic.failover.Hypervisor.IpConfig.Gateway
LivePrimaryDns = $nic.failover.Hypervisor.IpConfig.PrimaryDns
LiveSecondayDns = $nic.failover.Hypervisor.IpConfig.SecondaryDns
LiveDnsSuffix = $nic.failover.Hypervisor.DnsSuffix
TestNetwork = $networkMap[$nic.failoverTest.Hypervisor.NetworkIdentifier]
TestShouldReplaceMac = $nic.failoverTest.Hypervisor.ShouldReplaceMacAddress
TestIsDHCP = $nic.failoverTest.Hypervisor.IpConfig.IsDhcp
TestIpAddress = $nic.failoverTest.Hypervisor.IpConfig.StaticIp
TestIpSubnetMask = $nic.failoverTest.Hypervisor.IpConfig.SubnetMask
TestIpDefaultGateway = $nic.failoverTest.Hypervisor.IpConfig.Gateway
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 {
@@ -44,15 +44,28 @@ function Import-ZertoVmNicSetting {
[string]::IsNullOrWhiteSpace($Vm.LiveShouldReplaceMac) -or
[string]::IsNullOrWhiteSpace($Vm.TestNetwork) -or
[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 {
$uri = "vpgSettings/{0}/vms/{1}" -f $vpgSettingsId, $vmMap[$vm.VMName]
$VmNicSettings = Get-ZertoVpgSetting -vpgSettingsIdentifier $vpgSettingsId -vmIdentifier $vmMap[$vm.VMName]
foreach ($nic in $VmNicSettings.nics) {
if ($nic.NicIdentifier -eq $vm.NicIdentifier) {
$nic.failover.Hypervisor.NetworkIdentifier = $NetworkMap[$vm.LiveNetwork]
$nic.failover.Hypervisor.ShouldReplaceMacAddress = $vm.LiveShouldReplaceMac
if ($null -eq $nic.failover.Hypervisor.IpConfig -and ($null -ne $vm.LiveIsDHCP -or $null -ne $vm.LiveIpAddress)) {
$NicUri = "{0}/nics/{1}" -f $uri, $nic.NicIdentifier
Invoke-ZertoRestRequest -uri $NicUri -Method "DELETE" > $null
$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]@{
IsDhcp = $vm.LiveIsDHCP
StaticIp = $vm.LiveIpAddress
@@ -61,21 +74,22 @@ function Import-ZertoVmNicSetting {
PrimaryDns = $vm.LivePrimaryDns
SecondaryDns = $vm.LiveSecondayDns
}
$nic.failover.Hypervisor.IpConfig = $IpConfig
} elseif ($null -eq $nic.failover.Hypervisor.IpConfig -and $null -eq $vm.LiveIsDHCP -and $null -eq $vm.LiveIpAddress) {
$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
$nicSettings.failover.Hypervisor.IpConfig = $IpConfig
$nicSettings.failover.Hypervisor.DnsSuffix = $vm.LiveDnsSuffix
}
$nic.failover.Hypervisor.DnsSuffix = $vm.LiveDnsSuffix
$nic.failoverTest.Hypervisor.NetworkIdentifier = $NetworkMap[$vm.TestNetwork]
$nic.failoverTest.Hypervisor.ShouldReplaceMacAddress = $vm.TestShouldReplaceMac
if ($null -eq $nic.failoverTest.Hypervisor.IpConfig -and ($null -ne $vm.TestIsDHCP -or $null -ne $vm.TestIpAddress)) {
$nicSettings.failoverTest.Hypervisor.NetworkIdentifier = $NetworkMap[$vm.TestNetwork]
$nicSettings.failoverTest.Hypervisor.ShouldReplaceMacAddress = $vm.TestShouldReplaceMac
if ($vm.TestIsDHCP -imatch "true" ) {
$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]@{
IsDhcp = $vm.TestIsDHCP
StaticIp = $vm.TestIpAddress
@@ -84,24 +98,15 @@ function Import-ZertoVmNicSetting {
PrimaryDns = $vm.TestPrimaryDns
SecondaryDns = $vm.TestSecondayDns
}
$nic.failoverTest.Hypervisor.IpConfig = $IpConfig
} elseif ($null -eq $nic.failoverTest.Hypervisor.IpConfig -and $null -eq $vm.TestIsDHCP -and $null -eq $vm.TestIpAddress) {
$nic.failoverTest.Hypervisor.IpConfig = $null
} else {
$nic.failoverTest.Hypervisor.IpConfig.IsDhcp = $vm.TestIsDHCP
$nic.failoverTest.Hypervisor.IpConfig.StaticIp = $vm.TestIpAddress
$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
$nicSettings.failoverTest.Hypervisor.IpConfig = $IpConfig
$nicSettings.failoverTest.Hypervisor.DnsSuffix = $vm.TestDnsSuffix
}
Write-Verbose "Putting Updated Config for VM: $($vm.name), NIC: $($nic.nicidentifier) in VPG: $Vpg"
if ($PSCmdlet.ShouldProcess($vm.NicIdentifier, "Updating Nic")) {
Invoke-ZertoRestRequest -uri $NicUri -Method "PUT" -Body ($nicSettings | ConvertTo-Json -Depth 10) > $null
}
$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"
+8 -2
View File
@@ -1,7 +1,7 @@
---
external help file: ZertoApiWrapper-help.xml
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
---
@@ -19,7 +19,13 @@ Import-ZertoVmNicSetting [-InputFile] <String> [-WhatIf] [-Confirm] [<CommonPara
## 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.
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