Hi,
I'm trying to understand how these 2 statements are different, and why.
In the below statement there is no find by where used and instead just -name switch is used.
Remove-VirtualPortGroup -VirtualPortGroup (Get-VirtualPortGroup -Name vMotion) -Confirm:$false
While in this statement even though there is a -name switch it's being called using where and not directly.
Remove-VirtualSwitch -VirtualSwitch (Get-VirtualSwitch -VMHost 192.168.116.60 | where {$_.Name -eq "vSwitch1"}) -Confirm:$false.
Thank You.
On both your "Is this a general scripting understanding or specific to powershell / powercli" remarks:
since I don't know all scripting languages, I would say this is a concept that most languages that are object-oriented and use a pipeline concept, adhere to.
In this case, it is a PowerShell concept.
Remember, PowerCLI is a set of modules on top of PowerShell.
All PowerShell rules are thus implicitly applicable to PowerCLI as well.
There are many books, courses, blog posts... available on PowerShell and PowerCLI.
When you are starting, you will need to learn PowerShell basics and the PowerCLI cmdlets.
Getting proficient in PowerCLI requires you have a basic understanding of how PowerShell works.
There are two good lists that lay out a path for acquiring knowledge on both.
They are:
---------------------------------------------------------------------------------------------------------
Was it helpful? Let us know by completing this short survey here.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
For the Virtual Switch there is possibly more than 1 vSwitch on that ESXi node.
Hence the Where-cluse with the Name filter.
But that could have written in the same way as the VirtualPortGroup line.
Remove-VirtualSwitch -VirtualSwitch (Get-VirtualSwitch -VMHost 192.168.116.60 -Name "vSwitch1") -Confirm:$false.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks for clarifying.
Also if I may ask what does it mean in the below where it says pipeline input : false for VMHostPhysicalNic
Does it mean that when VMHostPhysicalNic is used as a parameter in a pipeline it can only accept input as a variable, because when I tried this in a pipeline I receive the below error.
$hosts = Get-VMhost -name $esxiHosts
foreach-object {Get-VMhost $esxiHosts | Get-VMHostNetworkAdapter -Physical | Get-VirtualSwitch -name "vSwitch0" | Add-VirtualSwitchPhysicalNetworkAdapter -VMHostPhysicalNic "vmnic1" -confirm:$false}
Add-VirtualSwitchPhysicalNetworkAdapter : Cannot bind parameter 'VMHostPhysicalNic'. Cannot convert the "vmnic1" value of type "System.String" to type
"VMware.VimAutomation.ViCore.Types.V1.Host.Networking.Nic.PhysicalNic".
At line:1 char:174
+ ... rtualSwitchPhysicalNetworkAdapter -VMHostPhysicalNic "vmnic1" -confir ...
+ ~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Add-VirtualSwitchPhysicalNetworkAdapter], ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgumentNoMessage,VMware.VimAutomation.ViCore.Cmdlets.Commands.AddVirtualSwitchPhysicalNetworkAdapter
If it says Pipeline input is false for a parameter, it means that you cannot provide a value for that parameter over the pipeline.
The error you are getting is something else though.
The VMHostPhysicalNic parameter expects a PhysicalNic object, not a string.
You could do
foreach-object {
$nic = Get-VMHostNetworkAdapter -VMHost $_ -Physical -Name "vmnic1"
Get-VirtualSwitch -VMHost $_ -Name "vSwitch0" |
Add-VirtualSwitchPhysicalNetworkAdapter -VMHostPhysicalNic $nic -confirm:$false
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks you that did work.
I do have some questions with that script of yours.
How is this:
Get-VMhost -name $esxiHosts |
foreach-object {
$nic = Get-VMHostNetworkAdapter -VMHost $_ -Physical -Name "vmnic1" # You removed the pipe here and simply added command to the next line
Get-VirtualSwitch -VMHost $_ -Name "vSwitch0" | # here the pipe is added still the command was in the next line
Add-VirtualSwitchPhysicalNetworkAdapter -VMHostPhysicalNic $nic -confirm:$false
}
Different from this :
Get-VMhost -name $esxiHosts | foreach-object {$nic = Get-VMHostNetworkAdapter -VMHost $_ -Physical -Name "vmnic1" | Get-VirtualSwitch -VMHost $_ -Name "vSwitch0" | Add-VirtualSwitchPhysicalNetworkAdapter -VMHostPhysicalNic $nic -confirm:$false}
What I'm trying to ask is as per Microsoft's explanation of pipeline in powershell the input from 1st command is passed on to the next in the pipeline.
When i wrote the 1st script all as piped the script ran into an error.
The explanation is that the Add-VirtualSwitchPhysicalNetworkAdapter needs two parameters in this case.
The VMHostPhysicalNic and the VirtualSwitch parameters.
You can't pass both on the same pipeline, only one.
As an addition, the object you pass, needs to be consumed on the next cmdlet in the pipeline, not 1 cmdlet further along.
It's a sequence.
As an alternative, you can do the following.
This passes the VMHostPhysicalNic parameter value over the pipeline.
foreach-object {
$sw = Get-VirtualSwitch -VMHost $_ -Name "vSwitch0"$
Get-VMHostNetworkAdapter -VMHost $_ -Physical -Name "vmnic1" |
Add-VirtualSwitchPhysicalNetworkAdapter -VirtualSwitch $sw
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks for the explanation well explained.
The explanation is that the Add-VirtualSwitchPhysicalNetworkAdapter needs two parameters in this case. - Understood.
The VMHostPhysicalNic and the VirtualSwitch parameters.
You can't pass both on the same pipeline, only one. - Is this a general scripting understanding or specific to powershell / powercli
As an addition, the object you pass, needs to be consumed on the next cmdlet in the pipeline, not 1 cmdlet further along. - Is this a general scripting understanding or specific to powershell / powercli
It's a sequence.
Thanks for taking the time to explain, may I know if there a good book that explains all that you are explaining.
On both your "Is this a general scripting understanding or specific to powershell / powercli" remarks:
since I don't know all scripting languages, I would say this is a concept that most languages that are object-oriented and use a pipeline concept, adhere to.
In this case, it is a PowerShell concept.
Remember, PowerCLI is a set of modules on top of PowerShell.
All PowerShell rules are thus implicitly applicable to PowerCLI as well.
There are many books, courses, blog posts... available on PowerShell and PowerCLI.
When you are starting, you will need to learn PowerShell basics and the PowerCLI cmdlets.
Getting proficient in PowerCLI requires you have a basic understanding of how PowerShell works.
There are two good lists that lay out a path for acquiring knowledge on both.
They are:
---------------------------------------------------------------------------------------------------------
Was it helpful? Let us know by completing this short survey here.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks for all the help..
Getting started now..
