Automation

 View Only
  • 1.  port groups security policy

    Posted Jun 23, 2020 10:32 PM

    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

                }

            }

        }

    }



  • 2.  RE: port groups security policy
    Best Answer

    Posted Jun 24, 2020 03:49 AM

    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

                }

            }

        }

    }



  • 3.  RE: port groups security policy

    Posted Jun 24, 2020 05:04 PM

    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 |



  • 4.  RE: port groups security policy

    Posted Jun 24, 2020 05:23 PM

    Not exactly, in the code-block for the Foreach-Object the $dc variable will hold the datacenter object



  • 5.  RE: port groups security policy

    Posted Jun 24, 2020 06:55 PM

    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

                 

                }

            }

        }

    }



  • 6.  RE: port groups security policy

    Posted Jun 24, 2020 07:22 PM

    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.



  • 7.  RE: port groups security policy

    Posted Jun 24, 2020 09:54 PM

    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 |



  • 8.  RE: port groups security policy

    Posted Jun 25, 2020 05:08 AM

    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.



  • 9.  RE: port groups security policy

    Posted Jun 25, 2020 04:34 PM

    thanks Luc.