Learning Powershell

I am trying to use the following command:

Get-Process -cn (cat C:\officemachines.txt)

1) It tells me" Get-Process : Couldn't connect to remote machine." If I specifiy each machine in the text file individully by copying each "cn" it infact works: Get-Process -cn computername1, computername2, etc etc

What am I missing here?
KonfigurationKingAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

arnoldCommented:
http://technet.microsoft.com/en-us/library/ee176843.aspx
This is an example that will list the contents of the file.

The commandlets you are using acts on a system at a time.  You seem to be trying to pass it a list of all your systems.
0
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
It should work that way, if your computer names are on individual lines. Please post an example file if you still have issues, as the file has to be the culprit (or you are doing some more operations in the subexpression after -cn).
0
KonfigurationKingAuthor Commented:
This is what the text file would look like:

computer1
computer2
computer3
etc...


It is just computers on seperate lines and it does not seem to work.
0
Problems using Powershell and Active Directory?

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

arnoldCommented:
Use the loop though file passing the read line to the get-process cmdlet.
0
KonfigurationKingAuthor Commented:
I am currently at the beginner’s stage of beginning. Could you provide example syntax?
0
arnoldCommented:
Ref the link posted earlier.



Get-Content c:\officemachines.txt | Foreach-Object {Get-Process -cn}
0
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
Correction:
Get-Content c:\officemachines.txt | Foreach-Object {Get-Process -cn $_}

Open in new window

0
KonfigurationKingAuthor Commented:
It is still giving error message:

Get-Process Couldn't connect to remote machine.
At line:1 Char65

Get-Content c:\officemachines.txt | Foreach-Object {Get-Process <<<< -cn $_}
+CategoryInfo :NotSpecified (:) [Get-Process], InvalidOperationException
+FullyQualifiedErrorId :system.INvalidOperationException,Microsoft.Powershell.Commands.GetProcessCommand



I even even tried using the FQDN instead of the netbios name. When that did not work I used the ip address and still no luck :(


Is there something that has to be enabled for this to work? The remote machines are Vista while the connecting machine is 7.
0
arnoldCommented:
Could not connect to the remote machine is an indication that something else is going on rather that the script is not working.


Get-Content c:\officemachines.txt | Foreach-Object {write-host $_; Get-Process -cn $_}
this works for me including on windows 7

The error only pops up when the system being polled is inaccessible or does not exist in DNS.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
KonfigurationKingAuthor Commented:
Get-Content c:\officemachines.txt | Foreach-Object {Get-Wmiobject -computername $_ win32_bios}


The above command works fine with the txt file.....
0
KonfigurationKingAuthor Commented:
WOW Thanks Arnold that worked perfect!!!! Why do you have to specifiy the 'write-host' option I wonder?
0
arnoldCommented:
If you do not specify the write-host, you will not have a separator that identifies which processes are running on which of your systems.  you will have just an output of processes
get-process -cn $_ only prints the header once.

host1
header
list of processes
host2
list of processes
host3
list of processes
0
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
Get-Process creates System.Diagnostics.Process objects, and one of their properties is MachineName, which contains the computer name. The default formatting only shows a few of the available properties. Never trust that list on any cmdlet, as most output is reduced by default - check the available properties by using ft * (the same as format-table *), fl * (the same as format-list *, select * or (best) Get-Member at the end of a pipeline.

Note that the accepted solution does not offer a solution in regard of your issue of getting error messages if the target is not available.

Lastly, I've tried again with the very first command, and it still works! I really don't know what you've put into your text file, there has to be something else (like a trailing space or tab).
0
arnoldCommented:
The issue relates to a host entry that is not resolvable.  I.e. a workstation that has a DHCP IP that has been off such that the DNS entry expired and has been deleted.

adding a test to see whether the host can be resolved before proceeding to the get-process will eliminate the error.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Legacy OS

From novice to tech pro — start learning today.