VMware Cloud Community
jvm2016
Hot Shot
Hot Shot
Jump to solution

port groups security policy

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

            }

        }

    }

}

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

If I understand the purpose of your script correctly, it should probably go like this

$dc = Read-Host "provide datacenter name"

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

View solution in original post

Reply
0 Kudos
8 Replies
LucD
Leadership
Leadership
Jump to solution

If I understand the purpose of your script correctly, it should probably go like this

$dc = Read-Host "provide datacenter name"

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

Reply
0 Kudos
jvm2016
Hot Shot
Hot Shot
Jump to solution

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 |

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

Reply
0 Kudos
jvm2016
Hot Shot
Hot Shot
Jump to solution

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

             

            }

        }

    }

}

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

Get-Datacenter -PipelineVariable whatever |

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

Reply
0 Kudos
jvm2016
Hot Shot
Hot Shot
Jump to solution

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 |

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

Get-Datacenter |

ForEach-Object -Process {

    $dc = $_

    Get-Cluster -Location $dc |

    Select @{N='Datacenter';E={$dc.Name}},

        @{N='Cluster';E={$_.Name}}

}

you can do

Get-Datacenter -PipelineVariable dc |

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

Reply
0 Kudos
jvm2016
Hot Shot
Hot Shot
Jump to solution

thanks Luc.

Reply
0 Kudos