VMware Cloud Community
AGFlora
Enthusiast
Enthusiast
Jump to solution

Trouble with running script using the Get-Content cmdlet

#Code to generate Report...

##-------------------------

$vm = gc "vm.txt"

&{foreach($vm in Get-VM -Location $DC){

    $parent = Get-View $vm.ExtensionData.Parent

    $path = $vm.Name

    while($parent.Parent){

        if($parent.Name -ne "vm"){

            $path = $parent.Name + "\" + $path

        }

        $parent = Get-View $parent.parent

    }

   

   #<#

    Get-Datastore -VM $vm |

    Select @{N="Cluster";E={Get-Cluster -VM $vm | Select -ExpandProperty Name}},

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

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

    @{N="VM Path";E={$path}},

    @{N="ProvisionedStorage";E={Get-FriendlyUnit -value ($vm.ProvisionedSpaceGB * 1GB) | %{"{0,7:f2} {1,2}" -f $_.Value,$_.Unit}}},

    @{N="UsedStorage";E={Get-FriendlyUnit -value ($vm.UsedSpaceGB * 1GB) | %{"{0,7:f2} {1,2}" -f $_.Value,$_.Unit}}}

 

   

}} $report | Export-Xlsx -Path $ExcelFile -WorksheetName $DC -AppendWorksheet -SheetPosition end

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

You could just pipe this to the Export-Xlsx function

   Get-VM -Name $vm |

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

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

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

    @{N="VM Path";E={$path}},

    @{N="ProvisionedStorage";E={Get-FriendlyUnit -value ($_.ProvisionedSpaceGB * 1GB) | %{"{0,7:f2} {1,2}" -f $_.Value,$_.Unit}}},

    @{N="UsedStorage";E={Get-FriendlyUnit -value ($_.UsedSpaceGB * 1GB) | %{"{0,7:f2} {1,2}" -f $_.Value,$_.Unit}}} |

  Export-Xlsx -Path $ExcelFile -WorksheetName $DC -AppendWorksheet -SheetPosition end


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

View solution in original post

0 Kudos
10 Replies
LucD
Leadership
Leadership
Jump to solution

How are the VM names stored in that file vm.txt ?


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

0 Kudos
AGFlora
Enthusiast
Enthusiast
Jump to solution

vm1

vm2

vm3

vm4

0 Kudos
LucD
Leadership
Leadership
Jump to solution

So where do you have a problem ?

When I do

$vm = Get-Content vm.txt

Get-Datastore -VM $vm

it works without an issue.

Or do you get another error ?

Perhaps include the error message.


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

0 Kudos
AGFlora
Enthusiast
Enthusiast
Jump to solution

there is no error. The results are all of the vm's in the datacenter instead of just the ones listed in the vm.txt file.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

But you are asking for datastores if I'm correct ?

That's what the 'Get-Datastore -VM $vm' does.


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

0 Kudos
AGFlora
Enthusiast
Enthusiast
Jump to solution

DOH!. How could we change the script to get the same info for just the vms in the vm.txt file?

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Did you already try something like this

   Get-VM -Name $vm |

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

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

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

    @{N="VM Path";E={$path}},

    @{N="ProvisionedStorage";E={Get-FriendlyUnit -value ($_.ProvisionedSpaceGB * 1GB) | %{"{0,7:f2} {1,2}" -f $_.Value,$_.Unit}}},

    @{N="UsedStorage";E={Get-FriendlyUnit -value ($_.UsedSpaceGB * 1GB) | %{"{0,7:f2} {1,2}" -f $_.Value,$_.Unit}}}


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

0 Kudos
AGFlora
Enthusiast
Enthusiast
Jump to solution

actually that works but how do I export to excel file?

$report | Export-Xlsx -Path $ExcelFile -WorksheetName $DC -AppendWorksheet -SheetPosition end

0 Kudos
LucD
Leadership
Leadership
Jump to solution

You could just pipe this to the Export-Xlsx function

   Get-VM -Name $vm |

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

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

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

    @{N="VM Path";E={$path}},

    @{N="ProvisionedStorage";E={Get-FriendlyUnit -value ($_.ProvisionedSpaceGB * 1GB) | %{"{0,7:f2} {1,2}" -f $_.Value,$_.Unit}}},

    @{N="UsedStorage";E={Get-FriendlyUnit -value ($_.UsedSpaceGB * 1GB) | %{"{0,7:f2} {1,2}" -f $_.Value,$_.Unit}}} |

  Export-Xlsx -Path $ExcelFile -WorksheetName $DC -AppendWorksheet -SheetPosition end


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

0 Kudos
AGFlora
Enthusiast
Enthusiast
Jump to solution

Yeah I removed $Report...Thanks

0 Kudos