VMware Cloud Community
Munster99
Enthusiast
Enthusiast

Help with Advanced function

Hello All

Was hoping someone might be able to help. Am in the process of creating an advanced function, and have become stuck with something that (i think) maybe more related to 'powershell' than 'powercli'.

What i want from the function is the ability to add a SINGLE portgroup to a SINGLE host OR the ability to add MANY portgroups to MANY hosts within a cluster. I have added in what i've achieved so far but and i can easily add the SINGLE portgroup but the problem is the MULTIPLE bit that is causing issues. (script is shown below)

I was thinking of using DYNAMIC PARAMETERS to assist, but I'm wondering if I can do this WITHOUT using them ?!?!?

The plan was to use the 'multiple' switch to choose a different set of parameters, but I get stuck when trying to choose a cluster within the environments (red, Blue, Green) as each enviroment has a load of different clusters attached to them and all clusters have a  different set of portgroups associated with them as well. ?!?!?

Any ideas anyone ???

AS usual - many thanks in advance

Munster99

Function Add-VMPortgroups{

[CmdletBinding(SupportsShouldProcess=$true,  

               ConfirmImpact='Low')]        

    PARAM

    (

    [Parameter(Mandatory=$true,

               ParameterSetName='Single')]

    [Switch]$SinglePortgroup,

    [Parameter(Mandatory=$true,

               ParameterSetName='Multiple')]

    [Switch]$MultiplePortgroups,

    ######################################################################

    [Parameter(Mandatory=$true,

               HelpMessage = 'Please enter Environment',

               ParameterSetName='Multiple',

               Position=0)]

    [ValidateNotNullOrEmpty()]

    [ValidateSet('Red','Blue','Green')]

    [String]$Environment,

    [Parameter(Mandatory=$true,

               ValueFromPipeline=$true,

               HelpMessage = 'Please enter ClusterName',

               ParameterSetName='Multiple',

               Position=1)]

    [ValidateNotNullOrEmpty()]

    [VMware.VimAutomation.ViCore.Impl.V1.Inventory.ClusterImpl[]]$ClusterName,

    ######################################################################

    [Parameter(Mandatory=$true,

               ValueFromPipeline=$true,

               ValueFromPipelineByPropertyName=$true,

               HelpMessage = "Please enter FQDN VMHost name",

               ParameterSetName='Single',

               Position=0)]

    [ValidateNotNullOrEmpty()]

    [Alias('Hostname')]

    [VMware.VimAutomation.ViCore.Impl.V1.Inventory.VMHostImpl[]]$VMhostName,

    [Parameter(Mandatory=$true)]

    [ValidateSet('vSwitch0','vSwitch1','vSwitch2','vSwitch3')]

    [String]$vSwitch,

    [Parameter(Mandatory=$true,

               ParameterSetName='Single',

               Position=2)]

    [String]$PGName,

    [Parameter(Mandatory=$true,

               ParameterSetName='Single',

               Position=3)]

    [Int]$VlanId

    )   

    BEGIN { }

    PROCESS {

        Foreach ($VMhost in $VMhostName){

            Write-Verbose " Processing $VMhost .... "

            Write-Verbose "Adding $NewVirtualPGName virtual portgroup with VlanID $Vlanid to vSwitch $vSwitch onto ESXi Host $VMhostName "

            If ($PSCmdlet.ShouldProcess("Adding $NewVirtualPGName virtual portgroup with VlanID $Vlanid to vSwitch $vSwitch onto ESXi Host $VMhostName ")){

                $result = New-VirtualPortgroup -Name "$NewVirtualPGName" -virtualSwitch (Get-VirtualSwitch -VMHost "$VMhost" -Name "$vSwitch") -vLanid "$VlanID" -confirm:$false

                if ($result -eq "true"){

                    Write-Output "Successfully added " }

                Else { $result }

                }

        }

    }

    END { }

}

Tags (1)
0 Kudos
5 Replies
LucD
Leadership
Leadership

Try something like this

Function Add-VMPortgroups{

[CmdletBinding(SupportsShouldProcess=$true, 

               ConfirmImpact='Low')]       

    PARAM

    (

    [Parameter(Mandatory=$true,

               ValueFromPipeline=$true,

               HelpMessage = 'Please enter ClusterName',

               ParameterSetName='Cluster')]

    [ValidateNotNullOrEmpty()]

    [VMware.VimAutomation.ViCore.Impl.V1.Inventory.ClusterImpl[]]$Cluster,

    ######################################################################

    [Parameter(Mandatory=$true,

               ValueFromPipeline=$true,

               ValueFromPipelineByPropertyName=$true,

               HelpMessage = "Please enter FQDN VMHost name",

               ParameterSetName='VMHost')]

    [ValidateNotNullOrEmpty()]

    [Alias('Hostname')]

    [VMware.VimAutomation.ViCore.Impl.V1.Inventory.VMHostImpl[]]$VMhost,

    [Parameter(Mandatory=$true)]

    [ValidateSet('vSwitch0','vSwitch1','vSwitch2','vSwitch3')]

    [String]$vSwitch,

    [Parameter(Mandatory=$true)]

    [String]$PGName,

    [Parameter(Mandatory=$true)]

    [Int]$VlanId

    )  

    BEGIN { }

    PROCESS {

        if($PSCmdlet.ParameterSetName -eq 'Cluster'){

            $VMHost = Get-VMHost -Location $Cluster

        }

        Foreach ($esx in $VMhost){

            Write-Verbose " Processing $($esx.Name) .... "

            Write-Verbose "Adding $PGName virtual portgroup with VlanID $Vlanid to vSwitch $vSwitch onto ESXi Host $(4esx.Name)"

            If ($PSCmdlet.ShouldProcess("Adding $PGName virtual portgroup with VlanID $Vlanid to vSwitch $vSwitch onto ESXi Host $($esx.Name) ")){

                $result = New-VirtualPortgroup -Name "$PGName" -VirtualSwitch (Get-VirtualSwitch -VMHost $esx -Name $vSwitch) -vLanid $VlanID -confirm:$false

                if ($result -eq "true"){

                    Write-Output "Successfully added " }

                Else { $result }

                }

        }

    }

    END { }

}


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

0 Kudos
Munster99
Enthusiast
Enthusiast

Thanks LucD for that !

The issue i have is I would like to add in 'more than one' portgroup to a particular vswitch from a csv list (I generated earlier). Except that, how would I add that into my function ??? i was thinking of using Import-csv to import in the list ( shown below)

"portgroup1","100"

"portgroup2","200"

"portgroup3","300"

"portgroup4","400"

"portgroup5","500"

etc ......

0 Kudos
LucD
Leadership
Leadership

You could do that like this

Import-Csv pg.csv -UseCulture | %{

    Add-VMPortgroups -VMHost $esx -VSwitch 'vswitch0' -PGName $_.PGName -VlanId $_.VlanId

}

Provided your CSV looks like this

"PGName","VlanId"

"portgroup1","100"

"portgroup2","200"

"portgroup3","300"

"portgroup4","400"

"portgroup5","500"


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

0 Kudos
Munster99
Enthusiast
Enthusiast

Aaaah nearly there !!!!

Problem i'm getting now is its asking that the 'vlanid' value is a string and supposed to be a [Int32] integer .

Cannot bind parameter 'VLanId'. Cannot convert value ".VlanID" to type "System.Int32". Error: "Input string was not in a correct format."

Any ideas ??!?!

0 Kudos
LucD
Leadership
Leadership

Indeed, everything you read from a CSV file is presented as a [string].

But you can easily cast this to an [int] with -VlanId ([int]$_.VlanId)


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

0 Kudos