Powershell script to create vsphere distributed virtual switch port groups

I used the following Powershell Powercli script to create a bunch of DVS Port Groups on a VSAN vSphere 6 cluster.  Using the command line speeds things up and eliminates minimizes the impact of human error – no more mouse clicking.
Based on the work from the following blog post – I adapted it for our environment:
Creating consistent Distributed Port Groups with PowerCLI  
I recently had to create a new vDS to replicate a standard vSwitch from another vCenter install. I wanted to create my vDS Distribute Port Groups (DPG) simply, but consistently. As I have a low number of DPGs to create, I could probably have done this manually, but scripting the creation ensures consistency. Plus, it’s a subset of PowerCLI that I wanted to familiarize myself with.
First, I created a vDS and a reference DPG through the vSphere Web Client. You can do this with PowerCLI, but you have to go down the rabbit hole of Views to touch some of the advanced settings, something that’s not well documented and would have been very time consuming for me to explore. I also didn’t mind creating the initial vDS and DPG as the visual view of the Web Client made it easy for me to verify the settings whereas a long string of PowerShell (PoSH) would have been a little more difficult to interpret.
 
 
————————-
Here is my script… it created 41 port groups with the name “VM Network 3500” and so on…
Iben Rodriguez – 2015
$vDS = “SNSJ-DSwitch”
$Reference_DPG = “DPortGroup”
$DPGs = @{
     “3500”  = “VM Network”;
     “3501”  = “VM Network”;
     “3502”  = “VM Network”;
     “3503”  = “VM Network”;
     “3504”  = “VM Network”;
     “3505”  = “VM Network”;
     “3506”  = “VM Network”;
     “3507”  = “VM Network”;
     “3508”  = “VM Network”;
     “3509”  = “VM Network”;
     “3510”  = “VM Network”;
     “3511”  = “VM Network”;
     “3512”  = “VM Network”;
     “3513”  = “VM Network”;
     “3514”  = “VM Network”;
     “3515”  = “VM Network”;
     “3516”  = “VM Network”;
     “3517”  = “VM Network”;
     “3518”  = “VM Network”;
     “3519”  = “VM Network”;
     “3520”  = “VM Network”;
     “3521”  = “VM Network”;
     “3522”  = “VM Network”;
     “3523”  = “VM Network”;
     “3524”  = “VM Network”;
     “3525”  = “VM Network”;
     “3526”  = “VM Network”;
     “3527”  = “VM Network”;
     “3528”  = “VM Network”;
     “3529”  = “VM Network”;
     “3530”  = “VM Network”;
     “3531”  = “VM Network”;
     “3532”  = “VM Network”;
     “3533”  = “VM Network”;
     “3534”  = “VM Network”;
     “3535”  = “VM Network”;
     “3536”  = “VM Network”;
     “3537”  = “VM Network”;
     “3538”  = “VM Network”;
     “3539”  = “VM Network”;
     “3540”  = “VM Network”;     
};
$DPGs.Keys | % {
     $VLAN = $_
     $DPG = $DPGs.Item($_) + ” ” + $VLAN
     New-VDPortgroup -VDSwitch $vDS -Name $DPG -ReferencePortgroup $Reference_DPG
     Set-VDPortgroup -VDPortgroup $DPG -VlanId $VLAN
}
 
Advertisements

PowerShell Bitbucket PowerGUI Text Binary Encoding UTF-8 with SourceTree

How to setup Dell Quest PowerGUI to edit PowerShell PS1 scripts to Bitbucket with the correct UTF-8 Text encoding to overcome the erroneous Binary Encoding

We use the following workflow to update Microsoft PowerShell (VMware PowerCLI actually) scripts in the Atlassian Bitbucket GIT repository and had this same issue when getting started out of the box with defaults.

Workflow

The default files created with PowerGui are having the 3 letter file name extension ps1 but when uploaded with Sourcetree they show up in Bitbucket as Binary so text features like edit or diff don’t work. Based on the feedback here and with other google searches the fix for this was to indeed change the encoding to UTF-8, make a simple change to the file, and upload it again. After a few tries this works perfectly and is repeatable. There is a trick though as PowerGUI doesn’t make it obvious how to change the encoding. Here are the steps.

Procedure

  1. Start powergui as you normally would. BTW – this is a windows program and I’ve tested this on Windows 8
  2. Choose “PowerShell Libraries” from the File menu
  3. Check the box to enable “Add-on.ScriptEditorEssentials” from the Snapins/Modules dialog box. Click OK.
  4. Now Enable UTF-8 Encoding from the File menu.
  5. That’s it. Any file you save now will be UTF-8 encoded and show up properly in Bitbucket once uploaded.

Reference:

http://en.community.dell.com/techcenter/powergui/f/4833/t/19575613.aspx


 

Image

Image

 

 

 

Image

Image

Image

 

Image

 

Image


 

powershell, powergui, powercli, text, binary, sourcetree, bitbucket