VMware Cloud Community
WHochradel
Contributor
Contributor
Jump to solution

VM's and their datastore

I am trying to figure out how to write a script that will result in 2 columns, Name and Datastore.  The Name column would contain the VM Name and the Datastore would be the normal datastore name.  I am new to PowerCLI and just getting frustrated  trying to figure this out on my own.  Please help.

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Try it like this

foreach($vm in Get-VM){
    Get-Datastore -vm $vm | Select @{N="VM";E={$vm.Name}},Name
}


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 it like this

foreach($vm in Get-VM){
    Get-Datastore -vm $vm | Select @{N="VM";E={$vm.Name}},Name
}


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

0 Kudos
WHochradel
Contributor
Contributor
Jump to solution

Thanks Luc.  I appreciate it.

0 Kudos
vmhyperv
Contributor
Contributor
Jump to solution

LucD,

  If  want to export in CSV format these output.Please help me on

foreach($vm in Get-VM){
    Get-Datastore -vm $vm | Select @{N="VM";E={$vm.Name}},Name
}  Export-Csv "C:\VM_Datastore.csv" -NoTypeInformation -UseCulture

It shows error

0 Kudos
LucD
Leadership
Leadership
Jump to solution

That's because the Foreach-Object cmdlet doesn't place anything in the pipeline.

A way around this, is to capture the output in a variable and export that one to a CSV file.

Something like this

$report = foreach($vm in Get-VM){
    Get-Datastore -vm $vm | Select @{N="VM";E={$vm.Name}},Name
}
$report | Export-Csv "C:\report.csv" -NoTypeInformation -UseCulture


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

0 Kudos
vmhyperv
Contributor
Contributor
Jump to solution

LucD  I appreciate You.

0 Kudos
nava_thulasi39
Jump to solution

Thanks for the tips. I scrateched my head to bring it into the export-csv.

If you find this or any other answer useful please consider awarding points by marking the answer correct or helpful
0 Kudos
WHochradel
Contributor
Contributor
Jump to solution

Let's say I want to be able to click on a VM from the results and vMotion it over with an Action in PowerGUI, and lets say the action script is what is listed below...Why would I receive the error "Move-VM Invalid / explicitly disabled state operation is invoked. (Move-VM)

$input | ForEach-Object {
$Dest = Read-Host "Where the Heck do you want it now???"
Move-VM -VM $_.Name -Destination $Dest
}

0 Kudos
WHochradel
Contributor
Contributor
Jump to solution

Interestingly enough, I was testing this in our LabManager environment and was receiving the error, but when I run in a Non-LM environment it works like a dream.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

I suspect this is due to the fact that Lab Manager is using so-called linked clones.

And afaik you can't do a Move-VM on these.


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

0 Kudos