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
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
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
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
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
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
Hi Luc,
I just upgraded to VMware vSphere PowerCLI 6.3 Release 1 build 3737840 but getting the same error.
Thanks
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
Version 3.0
Is there another way to accomplish this with PS 3.0 or is that too much of a pain?
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
Exactly what I needed. I'll use this till we upgrade to PowerShell v4 in our environment.
As always Luc thanks for your assistance!