pauledwardian
asked on
Powershell
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=$(thr ow "Command line required."))
$path = "\\$computer\root\cimv2:Wi n32_Proces s"
$mc = new-object System.Management.Manageme ntClass $path
$cmdargs = $commandline,$null,$null,0
$ret = $mc.psbase.InvokeMethod("C reate", $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
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=$(thr
$path = "\\$computer\root\cimv2:Wi
$mc = new-object System.Management.Manageme
$cmdargs = $commandline,$null,$null,0
$ret = $mc.psbase.InvokeMethod("C
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
ASKER
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
Either check my work or help me out with a differant code.
Thank you all,
Paul
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?
* 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?
ASKER
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.
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.
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
Does your script work the way you posted it? You still did not say that it does not.
psexec @c:\computers.txt -u Domain\User -p Password -d c:\command1.bat
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.
ASKER
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
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
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.
ASKER
So by looking at the code can you conclude something? Like if it looks good to you or not?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thanks
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