VMware Cloud Community
Munster99
Enthusiast
Enthusiast
Jump to solution

Help with an Advanced function please (new-virtualportgroup not playing ball) ?!?!?

Hi All

Could someone help with my attempt at creating an advanced function please. I've been trying to get this to work and for love nor money and its simply not doing what I'm asking of it. All I'm trying to do is use an advanced function to create new portgroups on our virtual switches. Its seems to be working fine (i.e. the vmhost object shows itself as an object) but the problem might be the switch statement or the commands within the '{}'. Now i've tested these on their own and they work fine yet I keep getting routed to the 'exception' at the end of the script ?!?!? :smileyconfused: Don't know whats happening could someone asist please ???? Any help would be greatfully appreciated ...... Smiley Happy

Many thanks in advance

Munster

Function Add-NewVPortgroup {

<#

#>

[CmdletBinding()]

PARAM

(

[Parameter(Mandatory=$true,

           HelpMessage = "Please enter a NEW VM Portgroup Name",

           Position=0)]

[String[]]$NewPortGroupName,

[Parameter(Mandatory=$true,

           HelpMessage = "Please enter its associated VlanID",

           Position=1)]

[Int[]]$VlanID,

[Parameter(Mandatory=$true,

           ValueFromPipeline=$True,                      

           ValueFromPipelineByPropertyName=$True,       

           HelpMessage = "Please enter a VMHost")]

[ValidateNotNullOrEmpty()]

[Alias('Hostname')]

[String[]]$VMhostName

)   

BEGIN { }   

PROCESS {

Try {

    Foreach($VMhost in $VMhostName){

        If (($VMhost).GetType().Name -eq "string"){

            Try {

                $VMhost = Get-VMhost $VMhost -ErrorAction Stop

                }

            Catch [Exception]{

                Write-Warning "VMhost $VMhost does not exist"

                continue

                }

        }

        Elseif ($VMhost -isnot [VMware.VimAutomation.ViCore.Impl.V1.Inventory.VMHostImpl]){

                Write-Warning "You did not pass a string or a VMhost object"

                continue

        }                

        $NumvSwitch = ($VMhost| get-virtualswitch).count

        Switch ($NumVSwitch){

        1 {New-VirtualPortgroup -virtualSwitch (Get-VirtualSwitch -VMHost $VMhost -Name "vSwitch0") -Name $NewPortGroupName -vLanid $VlanID; break}

        2 {New-VirtualPortgroup -virtualSwitch (Get-VirtualSwitch -VMHost $VMhost -Name "vSwitch1") -Name $NewPortGroupName -vLanid $VlanID; break}

        3 {New-VirtualPortgroup -virtualSwitch (Get-VirtualSwitch -VMHost $VMhost -Name "vSwitch2") -Name $NewPortGroupName -vLanid $VlanID; break}

        4 {New-VirtualPortgroup -virtualSwitch (Get-VirtualSwitch -VMHost $VMhost -Name "vSwitch2") -Name $NewPortGroupName -vLanid $VlanID; break}

        5 {New-VirtualPortgroup -virtualSwitch (Get-VirtualSwitch -VMHost $VMhost -Name "vSwitch2") -Name $NewPortGroupName -vLanid $VlanID; break}

        6 {New-VirtualPortgroup -virtualSwitch (Get-VirtualSwitch -VMHost $VMhost -Name "vSwitch2") -Name $NewPortGroupName -vLanid $VlanID; break}

        7 {New-VirtualPortgroup -virtualSwitch (Get-VirtualSwitch -VMHost $VMhost -Name "vSwitch2") -Name $NewPortGroupName -vLanid $VlanID; break}

        Default {Throw "The Number of vSwitches on this Host do NOT match up with a standard Host"}

        }

    } 

}

Catch [Exception]{

    THROW "Unable to get $VMhost details, please confirm the Host exists."

}

}   

END { }

}

0 Kudos
1 Solution

Accepted Solutions
Munster99
Enthusiast
Enthusiast
Jump to solution

Thanks for the help Luc. Unfortunately adding the $error[0] returned nothing Smiley Sad (I am running the latest versions of both PS and PowerCLI (4.0 & 5.5 Release 2 build 1671586).

I 'seemed' to have found out waht the problem was tho. Wierd as it might sound but it looks like the 'New-VirtualPortgroup' command was the culprit. I stripped everthing bare and started to add in variables and like back into the function one at a time. When i got to testing the New-VirtualPortgroup function that was where my main problem was occurring. So finally I amended as below and it fixed the issue and now the script works as required:

NOT WORKING

2 {New-VirtualPortgroup -virtualSwitch (Get-VirtualSwitch -VMHost $VMhost -Name "vSwitch1") -Name $NewPortGroupName -vLanid $VlanID; break}

WORKING

2 {New-VirtualPortgroup -virtualSwitch (Get-VirtualSwitch -VMHost $VMhost -Name "vSwitch1") -Name $NewPortGroupName -vLanid $VlanID; break}

I'm assuming that the double quotes are allowing the variables to be read in as what they have been declared as above and missing them out means they are being declared 'literally' hence the command doesnt pick them up correctly ??!?!?

Thanks for the help anyways

Munster99

View solution in original post

0 Kudos
4 Replies
LucD
Leadership
Leadership
Jump to solution

Try adding $error[0] to the Catch block.

That might give some information on what actually goes wrong.

Which PowerShell version are you using ?

The "($VMhost| get-virtualswitch).count" line might have an issue in an older PowerShell version (when there is only 1 vSwitch for example).


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Munster99
Enthusiast
Enthusiast
Jump to solution

Thanks for the help Luc. Unfortunately adding the $error[0] returned nothing Smiley Sad (I am running the latest versions of both PS and PowerCLI (4.0 & 5.5 Release 2 build 1671586).

I 'seemed' to have found out waht the problem was tho. Wierd as it might sound but it looks like the 'New-VirtualPortgroup' command was the culprit. I stripped everthing bare and started to add in variables and like back into the function one at a time. When i got to testing the New-VirtualPortgroup function that was where my main problem was occurring. So finally I amended as below and it fixed the issue and now the script works as required:

NOT WORKING

2 {New-VirtualPortgroup -virtualSwitch (Get-VirtualSwitch -VMHost $VMhost -Name "vSwitch1") -Name $NewPortGroupName -vLanid $VlanID; break}

WORKING

2 {New-VirtualPortgroup -virtualSwitch (Get-VirtualSwitch -VMHost $VMhost -Name "vSwitch1") -Name $NewPortGroupName -vLanid $VlanID; break}

I'm assuming that the double quotes are allowing the variables to be read in as what they have been declared as above and missing them out means they are being declared 'literally' hence the command doesnt pick them up correctly ??!?!?

Thanks for the help anyways

Munster99

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Do you have blanks in the name of any of the VirtualPortgroups by any chance ?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
Munster99
Enthusiast
Enthusiast
Jump to solution

Learned a long time to keep spaces out of labels in powershell Smiley Wink ! So all the new portgroups are made without any spaces.

Munster

0 Kudos