What is wrong with this for loop?

I am trying to find the state a a list of vms with this for loop:

$VM = get-content C:\Users\PowerShell\left_overs.txt

foreach ($PC in $VM) { get-vm }

Open in new window


Instead of giving me the list of the few VMs in the list it gives me everything in vsphere... any ideas why this is not working?
LVL 1
ThaidogAsked:
Who is Participating?
 
Joseph DalyConnect With a Mentor Commented:
Your not passing the -name parameter to your GET-VM command.

Try this.

$VM = get-content C:\leftovers.txt
foreach ($computer in $VM) {
get-vm -name $computer
}
0
 
SubsunCommented:
$VM = get-content C:\Users\PowerShell\left_overs.txt
foreach ($PC in $VM) { get-vm $PC}


and

Get-content C:\Users\PowerShell\left_overs.txt | get-vm

Should also work..
0
 
ThaidogAuthor Commented:
The first worked - thanks! The second I got an error:

"Cannot process argument transformation parameter 'Datastore'. Strings as pipeline input are not supported."
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
SubsunCommented:
Then Try..
Get-content C:\Users\PowerShell\left_overs.txt | % {get-vm $_}

Open in new window

0
 
ThaidogAuthor Commented:
Did not work:

PS C:\Users\> Get-content C:\Users\SkyDrive\PowerShell\left_overs.txt | % {get-vm $_}
Get-VM : 1/23/2013 10:33:23 AM    Get-VM        VM with name 'Server1 on' was not found using the specified filter(
s).
At line:1 char:77
+ Get-content C:\Users\SkyDrive\PowerShell\left_overs.txt | % {get-vm <<<<  $_}
    + CategoryInfo          : ObjectNotFound: (:) [Get-VM], VimException
    + FullyQualifiedErrorId : Core_OutputHelper_WriteNotFoundError,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetVM

Open in new window


(Edit: Redacted - Modulus_Twelve)
(http://www.experts-exchange.com/R_20923.html)
0
 
SubsunCommented:
It says  VM with name 'Server1 on' was not found

(Edit: Redacted - Modulus_Twelve)
(http://www.experts-exchange.com/R_20923.html)
0
 
Joseph DalyCommented:
If my solution worked why are you still looking for another answer?
0
All Courses

From novice to tech pro — start learning today.