PowerCLI

 View Only
  • 1.  Help with Datastore Report

    Posted Aug 04, 2016 12:30 PM

    HI

    I'm having trouble trying to get the following code to export to a csv file.

    Get-Datastore |

        Where-Object {$_.Name -notMatch ("ESX|Datastore1")} |

             New-Object PSObject -Property @{

                ClusterName = (Get-DatastoreCluster -Datastore $_ | Select -ExpandProperty Name)}},

                @{N="DataStoreName";E={$_.name}},        

                @{N="Folder";E={Get-VM -Datastore $_ | Select -ExpandProperty folder}},

                @{N="VMs";E={Get-VM -Datastore $_ | Select -ExpandProperty Name}} |

    Please assist.

    Thanks



  • 2.  RE: Help with Datastore Report

    Posted Aug 04, 2016 12:37 PM

    Try like this

    Get-Datastore |

        Where-Object {$_.Name -notMatch ("ESX|Datastore1")} |

        Select @{N='ClusterName';E={Get-DatastoreCluster -Datastore $_ | Select -ExpandProperty Name}},

                @{N="DataStoreName";E={$_.name}},       

                @{N="Folder";E={Get-VM -Datastore $_ | Select -ExpandProperty folder}},

                @{N="VMs";E={Get-VM -Datastore $_ | Select -ExpandProperty Name}} |

    Export-Csv report.csv -NoTypeInformation -UseCulture



  • 3.  RE: Help with Datastore Report

    Posted Aug 04, 2016 01:25 PM

    Hi Luc,

    Thanks so much for your help.

    The script exports nicely but for some reason for the Folder and VMs columns I'm getting "SystemObject[]" for all except one which I'm thinking since I;m not using -join "`n"

    that is why.

    Is there a way to modify this script to have each VM on separate line for datastores that have more than one VM?

    thanks



  • 4.  RE: Help with Datastore Report

    Posted Aug 04, 2016 01:35 PM

    Sure, thanks to the power of the Pipeline variable.

    Get-Datastore -PipelineVariable ds |

        Where-Object {$_.Name -notMatch ("ESX|Datastore1")} |

        Get-VM |

        Select @{N='ClusterName';E={Get-DatastoreCluster -Datastore $ds | Select -ExpandProperty Name}},

                @{N="DataStoreName";E={$ds.name}},       

                @{N="Folder";E={$_.folder}},

                @{N="VM";E={$_.Name}} |

    Export-Csv report.csv -NoTypeInformation -UseCulture

     



  • 5.  RE: Help with Datastore Report

    Posted Aug 04, 2016 01:58 PM

    I'm getting an error with the -Pipelinevariable.

    What version of PowerCLI are your using?

    I'm using

    VMware vSphere PowerCLI 6.0 Release 3 build 3205540



  • 6.  RE: Help with Datastore Report

    Posted Aug 04, 2016 02:43 PM

    Hi Luc,

    I just upgraded to VMware vSphere PowerCLI 6.3 Release 1 build 3737840 but getting the same error.

    Thanks



  • 7.  RE: Help with Datastore Report

    Posted Aug 04, 2016 03:31 PM

    What PowerShell version are you using?

    Display $PSVersionTable

    The Pipeline variable, one of the Common variables, was introduced in PowerShell v4



  • 8.  RE: Help with Datastore Report

    Posted Aug 04, 2016 03:41 PM

    Version 3.0 :smileysad:

    Is there another way to accomplish this with PS 3.0 or is that too much of a pain?



  • 9.  RE: Help with Datastore Report
    Best Answer

    Posted Aug 04, 2016 05:19 PM

    Everything is "posh"-ible, just not as elegant in v3 :smileygrin:

    $report = foreach($ds in Get-Datastore | Where-Object {$_.Name -notMatch ("ESX|Datastore1")}){

        Get-VM -Datastore $ds |

        Select @{N='ClusterName';E={Get-DatastoreCluster -Datastore $ds | Select -ExpandProperty Name}},

                @{N="DataStoreName";E={$ds.name}},      

                @{N="Folder";E={$_.folder}},

                @{N="VM";E={$_.Name}}

    }

    $report | Export-Csv report.csv -NoTypeInformation -UseCulture



  • 10.  RE: Help with Datastore Report

    Posted Aug 04, 2016 08:03 PM

    Exactly what I needed. I'll use this till we upgrade to PowerShell v4 in our environment.

    As always Luc thanks for your assistance!