VMware Cloud Community
AGFlora
Enthusiast
Enthusiast
Jump to solution

Help with Datastore Report

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

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

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


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
9 Replies
LucD
Leadership
Leadership
Jump to solution

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


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
AGFlora
Enthusiast
Enthusiast
Jump to solution

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

0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

 


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
AGFlora
Enthusiast
Enthusiast
Jump to solution

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

0 Kudos
AGFlora
Enthusiast
Enthusiast
Jump to solution

Hi Luc,

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

Thanks

0 Kudos
LucD
Leadership
Leadership
Jump to solution

What PowerShell version are you using?

Display $PSVersionTable

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


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
AGFlora
Enthusiast
Enthusiast
Jump to solution

Version 3.0 Smiley Sad

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

0 Kudos
LucD
Leadership
Leadership
Jump to solution

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


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
AGFlora
Enthusiast
Enthusiast
Jump to solution

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

As always Luc thanks for your assistance!

0 Kudos