VMware Cloud Community
TryllZ
Expert
Expert
Jump to solution

Is it a good idea to loop this..

Hi,

I have the following code

$getVMKernel = Get-VMHostNetworkAdapter -VMKernel

$getVMKernel | Where {$_.PortGroupName-match "vSANPG"} | Set-VMhostNetworkAdapter -vSANTrafficEnabled $true -Confirm:$false

$getVMKernel | Where {$_.PortGroupName-match "vMotionPG"} | Set-VMhostNetworkAdapter -VMotionEnabled $true -Confirm:$false

$getVMKernel | Where {$_.PortGroupName-match "FTPG"} | Set-VMhostNetworkAdapter -FaultToleranceLoggingEnabled $true -Confirm:$false

which I'm trying to loop it as follows:

$pgname = "vSANPG", "vMotionPG", "FTPG"

$adapter = "vSANTrafficEnabled", "vMotionEnabled", "FaultToleranceLoggingEnabled"

For ($i=0;$i -le $pgname.length;$i++)

   {

      $getVMKernel | Where {$_.PortGroupName-match $pgname[$i]} | Set-VMhostNetworkAdapter -$adapter[$i] $false -Confirm:$false

   }

The error is

Set-VMHostNetworkAdapter : A positional parameter cannot be found that accepts argument '-vSANTrafficEnabled vMotionEnabled FaultToleranceLoggingEnabled[3]'.

At line:1 char:61

+ ... name[$i]} | Set-VMhostNetworkAdapter -$adapter[$i] $false -Confirm:$f ...

+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : InvalidArgument: (:) [Set-VMHostNetworkAdapter], ParameterBindingException

    + FullyQualifiedErrorId : PositionalParameterNotFound,VMware.VimAutomation.ViCore.Cmdlets.Commands.Host.SetVMHostNetworkAdapter

I understand its much easier to use the code as is, but I'm trying to see if its better to loop it as much of the code is repetitive.

Thank You

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

The idea is good, but it will not work like this I'm afraid.
PS doesn't accept strings as parameters.

In this case, you will need to use splatting of the parameters.

I would do something like this

$data = @(

    @{

        Portgroup = 'vSANPG'

        Switch = 'vSANTrafficEnabled'

    },

    @{

        Portgroup = 'vMotionPG'

        Switch = 'vMotionEnabled'

    },

    @{

        Portgroup = 'FTPG'

        Switch = 'FaultToleranceLoggingEnabled'

    }

)

foreach($entry in $data){

    Get-VMHostNetworkAdapter -VMHost homelab* -VMKernel -PortGroup $entry.Portgroup |

    ForEach-Object -Process {

        $sNet = @{

            VirtualNic = $_

            "$($entry.Switch)" = $false

            Confirm = $false

        }

        Set-VMHostNetworkAdapter @sNet

    }

}

 


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

View solution in original post

Reply
0 Kudos
2 Replies
LucD
Leadership
Leadership
Jump to solution

The idea is good, but it will not work like this I'm afraid.
PS doesn't accept strings as parameters.

In this case, you will need to use splatting of the parameters.

I would do something like this

$data = @(

    @{

        Portgroup = 'vSANPG'

        Switch = 'vSANTrafficEnabled'

    },

    @{

        Portgroup = 'vMotionPG'

        Switch = 'vMotionEnabled'

    },

    @{

        Portgroup = 'FTPG'

        Switch = 'FaultToleranceLoggingEnabled'

    }

)

foreach($entry in $data){

    Get-VMHostNetworkAdapter -VMHost homelab* -VMKernel -PortGroup $entry.Portgroup |

    ForEach-Object -Process {

        $sNet = @{

            VirtualNic = $_

            "$($entry.Switch)" = $false

            Confirm = $false

        }

        Set-VMHostNetworkAdapter @sNet

    }

}

 


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

Reply
0 Kudos
TryllZ
Expert
Expert
Jump to solution

Hi LucD,

Thanks, the mentioning of Parameter Splatting was very helpful along with the code.

Reply
0 Kudos