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
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
@{
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
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
@{
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
Hi LucD,
Thanks, the mentioning of Parameter Splatting was very helpful along with the code.