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!
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
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
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
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
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
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
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
Was missing the VMName field in the first row. Works great now - thanks!