Adding scripts

This commit is contained in:
ackitsme committed 2026-07-24 17:35:40 -07:00
1 parent e78eed48b6
commit fc7cfeb008
10 files changed
+2321

No files matched your search

+226
View File
@@ -0,0 +1,226 @@
param (
[string]$vCenter = "",
[string]$dvSwitch = "",
[string]$CSVFile = "",
[switch]$SaveCred = $false,
[switch]$Help = $false
)
# Variables
$fileVersion = "1.0.0"
$fileDate = "2026-01-28"
# Display Script Banner
Write-Host "--------------------------------------------------" -ForegroundColor White
Write-Host "dvSwitch Port Group Creation Script" -ForegroundColor Green
Write-Host "Version $($FileVersion) ($($FileDate))" -ForegroundColor Green
Write-Host "Written by Charlie Silverman" -ForegroundColor Green
Write-Host "--------------------------------------------------" -ForegroundColor White
if ($Help) {
Write-Host
Write-Host "Purpose: Create dvSwitch Port Groups en masse."
Write-Host "Command Syntax:"
Write-Host " CreatedvSwitchPortGroups.ps1 (-vcenter [vcenter host name]) (-dvSwitch ""[dvswitch]"") (-CSVFile ""[csvfile]"")"
Write-Host " (-savecred) (-help)`n" -foregroundcolor green
Write-Host "Command Line Options:"
Write-Host
Write-Host -NoNewLine " -vCenter " -foregroundcolor Green
Write-Host "- The FQDN or IP address of a vCenter server."
Write-Host -NoNewLine " -dvSwitch " -foregroundcolor Green
Write-Host "- The name of the distributed virtual switch in vCenter."
Write-Host -NoNewLine " -CSVfile " -foregroundcolor Green
Write-Host "- The path to the CSV file. See note below for format."
Write-Host -NoNewLine " -savecred " -foregroundcolor Green
Write-Host "- Configure and store vCenter server credentials. This will only store the credentials under"
Write-Host " the current logged in user."
Write-Host -NoNewLine " -help " -foregroundcolor Green
Write-Host "- Display command help."
Write-Host
Write-Host "If any of the information is not provided, you will be prompted. If a credential has been previously stored, it will"
Write-Host "be used again unless the -savecred option is specified."
Write-Host
Write-Host "The CSV file must follow the format listed below. The only field required is PortGroup. Fields can be in any order."
Write-Host " PortGroup, VLAN, PortBinding, NumPorts, ActiveUplink, DisabledUplink, StandbyUplink, Notes"
Write-Host "Where:"
Write-Host " Portgroup " -ForegroundColor Green -NoNewLine
Write-Host " - The name of the port group."
Write-Host " VLAN " -ForegroundColor Green -NoNewLine
Write-Host " - [Optional] The VLAN tag for the port group."
Write-Host " PortBinding " -ForegroundColor Green -NoNewLine
Write-Host " - [Optional] The type of port binding for the port group. This can be either Static or Dynamic."
Write-Host " Defaults to Static if not specified."
Write-Host " NumPorts " -ForegroundColor Green -NoNewLine
Write-Host " - [Optional] The number of ports for the port group. This is ignored for a dynamic port group."
Write-Host " Defaults to 254 if not specified."
Write-Host " ActiveUplink " -ForegroundColor Green -NoNewLine
Write-Host " - [Optional] A comma delimited list of switch uplinks to be set as active for the port group."
Write-Host " StandbyUplink " -ForegroundColor Green -NoNewLine
Write-Host " - [Optional] A comma delimited list of switch uplinks to be set as standby for the port group."
Write-Host " UnusedUplink " -ForegroundColor Green -NoNewLine
Write-Host " - [Optional] A comma delimited list of switch uplinks to be set as unused for the port group."
Write-Host " Notes " -ForegroundColor Green -NoNewLine
Write-Host " - [Optional] Notes to add to the port group."
exit
}
$quiet = Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false
if ($vCenter -eq "") {
$vCenter = Read-Host "Enter name of vCenter Server"
}
if ($SaveCred) {
$credential = Get-Credential -Message "Enter credentials for vCenter server $vCenter"
$credential | Export-Clixml -path $env:USERPROFILE\kistools-$vcenter.cred
Write-Host "Saving credentials."
}
if (Test-Path -path $env:USERPROFILE\kistools-$vcenter.cred -PathType Leaf) {
$credentials = Import-Clixml -path $env:USERPROFILE\kistools-$vcenter.cred
Write-Host "Using stored credentials"
} else {
$credentials = Get-Credential -Message "Enter credentials for vCenter server $vCenter"
if (!$credentials) {
Write-Host "ERROR: No credentials were entered" -foregroundcolor yellow
}
}
try {
Write-Host -NoNewLine "Connecting to vCenter $vcenter..."
$vcconn = Connect-VIServer $vcenter -Credential $credentials -ErrorAction Stop
Write-Host " Success" -ForegroundColor green
} catch {
Write-Host " Failure" -ForegroundColor Yellow -BackgroundColor Red
exit
}
try {
if ($dvSwitch -ne "") {
get-VDswitch $dvSwitch -ErrorAction Stop
}
} catch {
Write-Host "ERROR: dvSwitch ""$dvSwitch"" is not found." -ForegroundColor Red
$dvSwitch = ""
}
if ($dvSwitch -eq "") {
$dvSwitchlist = Get-VDSwitch | Sort-Object
While (($dvSwitch -eq "") -or (-not $dvSwitch)) {
Write-Host "`ndvSwitch Selection:`n"
for ($x = 0; $x -lt $dvSwitchlist.length; $x++) {
Write-Host "$(([string]($x+1)).PadLeft(3,' '))" -NoNewLine -ForegroundColor Green
Write-Host ": " -NoNewLine -ForegroundColor Gray
Write-Host "$($DVSwitchlist[$x].name)" -ForegroundColor Yellow
}
$dvSwitchNum = [int](Read-Host "`nSelect dvSwitch Number") - 1
$dvSwitch = $dvSwitchlist[$dvSwitchNum]
}
$mySwitch = get-VDSwitch $dvSwitch
} else {
$mySwitch = get-VDSwitch $dvSwitch
}
$csvdata = Import-CSV $csvfile
Write-Host "Processing Port Group List"
for ($x = 0; $x -lt $csvdata.length; $x++) {
$pgname = $csvdata[$x].PortGroup
if ($pgname -eq "") { continue }
if (-not ($pgvlan = $csvdata[$x].VLAN)) {
$pgvlan = 0
}
if (-not ($pgbinding = $csvdata[$x].PortBinding)) {
$pgbinding = "Static"
}
if (-not ($pgports = $csvdata[$x].NumPorts)) {
$pgports = 254
}
if ($pgactive -and (($pgactive = ($csvdata[$x].ActiveUplink).split(",")).count -eq 0)) {
$pgactive = @()
}
if ($pgstandby -and (($pgstandby = ($csvdata[$x].StandbyUplink).split(",")).count -eq 0)) {
$pgstandby = @()
}
if ($pgunused -and (($pgunused = ($csvdata[$x].UnusedUplink).split(",")).count -eq 0)) {
$pgunused = @()
}
Write-Host "$pgname... " -NoNewLine
if ($temp = Get-VdPortGroup $pgname -erroraction silentlycontinue) {
Write-Host " Already Exists - Skipping" -ForegroundColor Yellow
continue
}
try {
$newpg = New-VdPortGroup -name "$pgname" -vlanid $pgvlan -PortBinding $pgbinding -VdSwitch "$dvSwitch" -erroraction stop
Write-Host "Created Successfully" -ForegroundColor Green
} catch {
Write-Host " ERROR - Unable to Create Port Group $pgname on dvSwitch $dvSwitch." -ForegroundColor Red
Write-Host "$($_.Exception.Message)" -ForegroundColor Gray
continue
}
$myuplinks = @()
foreach($uplink in $pgactive) {
if ($uplink -eq "") { continue }
if (($mySwitch.ExtensionData.Config.UplinkPortPolicy.UplinkPortName | Where-Object {$_ -eq $uplink}).Count -eq 0) {
Write-Host "Warning: Uplink ""$uplink"" for Active is not found on switch $dvSwitch" -ForegroundColor Yellow
continue
}
$myuplinks += $uplink
}
if ($myuplinks.Count -gt 0) {
Write-Host " Assigning active uplink ports... " -NoNewLine
try {
$temp = $newpg | Get-VDUplinkTeamingPolicy | Set-VDUplinkTeamingPolicy -ActiveUplinkPort $myuplinks -ErrorAction Stop
Write-Host "Success" -ForegroundColor Green
} catch {
Write-Host "ERROR assigning uplinks to $pgname" -ForegroundColor Red
Write-Host "$($_.Exception.Message)" -ForegroundColor Gray
}
}
$myuplinks = @()
foreach($uplink in $pgstandby) {
if ($uplink -eq "") { continue }
if (($mySwitch.ExtensionData.Config.UplinkPortPolicy.UplinkPortName | Where-Object {$_ -eq $uplink}).Count -eq 0) {
Write-Host "Warning: Uplink ""$uplink"" for STANDBY is not found on switch $dvSwitch" -ForegroundColor Yellow
continue
}
$myuplinks += $uplink
}
if ($myuplinks.Count -gt 0) {
Write-Host " Assigning standby uplink ports... " -NoNewLine
try {
$temp = $newpg | Get-VDUplinkTeamingPolicy | Set-VDUplinkTeamingPolicy -StandbyUplinkPort $myuplinks -ErrorAction Stop
Write-Host "Success" -ForegroundColor Green
} catch {
Write-Host "ERROR assigning standby uplinks to $pgname" -ForegroundColor Red
Write-Host "$($_.Exception.Message)" -ForegroundColor Gray
}
}
$myuplinks = @()
foreach($uplink in $pgunused) {
if ($uplink -eq "") { continue }
if (($mySwitch.ExtensionData.Config.UplinkPortPolicy.UplinkPortName | Where-Object {$_ -eq $uplink}).Count -eq 0) {
Write-Host "Warning: Uplink ""$uplink"" for UNUSED is not found on switch $dvSwitch" -ForegroundColor Yellow
continue
}
$myuplinks += $uplink
}
if ($myuplinks.Count -gt 0) {
Write-Host " Assigning unused uplink ports... " -NoNewLine
try {
$temp = $newpg | Get-VDUplinkTeamingPolicy | Set-VDUplinkTeamingPolicy -UnusedUplinkPort $myuplinks -ErrorAction Stop
Write-Host "Success" -ForegroundColor Green
} catch {
Write-Host "ERROR assigning unused uplinks to $pgname" -ForegroundColor Red
Write-Host "$($_.Exception.Message)" -ForegroundColor Gray
}
}
}