VMware Cloud Community
TheVMinator
Expert
Expert
Jump to solution

Check if VMs exist in a vCenter Server

I would like to do the following with PowerCLI:

-Import a text file with a list of VMs

-Check if the VMs exist in vCenter Server

-If so, export them back out to another file

How would I do this in PowerCLI?

Thanks!

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

It looks ok, i just did some reformatting to make it a bit more readable.

Is your CSV file correct ?

1st line the column header "VMname",

on the following lines the names of the VMs you want to check.


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

View solution in original post

0 Kudos
8 Replies
LucD
Leadership
Leadership
Jump to solution

Try something like this

$array = @()

Import-Csv C:\vmnames.csv -UseCulture | %{
 
Try {
   
Get-VM -Name $_.VMName -ErrorAction Stop | Out-Null
  }
 
Catch {
   
return
  }
 
$array += $_ 
}

$array | Export-Csv c:\report.csv -NoTypeInformation -UseCulture

The CSV file with the names should look like this

"VMName"

"VM1"

"lala"

"VM2"


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

TheVMinator
Expert
Expert
Jump to solution

OK thanks - I'm getting an error though when I run it:

unexpected token 'import-csv' in expression or statement.

At [path] char 23

+ $array = @()Import-Csv <<<< c:\vmlist.csv -UseCulture | %{ Try {Get-VM -Name $_.VMName -ErrorAction Stop | Out-Null } Catch { return } $array += $_  }$array | Export-Csv c:\report.csv -NoTypeInformation -UseCulture

+ CategoryInfo : ParserError: (Import-Csv) [], ParseException

+FullyQualifiedErrorId : UnexpectedToken

LucD
Leadership
Leadership
Jump to solution

It seems you lost some <CR><LF> on the copy/paste.

Do you separate lines with this unformatted version ?

$array = @()

Import-Csv C:\vmnames.csv -UseCulture | %{

  Try {

    Get-VM -Name $_.VMName -ErrorAction Stop | Out-Null

  }

  Catch {

    return

  }

  $array += $_ 

}

The forum SW is known to have some problems with IE, is it possible to try another browser ?


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

0 Kudos
TheVMinator
Expert
Expert
Jump to solution

I copied the lines over one line at a teim into powerShell ISE until everthing was exactly the same in terms of line breaks and so on.

When I run the report now, it just doesn't give me any output.  It immediately goes back to a command prompt without errors.  however even though the VMs are in the csv file, they dont get put into the $array variable.  I

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Ok, can you attach the code you are using currently in a file ?

You will have to switch to the "advanced editor" (top-right of the reply box) to be able to attach a file


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

0 Kudos
TheVMinator
Expert
Expert
Jump to solution


thanks - here is the file--

0 Kudos
LucD
Leadership
Leadership
Jump to solution

It looks ok, i just did some reformatting to make it a bit more readable.

Is your CSV file correct ?

1st line the column header "VMname",

on the following lines the names of the VMs you want to check.


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

0 Kudos
TheVMinator
Expert
Expert
Jump to solution

Was missing the VMName field in the first row.  Works great now - thanks!

0 Kudos