Skip to content

Instantly share code, notes, and snippets.

@GodfatherX64
Forked from 9to5IT/Duplicate-PortGroups.ps1
Created August 16, 2021 12:51
Show Gist options
  • Save GodfatherX64/b23f02b4fcc11ae9abf6243fa3aa129b to your computer and use it in GitHub Desktop.
Save GodfatherX64/b23f02b4fcc11ae9abf6243fa3aa129b to your computer and use it in GitHub Desktop.
PowerShell: Duplicate Port Groups from one vDS to another using PowerCLI
#requires -version 3
<#
.SYNOPSIS
Copies port groups and all of their settings from one vDS to another vDS
.DESCRIPTION
This script is used to duplicate port groups (and their settings) from one vDS to another vDS.
This is useful if you want to create a duplicate vDS with the same configuration as the original
.PARAMETER
None
.INPUTS
Input.csv file. This file contains the list of port groups you want to duplicate and their new name (as you cannot have two port groups with the same name even in different vDS)
.OUTPUTS
Log file stored in C:\Windows\Temp\Duplicate-PortGroups.log
.NOTES
Version: 1.0
Author: Luca Sturlese
Creation Date: 13.01.2015
Purpose/Change: Initial script development
.EXAMPLE
Duplicate-PortGroups.ps1
#>
#---------------------------------------------------------[Initialisations]--------------------------------------------------------
#Set Error Action to Silently Continue
$ErrorActionPreference = "SilentlyContinue"
#Dot Source required Function Libraries
. "C:\Scripts\Functions\Logging_Functions.ps1"
#Add VMware PowerCLI Snap-Ins
Add-PSSnapin VMware.VimAutomation.Core
#----------------------------------------------------------[Declarations]----------------------------------------------------------
#Script Version
$sScriptVersion = "1.0"
#Log File Info
$sLogPath = "C:\Windows\Temp"
$sLogName = "Duplicate-PortGroups.log"
$sLogFile = Join-Path -Path $sLogPath -ChildPath $sLogName
#Global Variables
$Global:Data
$Global:dvNewName
#-----------------------------------------------------------[Functions]------------------------------------------------------------
Function Connect-VMwareServer{
Param([Parameter(Mandatory=$true)][string]$VMServer)
Begin{
Log-Write -LogPath $sLogFile -LineValue "Connecting to VMware environment [$VMServer]..."
}
Process{
Try{
$oCred = Get-Credential -Message "Enter credentials to connect to vSphere Server or Host"
Connect-VIServer -Server $VMServer -Credential $oCred
}
Catch{
Log-Error -LogPath $sLogFile -ErrorDesc $_.Exception -ExitGracefully $True
Break
}
}
End{
If($?){
Log-Write -LogPath $sLogFile -LineValue "Completed Successfully."
Log-Write -LogPath $sLogFile -LineValue " "
}
}
}
Function Read-Input{
Param()
Begin{
Log-Write -LogPath $sLogFile -LineValue "Reading csv to collect required data..."
}
Process{
Try{
#Collect info from CSV file
$FilePath = Join-Path -Path $PSScriptRoot -ChildPath Input.csv
$Global:Data = Import-Csv -Path $FilePath
#Collect new dvSwitch name
$Global:dvNewName = Read-Host "What is the name of the new vDS?"
}
Catch{
Log-Error -LogPath $sLogFile -ErrorDesc $_.Exception -ExitGracefully $True
Break
}
}
End{
If($?){
Log-Write -LogPath $sLogFile -LineValue "Completed Successfully."
Log-Write -LogPath $sLogFile -LineValue " "
}
}
}
Function Create-PortGroup{
Param()
Begin{
Log-Write -LogPath $sLogFile -LineValue "Duplicating port groups..."
}
Process{
Try{
ForEach($Row in $Global:Data){
$PG = Get-VDPortgroup -Name $Row.orig
Get-VDSwitch -Name $Global:dvNewName | New-VDPortgroup -Name $Row.new -ReferencePortgroup $PG
Log-Write -LogPath $sLogFile -LineValue " $($Row.new) - Created"
}
}
Catch{
Log-Error -LogPath $sLogFile -ErrorDesc $_.Exception -ExitGracefully $False
}
}
End{
If($?){
Log-Write -LogPath $sLogFile -LineValue "Completed Successfully."
Log-Write -LogPath $sLogFile -LineValue " "
}
}
}
#-----------------------------------------------------------[Execution]------------------------------------------------------------
Log-Start -LogPath $sLogPath -LogName $sLogName -ScriptVersion $sScriptVersion
$Server = Read-Host "Specify the vCenter Server to connect to (IP or FQDN)?"
Connect-VMwareServer -VMServer $Server
Read-Input
Create-PortGroup
Log-Finish -LogPath $sLogFile
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment