website about Regular Expressions

01.param(
02. $server="<hostIP>",
03. $password="pwd",
04. $file="VLANS.csv"
05.)
06. 
07.# preparing values
08.# BROKER & MGMT are
09.# always the same so can be specified explicitly
10.$portgrpexmatch = "BROKER","MGMT"
11. 
12.# match to be on the start and end of the string as array values
13.# and string match in the middle
14.# separated by comma
15.$portgrpsematch = @("START","_OP_END")
16. 
17.# match on the beginning to the Port Group name i.e. partial match
18.$portgrpptmatch = "START-P2P-"
19. 
20.$errorActionPreference = "SilentlyContinue"
21. 
22.<# old way loading Snapin
23.if ((Get-PSSnapin | Where {$_.Name -eq "VMware.VimAutomation.Core"}) -eq $null)
24.{ Write-Output "Importing VMware.VimAutomation.Core Snapin"
25. Add-PSSnapin VMware.VimAutomation.Core}
26.#>
27. 
28.if ((Get-Module | Where {$_.Name -eq "VMware.VimAutomation.Core"}) -eq $null)
29.{ Write-Output "Importing VMware.VimAutomation.Core Module ..."
30. Import-Module VMware.VimAutomation.Core}
31. 
32.$scriptPath = Split-Path $SCRIPT:MyInvocation.MyCommand.Path -Parent
33. 
34.# import csv file
35.$fname = $scriptPath + [char]92 + $file
36.$vlanFile = Import-CSV $fname
37. 
38.# connect to ESXi host
39.Write-Output "Connecting to host $server"
40.$svr = Connect-VIServer -Server $server -User root -Password $password
41.$vmHost = Get-VMHost -Server $svr
42. 
43.# Create Portgroups with default security settings from CSV file
44.Write-Output "Creating PortGroups with default security settings ..."
45.$vlanfile | % { Get-VirtualSwitch -VMHost $vmHost -Name $_.vSwitch | New-VirtualPortGroup -Name $_.vlanName -VLanId $_.vlanID }
46. # Write-Output "PortGroup $($_.vlanname) with VLAN ID $($_.vlanID) has been created on Virtual Switch $($_.vSwitch)" }
47.Write-Output "PortGroups were successfully created"
48.Write-Output ""
49. 
50.Write-Output "Updating SecurityPolicy on selected Port Groups ..."
51.#updating SecurityPolicy - explicit match
52.$portgrpexmatch | % {
53.Write-Output "Updating SecurityPolicy on $_ Port Group ..."
54.Get-VirtualPortGroup -Name $_ | Get-SecurityPolicy | Set-SecurityPolicy -AllowPromiscuous $true -MacChanges $true -ForgedTransmits $true }
55. 
56.#updating SecurityPolicy - match on the start and end of the string
57.$portgrpset = $vlanFile | where {($_.vlanname -match ('^'+$portgrpsematch[0]+'[A-B]'+$portgrpsematch[1]+'$'))}
58.$portgrpset | % {
59.Write-Output "Updating VLAN SecurityPolicy on $($_.vlanname) Port Group ..."
60.Get-VirtualPortGroup -Name $_.vlanname | Get-SecurityPolicy | Set-SecurityPolicy -AllowPromiscuous $true -MacChanges $true -ForgedTransmits $true }
61. 
62.#updating SecurityPolicy - partial match from the begining of string
63.$portgrpset = $vlanFile | where {($_.vlanname -match '^'+$portgrpptmatch )} #+ '$')}
64.$portgrpset | % {
65.Write-Output "Updating P2P VLAN SecurityPolicy on $($_.vlanname) Port Group ..."
66.Get-VirtualPortGroup -Name $_.vlanname | Get-SecurityPolicy | Set-SecurityPolicy -AllowPromiscuous $true -MacChanges $true -ForgedTransmits $true }
67. 
68.Write-Output "Updating default portgroups ..."
69.Write-Output "Removing VM Network portgroup ..."
70.Get-VirtualPortGroup -Name "VM Network" | Remove-VirtualPortGroup
71.Write-Output "Renaming Management Network portgroup to Management Kernel ..."
72.Get-VirtualPortGroup -Name "Management Network" | set-VirtualPortGroup -Name "Management Kernel"
73. 
74.# close connection to ESXi host
75.Write-Output "Disconnecting from host $server ..."
76.Disconnect-VIServer -Server $server -Force -Confirm:$false


VLAN.csv file
vswitch,vlanname,vlanid
vswitch0,MGMT,2
vswitch0,BROKER,10
.