hi luc ,
can yu please suggest whats wrong in below
$dc=Read-Host "provide datacenter name"
Get-datacenter -Name $dc -PipelineVariable vdswitch |
ForEach-Object -Process {
Get-VDSwitch -Location $dc -PipelineVariable vdportgroup |
ForEach-Object -Process {
Get-vdportgroup -$VDSwitch | Sort-Object -Property Name |
ForEach-Object -Process {
[PSCustomObject]@{
datacenter = $dc.name
vdswitches = Get-VDSwitch -Location $dc
vdportgroups = Get-vdportgroup -$VDSwitch
}
}
}
}
If I understand the purpose of your script correctly, it should probably go like this
Get-Datacenter -Name $dc -PipelineVariable dc |
ForEach-Object -Process {
Get-VDSwitch -Location $dc -PipelineVariable VDSwitch |
ForEach-Object -Process {
Get-vdportgroup -VDSwitch $VDSwitch | Sort-Object -Property Name |
ForEach-Object -Process {
[PSCustomObject]@{
datacenter = $dc.name
vdswitch = $VDSwitch.Name
vdportgroups = $_.Name
}
}
}
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
If I understand the purpose of your script correctly, it should probably go like this
Get-Datacenter -Name $dc -PipelineVariable dc |
ForEach-Object -Process {
Get-VDSwitch -Location $dc -PipelineVariable VDSwitch |
ForEach-Object -Process {
Get-vdportgroup -VDSwitch $VDSwitch | Sort-Object -Property Name |
ForEach-Object -Process {
[PSCustomObject]@{
datacenter = $dc.name
vdswitch = $VDSwitch.Name
vdportgroups = $_.Name
}
}
}
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
i m trying to understand the use of pipelinevariable here even if i dont use it the it will have datacenters objects only .
what i mean to say in following
Get-Datacenter -Name $dc | after pipe it will have datacwnter object |
Not exactly, in the code-block for the Foreach-Object the $dc variable will hold the datacenter object
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
i m sorry to ask this again but if yu can check below code especially the orange line .
i using pipelinevariable as cluster though it is not defined .
as per orange line foreac-object code block will have now cluster object instead of datacenterobject???
$dc = Read-Host "provide datacenter name"
Get-Datacenter -Name $dc -PipelineVariable cluster |
ForEach-Object -Process {
Get-vmhost -Location $cluster -PipelineVariable esxi |
ForEach-Object -Process {
Get-vm -Location $esxi | Sort-Object -Property Name |
ForEach-Object -Process {
[PSCustomObject]@{
datacenter = $dc
cluster = $cluster.name
esxi = $esxi.Name
vm = $_.name
}
}
}
}
You now defined a variable with the name 'cluster' that will contain a Datacenter object.
That name is something you can choose, it has nothing to do with the content.
You could specify
ForEach-Object -Process {
$whatever
}
Now, inside the process block the variable $whatever will contain a Datacenter object.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
thats what i am stating if i remove pipeline parameter it will have datacenter object at the right hand side of pipe .so not sure in what what way pipilinevariable is helping
Get-Datacenter -PipelineVariable whatever |
The pipeline variable is useful when you have nested foreach loops.
But also it allows to have a meaningful name for the pipeline object inside your Process block.
And it avoids having to do an additional assignment inside the Process block.
For example, instead of doing
ForEach-Object -Process {
$dc = $_
Get-Cluster -Location $dc |
Select @{N='Datacenter';E={$dc.Name}},
@{N='Cluster';E={$_.Name}}
}
you can do
ForEach-Object -Process {
Get-Cluster -Location $dc |
Select @{N='Datacenter';E={$dc.Name}},
@{N='Cluster';E={$_.Name}}
}
Admittedly this is a personal preference, both solutions work.
But, personally, I prefer the more concise code with the PipelineVariable.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
thanks Luc.