PowerCLI: vProfiles

Following my recent post, Easy vSwitch & PortGroup Setup I thought I would give PrimalForms a go and create a quick GUI, PrimalForms is a great app from Sapien which allows you create form based windows for which it gives you the PowerShell code, this means no install and yet the users still get a great looking GUI.

It turned out pretty good, even if I do say so myself.

Here’s what the script allows you to do:

  • Compares one host to another
  • Copies vSwitches if they do not currently exist
  • Copies PortGroups + VLAN Tags if they do not currently exist

What it doesn’t do at the moment is copy any of the custom settings for the vSwitches or PortGroups.

This is still very much a V1 and it will be added to, I hope to include other areas such as the NTP servers, advanced host configuration etc, if you have an idea then please add it to the comments and I will see what I can do.

To see this in action please check out the below video, this script can easily be run straight from the PowerCLI prompt also.

vProfiles from Alan Renouf on Vimeo.

To download the script check below:

13 thoughts on “PowerCLI: vProfiles”

  1. Guys, this worked script, if you have only one vswitch vSwitch0 on ESX 5.1 , and you need only transfer ports groups.

    #
    # vProfiles V1
    # By Alan Renouf
    # http://virtu-al.net
    #

    #Generated Form Function
    function GenerateForm {
    ########################################################################
    # Code Generated By: SAPIEN Technologies PrimalForms (Community Edition) v1.0.3.0
    # Generated On: 27/06/2009 9:17 PM
    # Generated By: Alan
    ########################################################################

    #region Import the Assemblies
    [reflection.assembly]::loadwithpartialname(“System.Drawing”) | Out-Null
    [reflection.assembly]::loadwithpartialname(“System.Windows.Forms”) | Out-Null
    #endregion

    #region Generated Form Objects
    $vProfiles = New-Object System.Windows.Forms.Form
    $label3 = New-Object System.Windows.Forms.Label
    $label2 = New-Object System.Windows.Forms.Label
    $Disconnect = New-Object System.Windows.Forms.Button
    $Connect = New-Object System.Windows.Forms.Button
    $OutputWin = New-Object System.Windows.Forms.TextBox
    $CopyNetwork = New-Object System.Windows.Forms.Button
    $NewHost = New-Object System.Windows.Forms.ListBox
    $BaseHost = New-Object System.Windows.Forms.ListBox
    $label1 = New-Object System.Windows.Forms.Label
    $VCSRV = New-Object System.Windows.Forms.TextBox
    $InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState
    #endregion Generated Form Objects

    #———————————————-
    #Generated Event Script Blocks
    #———————————————-
    #Provide Custom Code for events specified in PrimalForms.
    $Disconnect_OnClick=
    {
    Disconnect-VIServer $ConVISRV -Confirm:$false
    $Disconnect.Enabled = $false
    $CopyNetwork.Enabled = $false
    $OutputWin.Text += “rnDisconnect Successful”
    $vProfiles.Refresh()

    }

    $handler_NewHost_SelectedIndexChanged=
    {
    #TODO: Place custom script here

    }

    $handler_button3_Click=
    {
    $OutputWin.Text += “rnConnecting to $($Basehost.SelectedItem)”
    $vProfiles.Refresh()
    $CBASEHost = Get-VMHost -Name ($Basehost.SelectedItem)
    $OutputWin.Text += “rnConnecting to $($Newhost.SelectedItem)”
    $vProfiles.Refresh()
    $CNEWHost = Get-VMHost -Name ($NewHost.SelectedItem)
    $OutputWin.Text += “rnRetrieving Virtual Switches”
    $vProfiles.Refresh()
    $CBASEHost | Get-VirtualSwitch | Foreach {
    #If (($CNEWHost | Get-VirtualSwitch -Name $_.Name -ErrorAction SilentlyContinue) -eq $null){
    #$vSwitch = $_
    #$OutputWin.Text += “rnCreating Virtual Switch $($_.Name)”
    #$vProfiles.Refresh()
    #$NewSwitch = $CNEWHost | New-VirtualSwitch -Name $_.Name -NumPorts $_.NumPorts -Mtu $_.Mtu
    #}
    $vSwitch = $_
    $_ | Get-VirtualPortGroup | Foreach {
    If (($CNEWHost | Get-VirtualPortGroup -Name $_.Name -ErrorAction SilentlyContinue) -eq $null){
    $OutputWin.Text += “rn..Creating Portgroup $($_.Name) on $($vSwitch.Name)”
    $vProfiles.Refresh()
    $NewPortGroup = $CNEWHost | Get-VirtualSwitch -Name $vSwitch | New-VirtualPortGroup -Name $_.Name -VLanId $_.VLanID
    }
    }
    }
    $OutputWin.Text += “rn– Configuration Completed –”
    $vProfiles.Refresh()

    }

    $handler_label1_Click=
    {
    #TODO: Place custom script here

    }

    $handler_listBox1_SelectedIndexChanged=
    {
    #TODO: Place custom script here

    }

    $handler_button1_Click=
    {
    If ($VCSRV.Text -eq “”) {$OutputWin.Text = “Please Specify a VC Server above”}
    Else
    {
    $OutputWin.Text = “Connecting to $($VCSRV.Text)”
    $vProfiles.Refresh()
    $ConVISRV = Connect-VIServer $VCSRV.Text
    If ($ConVISRV.IsConnected -eq $true) {
    $OutputWin.Text += “rnConnection Successful”
    $vProfiles.Refresh()
    $Disconnect.Enabled = $true
    $CopyNetwork.Enabled = $true
    Foreach ($EsxHost in (Get-VMHost))
    {
    $BaseHost.Items.Add(“$($EsxHost.Name)”)|Out-Null
    $NewHost.Items.Add(“$($EsxHost.Name)”)|Out-Null
    }
    }
    Else {
    $OutputWin.Text += “rnConnection Un-Successful – Please try again.”
    $vProfiles.Refresh()
    }
    }

    }

    $OnLoadForm_StateCorrection=
    {#Correct the initial state of the form to prevent the .Net maximized form issue
    $vProfiles.WindowState = $InitialFormWindowState
    }

    #———————————————-
    #region Generated Form Code
    $System_Drawing_Size = New-Object System.Drawing.Size
    $System_Drawing_Size.Width = 563
    $System_Drawing_Size.Height = 503
    $vProfiles.MaximumSize = $System_Drawing_Size
    $vProfiles.AutoSize = $True
    $vProfiles.Text = ‘vProfiles’
    $vProfiles.ShowInTaskbar = $False
    $vProfiles.MaximizeBox = $False
    $vProfiles.Name = ‘vProfiles’
    $vProfiles.SizeGripStyle = 2
    $vProfiles.ShowIcon = $False
    $System_Drawing_Size = New-Object System.Drawing.Size
    $System_Drawing_Size.Width = 563
    $System_Drawing_Size.Height = 503
    $vProfiles.MinimumSize = $System_Drawing_Size
    $vProfiles.StartPosition = 1
    $vProfiles.DataBindings.DefaultDataSourceUpdateMode = 0
    $System_Drawing_Size = New-Object System.Drawing.Size
    $System_Drawing_Size.Width = 547
    $System_Drawing_Size.Height = 465
    $vProfiles.ClientSize = $System_Drawing_Size

    $label3.TabIndex = 11
    $System_Drawing_Size = New-Object System.Drawing.Size
    $System_Drawing_Size.Width = 100
    $System_Drawing_Size.Height = 13
    $label3.Size = $System_Drawing_Size
    $label3.Text = ‘Destination:’

    $System_Drawing_Point = New-Object System.Drawing.Point
    $System_Drawing_Point.X = 271
    $System_Drawing_Point.Y = 43
    $label3.Location = $System_Drawing_Point
    $label3.DataBindings.DefaultDataSourceUpdateMode = 0
    $label3.Name = ‘label3’
    $label3.add_Click($handler_label3_Click)

    $vProfiles.Controls.Add($label3)

    $label2.TabIndex = 10
    $System_Drawing_Size = New-Object System.Drawing.Size
    $System_Drawing_Size.Width = 100
    $System_Drawing_Size.Height = 13
    $label2.Size = $System_Drawing_Size
    $label2.Text = ‘Source:’

    $System_Drawing_Point = New-Object System.Drawing.Point
    $System_Drawing_Point.X = 24
    $System_Drawing_Point.Y = 43
    $label2.Location = $System_Drawing_Point
    $label2.DataBindings.DefaultDataSourceUpdateMode = 0
    $label2.Name = ‘label2’

    $vProfiles.Controls.Add($label2)

    $Disconnect.TabIndex = 9
    $Disconnect.Name = ‘Disconnect’
    $Disconnect.Enabled = $False
    $System_Drawing_Size = New-Object System.Drawing.Size
    $System_Drawing_Size.Width = 75
    $System_Drawing_Size.Height = 23
    $Disconnect.Size = $System_Drawing_Size
    $Disconnect.UseVisualStyleBackColor = $True

    $Disconnect.Text = ‘Disconnect’

    $System_Drawing_Point = New-Object System.Drawing.Point
    $System_Drawing_Point.X = 442
    $System_Drawing_Point.Y = 10
    $Disconnect.Location = $System_Drawing_Point
    $Disconnect.DataBindings.DefaultDataSourceUpdateMode = 0
    $Disconnect.add_Click($Disconnect_OnClick)

    $vProfiles.Controls.Add($Disconnect)

    $Connect.TabIndex = 8
    $Connect.Name = ‘Connect’
    $System_Drawing_Size = New-Object System.Drawing.Size
    $System_Drawing_Size.Width = 75
    $System_Drawing_Size.Height = 23
    $Connect.Size = $System_Drawing_Size
    $Connect.UseVisualStyleBackColor = $True

    $Connect.Text = ‘Connect’

    $System_Drawing_Point = New-Object System.Drawing.Point
    $System_Drawing_Point.X = 360
    $System_Drawing_Point.Y = 10
    $Connect.Location = $System_Drawing_Point
    $Connect.DataBindings.DefaultDataSourceUpdateMode = 0
    $Connect.add_Click($handler_button1_Click)

    $vProfiles.Controls.Add($Connect)

    $OutputWin.Multiline = $True
    $System_Drawing_Size = New-Object System.Drawing.Size
    $System_Drawing_Size.Width = 491
    $System_Drawing_Size.Height = 264
    $OutputWin.Size = $System_Drawing_Size
    $OutputWin.DataBindings.DefaultDataSourceUpdateMode = 0
    $OutputWin.AcceptsReturn = $True
    $OutputWin.ScrollBars = 2
    $OutputWin.Name = ‘OutputWin’
    $System_Drawing_Point = New-Object System.Drawing.Point
    $System_Drawing_Point.X = 24
    $System_Drawing_Point.Y = 189
    $OutputWin.Location = $System_Drawing_Point
    $OutputWin.Enabled = $False
    $OutputWin.TabIndex = 7

    $vProfiles.Controls.Add($OutputWin)

    $CopyNetwork.TabIndex = 6
    $CopyNetwork.Name = ‘CopyNetwork’
    $System_Drawing_Size = New-Object System.Drawing.Size
    $System_Drawing_Size.Width = 179
    $System_Drawing_Size.Height = 23
    $CopyNetwork.Size = $System_Drawing_Size
    $CopyNetwork.UseVisualStyleBackColor = $True
    $CopyNetwork.Enabled = $false

    $CopyNetwork.Text = ‘Copy vSwitches and PortGroups’

    $System_Drawing_Point = New-Object System.Drawing.Point
    $System_Drawing_Point.X = 175
    $System_Drawing_Point.Y = 160
    $CopyNetwork.Location = $System_Drawing_Point
    $CopyNetwork.DataBindings.DefaultDataSourceUpdateMode = 0
    $CopyNetwork.add_Click($handler_button3_Click)

    $vProfiles.Controls.Add($CopyNetwork)

    $NewHost.FormattingEnabled = $True
    $System_Drawing_Size = New-Object System.Drawing.Size
    $System_Drawing_Size.Width = 244
    $System_Drawing_Size.Height = 95
    $NewHost.Size = $System_Drawing_Size
    $NewHost.DataBindings.DefaultDataSourceUpdateMode = 0
    $NewHost.ScrollAlwaysVisible = $True
    $NewHost.Name = ‘NewHost’
    $System_Drawing_Point = New-Object System.Drawing.Point
    $System_Drawing_Point.X = 271
    $System_Drawing_Point.Y = 59
    $NewHost.Location = $System_Drawing_Point
    $NewHost.TabIndex = 5
    $NewHost.add_SelectedIndexChanged($handler_NewHost_SelectedIndexChanged)

    $vProfiles.Controls.Add($NewHost)

    $BaseHost.FormattingEnabled = $True
    $System_Drawing_Size = New-Object System.Drawing.Size
    $System_Drawing_Size.Width = 241
    $System_Drawing_Size.Height = 95
    $BaseHost.Size = $System_Drawing_Size
    $BaseHost.DataBindings.DefaultDataSourceUpdateMode = 0
    $BaseHost.ScrollAlwaysVisible = $True
    $BaseHost.Name = ‘BaseHost’
    $System_Drawing_Point = New-Object System.Drawing.Point
    $System_Drawing_Point.X = 24
    $System_Drawing_Point.Y = 59
    $BaseHost.Location = $System_Drawing_Point
    $BaseHost.TabIndex = 4
    $BaseHost.add_SelectedIndexChanged($handler_listBox1_SelectedIndexChanged)

    $vProfiles.Controls.Add($BaseHost)

    $label1.TabIndex = 1
    $System_Drawing_Size = New-Object System.Drawing.Size
    $System_Drawing_Size.Width = 86
    $System_Drawing_Size.Height = 23
    $label1.Size = $System_Drawing_Size
    $label1.Text = ‘Virtual Center:’

    $System_Drawing_Point = New-Object System.Drawing.Point
    $System_Drawing_Point.X = 34
    $System_Drawing_Point.Y = 15
    $label1.Location = $System_Drawing_Point
    $label1.DataBindings.DefaultDataSourceUpdateMode = 0
    $label1.Name = ‘label1’
    $label1.add_Click($handler_label1_Click)

    $vProfiles.Controls.Add($label1)

    $System_Drawing_Size = New-Object System.Drawing.Size
    $System_Drawing_Size.Width = 228
    $System_Drawing_Size.Height = 20
    $VCSRV.Size = $System_Drawing_Size
    $VCSRV.DataBindings.DefaultDataSourceUpdateMode = 0
    $VCSRV.Name = ‘VCSRV’
    $System_Drawing_Point = New-Object System.Drawing.Point
    $System_Drawing_Point.X = 126
    $System_Drawing_Point.Y = 12
    $VCSRV.Location = $System_Drawing_Point
    $VCSRV.TabIndex = 0

    $vProfiles.Controls.Add($VCSRV)

    #endregion Generated Form Code

    #Save the initial state of the form
    $InitialFormWindowState = $vProfiles.WindowState
    #Init the OnLoad event to correct the initial state of the form
    $vProfiles.add_Load($OnLoadForm_StateCorrection)
    #Show the Form
    $vProfiles.ShowDialog()| Out-Null

    } #End Function

    #Call the Function
    GenerateForm

    P.S. Waiting for v2 of this script -Alan.

  2. copied the wrong lines. here it is:

    just edit line 65 following to this to get the Script to work, even you precreated the vSwitches

    $CBASEHost | Get-VirtualSwitch | Foreach {
    $vSwitch = $_
    If (($CNEWHost | Get-VirtualSwitch -Name $vSwitch.Name -ErrorAction SilentlyContinue) -eq $null){
    $OutputWin.Text += “rnCreating Virtual Switch $($vSwitch.Name)”
    $vProfiles.Refresh()
    $NewSwitch = $CNEWHost | New-VirtualSwitch -Name $vSwitch.Name -NumPorts $vSwitch.NumPorts -Mtu $vSwitch.Mtu
    }
    $vSwitch | Get-VirtualPortGroup | Foreach {
    $vPortgroup = $_
    If (($CNEWHost | Get-VirtualPortGroup -Name $vPortgroup.Name -ErrorAction SilentlyContinue) -eq $null){
    $OutputWin.Text += “rn..Creating Portgroup $($vPortgroup.Name) on $($vSwitch.Name)”
    $vProfiles.Refresh()
    $NewPortGroup = $CNEWHost | Get-VirtualSwitch -Name $vSwitch | New-VirtualPortGroup -Name $vPortgroup.Name -VLanId $vPortgroup.VLanID
    }
    }
    }

  3. just edit line 65 following to this to get the Script to work, even you precreated the vSwitches

    $CBASEHost | Get-VirtualSwitch | Foreach {
    If (($CNEWHost | Get-VirtualSwitch -Name $_.Name -ErrorAction SilentlyContinue) -eq $null){
    $vSwitch = $_
    $OutputWin.Text += “rnCreating Virtual Switch $($_.Name)”
    $vProfiles.Refresh()
    $NewSwitch = $CNEWHost | New-VirtualSwitch -Name $_.Name -NumPorts $_.NumPorts -Mtu $_.Mtu
    }
    $_ | Get-VirtualPortGroup | Foreach {
    If (($CNEWHost | Get-VirtualPortGroup -Name $_.Name -ErrorAction SilentlyContinue) -eq $null){
    $OutputWin.Text += “rn..Creating Portgroup $($_.Name) on $($vSwitch.Name)”
    $vProfiles.Refresh()
    $NewPortGroup = $CNEWHost | Get-VirtualSwitch -Name $vSwitch | New-VirtualPortGroup -Name $_.Name -VLanId $_.VLanID
    }
    }
    }

  4. It wouldbe awesome if it did have dvswitch support – we have a mess of dvswitches and we have to consolidate a lot of things right now. Hopefully I can figure something out.

  5. Hi Al, this doesn’t work for me (ESXi 3.5 & ESXi 4)…this is the error:
    Get-VirtualSwitch : Cannot validate argument on parameter ‘Name’. The argument
    is null or empty. Supply an argument that is not null or empty and then try the
    command again.

    I had the same as above, removed the switches and re-run, it works OK. It seems that it does not like if you pre-create switches.

  6. Hi Al, like Mark I’m unable to run this against an ESX 4 update 1 host. It gives the error ‘The argument cannot be null or empty’ just after enumerating the virtual switches and trying to create the first portgroup. If I find time I’ll try to dig into the code myself but I’m no Virtu-Al!

  7. Hi Al, this doesn’t work for me (ESXi 3.5 & ESXi 4)…this is the error:

    Get-VirtualSwitch : Cannot validate argument on parameter ‘Name’. The argument
    is null or empty. Supply an argument that is not null or empty and then try the
    command again.

  8. This would be beautiful:

    iSCSI enable and config
    Security Profile: NFS enable, NTP enable, SNMP and so on
    License Server Source: populate from source host
    DNS and Routing: populate domain and DNS servers from source host?

    alex AT myra.com

  9. Thanks Kenny, anything you think would be useful to add let me know !

    NTP
    Advanced Host settings
    ETC

Leave a Reply