VMware Cloud Community
nl26348
Contributor
Contributor

Import-csv vm's to get folderpath

Hello,

I need a little help with script below.

I want to import a column vm's from a csv file and get the path folder where the vm exist.

$ExportFilePath = Get-Date -Uformat "c:\temp\%Y%m%d-Export-VMInfolderpath.csv"
$report = @()
$vms = Import-CSV "c:\temp\info.csv"
foreach ($vm in $vms)
{ ???? }
Get-VM $vm.name | Get-View | % {
  $row = "" | select Name, Datastore, Path
  $row.Name = $_.Name
  $row.Datastore = (Get-View $_.Datastore[0]).Summary.Name
  $current = Get-View $_.Parent
  $path = $_.Name
  do {
    $parent = $current
     if($parent.Name -ne "vm"){$path =  $parent.Name + "\" + $path}
     $current = Get-View $current.Parent
  } while ($current.Parent -ne $null)
  $row.Path = $path
  $report += $row
}

Regards,

Harjan

0 Kudos
6 Replies
LucD
Leadership
Leadership

Does this do the trick ?

$ExportFilePath = Get-Date -Uformat "c:\temp\%Y%m%d-Export-VMInfolderpath.csv" 
$report
= @() $vms = Import-CSV "c:\temp\info.csv"
foreach ($vm in $vms){     $vmView = Get-VM $vm.name | Get-View     $row = "" | select Name, Datastore, Path
    $row.Name = $vmView.Name     $row.Datastore = (Get-View $vmView.Datastore[0]).Summary.Name     $current = Get-View $vmView.Parent     $path = $vmView.Name     do {       $parent = $current
      if($parent.Name -ne "vm"){
       
$path =  $parent.Name + "\" + $path
      }      
$current = Get-View $current.Parent     } while ($current.Parent -ne $null)     $row.Path = $path
    $report += $row
}


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

0 Kudos
nl26348
Contributor
Contributor

Hi Luc,

Thank you for answer.

I get the follow error

Get-VM : Cannot validate argument on parameter 'Name'. The argument is null or
empty. Supply an argument that is not null or empty and then try the command ag
ain.
At C:\apps\vmware\pad3.ps1:5 char:21
+     $vmView = Get-VM <<<<  $vm.name | Get-View
    + CategoryInfo          : InvalidData: (:) [Get-VM], ParameterBindingValid
   ationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutom
   ation.ViCore.Cmdlets.Commands.GetVM
0 Kudos
LucD
Leadership
Leadership

What is the layout of your CSV file ?

It should be like this

"Name"

"vm1"

"vm3"

"vm3"


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

0 Kudos
nl26348
Contributor
Contributor

Name
NLSIVM539
NLSIVM530
NLSIVM266



0 Kudos
LucD
Leadership
Leadership

The only explanation I can see, is that there must be 1 or more blank lines in the CSV file.

Can you check ?


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

0 Kudos
nl26348
Contributor
Contributor

Yes, that works.

If I added a blank line the script runs successfully. When I removed the blank line the scripts runs now also.

Thanks!

0 Kudos