Create function and required associated docs

Export-ZertoVmNicSetting
This commit is contained in:
Wes Carroll
2019-07-08 13:22:18 -04:00
parent 1fd92008ac
commit ef1584032e
3 changed files with 206 additions and 0 deletions
@@ -0,0 +1,42 @@
#Requires -Modules Pester
#Region - Test Setup
$moduleFileName = "ZertoApiWrapper.psd1"
$here = (Split-Path -Parent $MyInvocation.MyCommand.Path).Replace("Tests", "ZertoApiWrapper")
$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".")
$file = Get-ChildItem "$here\$sut"
$modulePath = $here -replace "Public", ""
$moduleFile = Get-ChildItem "$modulePath\$moduleFileName"
Get-Module -Name ZertoApiWrapper | Remove-Module -Force
Import-Module $moduleFile -Force
#EndRegion
Describe $file.BaseName -Tag 'Unit' {
It "is valid Powershell (Has no script errors)" {
$contents = Get-Content -Path $file -ErrorAction Stop
$errors = $null
$null = [System.Management.Automation.PSParser]::Tokenize($contents, [ref]$errors)
$errors | Should -HaveCount 0
}
Context "$($file.BaseName)::Parameter Unit Tests" {
It "has a mantatory string parameter for the output file" {
Get-Command $file.BaseName | Should -HaveParameter OutputFile -Type String -Mandatory
}
It "has a non-mandatory string array parameter for vpgName(s) to export" {
Get-Command $file.BaseName | Should -HaveParameter vpgName -Type String[]
Get-Command $file.BaseName | Should -HaveParameter vpgName -Not -Mandatory
}
It "Output File does not take null or empty string" {
{ Export-ZertoVpg -outputFile "" } | Should -Throw
{ Export-ZertoVpg -outputFile $null } | Should -Throw
}
It "Vpg Name parameter does not take null or empty string" {
{ Export-ZertoVpg -outputFile ".\ExportedInfo.csv" -vpgName = "" } | Should -Throw
{ Export-ZertoVpg -outputFile ".\ExportedInfo.csv" -vpgName = $null } | Should -Throw
}
}
}
@@ -0,0 +1,85 @@
<# .ExternalHelp ./en-us/ZertoApiWrapper-help.xml #>
function Export-ZertoVmNicSetting {
[CmdletBinding()]
param (
# Vpg(s) to export. If no VPG was named, all data will be exported.
[Parameter(
Helpmessage = "VPG(s) to export"
)]
[ValidateNotNullOrEmpty()]
[string[]]
$VpgName,
# Output file information
[Parameter(
Helpmessage = "File to export the data to. This MUST be a CSV. If a CSV filename is not specified, the file will be forced into a CSV",
Mandatory
)]
[ValidateNotNullOrEmpty()]
[string]
$OutputFile
)
begin {
}
process {
if (($OutputFile.Split('.')[-1]) -ne 'csv') {
$OutputFile += '.csv'
}
if ( "VpgName" -in $PSBoundParameters.Keys ) {
$vpgs = Get-ZertoVpg | Where-Object { $_.VpgName -in $VpgName }
foreach ($group in $VpgName) {
if ($group -notin $vpgs.VpgName) {
Write-Error "$group VPG not found. Skipping." -ErrorAction Continue
}
}
} else {
$vpgs = Get-ZertoVpg
}
$nicSettings = foreach ($group in $vpgs) {
$protectedVms = Get-ZertoProtectedVm -vpgName ($group.vpgname)
$vmMap = @{ }
foreach ($vm in $protectedVms) {
$vmMap["$($vm.vmIdentifier)"] = $vm.vmName
}
$settingsId = New-ZertoVpgSettingsIdentifier -vpgIdentifier $group.vpgIdentifier
$vmSettings = Get-ZertoVpgSetting -vpgSettingsIdentifier $settingsId -vms
$networks = Get-ZertoVirtualizationSite -siteIdentifier $group.RecoverySite.identifier -networks
$null = Remove-ZertoVpgSettingsIdentifier -vpgSettingsIdentifier $settingsId
$networkMap = @{ }
foreach ($network in $networks) {
$networkMap[$network.NetworkIdentifier] = $network.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
}
$nicInfo
}
}
$nicSettings | ConvertTo-Csv | Out-File $OutputFile
}
end {
}
}
+79
View File
@@ -0,0 +1,79 @@
---
external help file: ZertoApiWrapper-help.xml
Module Name: ZertoApiWrapper
online version: https://github.com/ZertoPublic/ZertoApiWrapper/blob/Master/docs/Export-ZertoVmNicSettings.md
schema: 2.0.0
---
# Export-ZertoVmNicSetting
## SYNOPSIS
Queries VPGs and associated Virtual Machines to export all protected virtual machine NIC settings to a Comma Separated Value (CSV) file.
## SYNTAX
```
Export-ZertoVmNicSetting [[-VpgName] <String[]>] [-OutputFile] <String> [<CommonParameters>]
```
## DESCRIPTION
Queries VPGs and associated Virtual Machines to export all protected virtual machine NIC settings to a Comma Separated Value (CSV) file. This file can be used with the Import-ZertoVmNicSetting function to bulk update.
## EXAMPLES
### Example 1
```powershell
PS C:\> Export-ZertoVmNicSetting -OutputFile "C:\ZertoInfo\VMNicSettings.csv"
```
Exports VM Nic Settings for ALL Virtual Protection Groups to a file located at "C:\ZertoInfo\VMNicSettings.csv"
### Example 2
```powershell
PS C:\> Export-ZertoVmNicSetting -OutputFile "C:\ZertoInfo\VMNicSettings.csv" -VpgName "Exchange", "Support Forum"
```
Exports VM Nic Settings for Exchange and Support Forum Virtual Protection Groups to a file located at "C:\ZertoInfo\VMNicSettings.csv"
## PARAMETERS
### -OutputFile
Output file information
```yaml
Type: String
Parameter Sets: (All)
Aliases:
Required: True
Position: 2
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
```
### -VpgName
Vpg(s) to export. If no VPG was named, all data will be exported.
```yaml
Type: String[]
Parameter Sets: (All)
Aliases:
Required: False
Position: 1
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
```
### CommonParameters
This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see [about_CommonParameters](http://go.microsoft.com/fwlink/?LinkID=113216).
## INPUTS
## OUTPUTS
## NOTES
## RELATED LINKS