Solved

What is wrong with this for loop?

Posted on 2013-01-22
7
470 Views
Last Modified: 2013-06-16
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?
0
Comment
Question by:Thaidog
  • 3
  • 2
  • 2
7 Comments
 
LVL 35

Accepted Solution

by:
Joseph Daly earned 500 total points
ID: 38807238
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
 
LVL 40

Expert Comment

by:Subsun
ID: 38807310
$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
 

Author Comment

by:Thaidog
ID: 38807358
The first worked - thanks! The second I got an error:

"Cannot process argument transformation parameter 'Datastore'. Strings as pipeline input are not supported."
0
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
LVL 40

Expert Comment

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

Open in new window

0
 

Author Comment

by:Thaidog
ID: 38810412
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
 
LVL 40

Expert Comment

by:Subsun
ID: 38810452
It says  VM with name 'Server1 on' was not found

(Edit: Redacted - Modulus_Twelve)
(http://www.experts-exchange.com/R_20923.html)
0
 
LVL 35

Expert Comment

by:Joseph Daly
ID: 38810453
If my solution worked why are you still looking for another answer?
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
ejb wildfly example 2 25
rename files 4 25
enable or disable exchange activesync for all shared mailbox 5 19
ADModify LDAP Query Help 7 10
I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

837 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question