Solved

Powershell

Posted on 2011-09-13
10
856 Views
Last Modified: 2012-05-12
I am trying to write a powershell script to run a batc hfile that is located on the computers' C Drive. It should reads the name from the computers.txt that is on my C drive.
Please double check my work to see if I have done it correctly. I have NO EXPERIENCE in powershell. So please help.

function global:new-process()
 {
     param ([string]$computer, [string]$commandline=$(throw "Command line required."))
 
     $path = "\\$computer\root\cimv2:Win32_Process"
     $mc = new-object System.Management.ManagementClass $path
 
     $cmdargs = $commandline,$null,$null,0
 
     $ret = $mc.psbase.InvokeMethod("Create", $cmdargs)
     if ($ret -eq 0) {
         write-host "Created Process ID: $($cmdargs[3])"
     }
     else {
         write-host "Error $ret creating process."
     }
 
     $mc.psbase.Dispose()
 }
 
$servers = gc c:\Computers.txt
foreach ($server in $servers){new-process $server "c:\command1.bat"}



Thanks,
Paul
0
Comment
Question by:pauledwardian
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
10 Comments
 
LVL 70

Expert Comment

by:Qlemo
ID: 36532755
I have no idea if the script is correct, but there is no obvious flaw. However, it looks kind of cumbersome.

You main task is to run one and the same batch file remotely on different machines? PowerShell 2.0 allows for remoting to do that (see Invoke-Command) - it requires WinRM (part of PowerShell) to run and be configured on each remote machine, but allows for much more control.

Without PowerShell, just using the free PsExec from www.sysinternals.com, it is a one-liner:
  psexec @c:\computers.txt c:\command1.bat
0
 

Author Comment

by:pauledwardian
ID: 36533115
Can anyone PLEASE help with the code. Like I said Im a beginner.
Either check my work or help me out with a differant code.

Thank you all,
Paul
0
 
LVL 70

Expert Comment

by:Qlemo
ID: 36534238
If you do not clarify your original intention, you might get lousy answers. If an Expert poses a question, you should answer it. For clarity, here are mine:
* Why using PowerShell for this purpose?
* What can we expect to be on the remote side? PowerShell again?
* Is using other tools (like PsExec) an option?
0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 

Author Comment

by:pauledwardian
ID: 36546304
Why using PowerShell for this purpose?
Because it is better than vbscript and easier to figure out.

* What can we expect to be on the remote side? PowerShell again?
It needs to execute a batch file that is located on the C drive of all computers in the domain. (The computers list are in a text file that powershell needs to execute on all of them.)

* Is using other tools (like PsExec) an option?
Not now since we are in hurry and we are looking for a faster way to approach. It would be a good tool if we had more time to spend and learn that tool.
0
 
LVL 70

Expert Comment

by:Qlemo
ID: 36557483
PsExec would be the fastest way - if it works, that is. But having the script stored locally, as you stated, and using the same credentials on each machine, should make it working instantly. As I have shown, it is a one-liner, and easy to run. Using WMI or PS Remoting introduces more issues than it is worth. In particular if you are in a hurry. The more appropriate way to run psexec is
  psexec @c:\computers.txt -u Domain\User -p Password -d c:\command1.bat

Open in new window

as that will detach PsExec from the processes it starts, allowing for parallel execution.

Does your script work the way you posted it? You still did not say that it does not.

0
 

Author Comment

by:pauledwardian
ID: 36561913
I appreciate the psexec line you provided.
But all I needed was to verify my script. It works on computers that I checked but I cannot verify it works on all computers since there are hundreds of them. Thats why I asked if somone can please verify the code to see if it is a right code.

Thanks,
Paul
0
 
LVL 70

Expert Comment

by:Qlemo
ID: 36561955
If it works on the few PCs you tested it against, you can assume it does work for all. Only possible reasons for failure are  login restrictions (credentials not valid for all machines), or machines not reachable.
0
 

Author Comment

by:pauledwardian
ID: 36571488
So by looking at the code can you conclude something? Like if it looks good to you or not?
0
 
LVL 70

Accepted Solution

by:
Qlemo earned 500 total points
ID: 36572013
The only part I would change are the last lines:
$servers = gc c:\Computers.txt
foreach ($server in $servers){new-process $server "c:\command1.bat"}

Open in new window

is usually more PS-ish written
gc c:\Computers.txt | % { new-process $_ "c:\command1.bat" }

Open in new window

Next possible improvement would be to use jobs, which allow for parallel execution. But that would require some job control (e.g. limiting to 10 running jobs), which makes it look more complicated (but indeed it isn't that difficult to accomplish).
0
 

Author Closing Comment

by:pauledwardian
ID: 36714576
thanks
0

Featured Post

Veeam gives away 10 full conference passes

Veeam is a VMworld 2017 US & Europe Platinum Sponsor. Enter the raffle to get the full conference pass. Pass includes the admission to all general and breakout sessions, VMware Hands-On Labs, Solutions Exchange, exclusive giveaways and the great VMworld Customer Appreciation Part

Question has a verified solution.

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

A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
My attempt to use PowerShell and other great resources found online to simplify the deployment of Office 365 ProPlus client components to any workstation that needs it, regardless of existing Office components that may be needing attention.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

623 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