Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

could not execute psexec

1,222 views
Skip to first unread message

Perl Pra

unread,
Dec 7, 2009, 7:29:28 AM12/7/09
to Beginners List
Hi All,

I need to execute "*psexec.exe"* which is in C:\Windows\system32 through
perl scirpt.
.I am getting the error "*'psexec' is not recognized as an internal or
external command,operable program or batch file"*

Below is the perl script.

--- SNIP
#!/usr/bin/perl -w
use strict;
use warnings;
use File::Copy;
use File::Find;

$ENV{path}= $ENV{path} . ';' . 'C:\\Windows\\System32';
my $cmd='psexec' . " " . '\\\\' . 10.1.1.121 . ' -u ' . "Adminuser" . '
-p ' . "adminpassword" . ' -w ' . "C:\\commands\\AutoIT\\ ". "
\"C:\\commands\\AutoIT\\sch.bat\"";
system("$cmd") or die "$!";
--SNIP.


PS: If i directly run the command on dos promt it gets executed, I am
getting error if I run the command through perl script only.
Please help me with the below.

Thanks,
Siva

Shlomi Fish

unread,
Dec 7, 2009, 8:19:31 AM12/7/09
to begi...@perl.org, perl pra
On Monday 07 Dec 2009 14:29:28 perl pra wrote:
> Hi All,
>
> I need to execute "*psexec.exe"* which is in C:\Windows\system32 through
> perl scirpt.
> .I am getting the error "*'psexec' is not recognized as an internal or
> external command,operable program or batch file"*
>
> Below is the perl script.
>

OK, a few notes on the script.

> --- SNIP
> #!/usr/bin/perl -w
> use strict;
> use warnings;

It's good that you are using "strict" and "warnings". You don't need "-w" if
you are using warnings.

> use File::Copy;
> use File::Find;
>
> $ENV{path}= $ENV{path} . ';' . 'C:\\Windows\\System32';

It's $ENV{PATH} - not $ENV{path} - though it may not matter on Win32.

This can better be written as:

$ENV{'PATH'} .= ";C:\\Windows\\System32";

And consider using Env::Path:

http://search.cpan.org/dist/Env-Path/

> my $cmd='psexec' . " " . '\\\\' . 10.1.1.121 . ' -u ' . "Adminuser" . '
> -p ' . "adminpassword" . ' -w ' . "C:\\commands\\AutoIT\\ ". "
> \"C:\\commands\\AutoIT\\sch.bat\"";
> system("$cmd") or die "$!";

It seems like you can use the list syntax of perldoc -f system here, which
will save you a lot of problems. This whole code is very unreadable.

Regards,

Shlomi Fish

> --SNIP.
>
>
> PS: If i directly run the command on dos promt it gets executed, I am
> getting error if I run the command through perl script only.
> Please help me with the below.
>
> Thanks,
> Siva
>

--
-----------------------------------------------------------------
Shlomi Fish http://www.shlomifish.org/
"Star Trek: We, the Living Dead" - http://shlom.in/st-wtld

Bzr is slower than Subversion in combination with Sourceforge.
( By: http://dazjorz.com/ )

Robert Wohlfarth

unread,
Dec 7, 2009, 11:53:22 AM12/7/09
to Beginners List
On Mon, Dec 7, 2009 at 6:29 AM, perl pra <per...@gmail.com> wrote:

> Hi All,
>
> I need to execute "*psexec.exe"* which is in C:\Windows\system32 through
> perl scirpt.
> .I am getting the error "*'psexec' is not recognized as an internal or
> external command,operable program or batch file"*
>

[snip]

> my $cmd='psexec' . " " . '\\\\' . 10.1.1.121 . ' -u ' . "Adminuser" . '
> -p ' . "adminpassword" . ' -w ' . "C:\\commands\\AutoIT\\ ". "
> \"C:\\commands\\AutoIT\\sch.bat\"";
> system("$cmd") or die "$!";
>

Try using 'psexec.exe' as the command. It's been a while since I've used my
Windows scripts. And I seem to remember having to use the extension. Or
maybe that's just old age...

--
Robert Wohlfarth

C.DeRykus

unread,
Dec 7, 2009, 8:51:50 AM12/7/09
to begi...@perl.org
On Dec 7, 4:29 am, perl...@gmail.com (Perl Pra) wrote:
> Hi All,
>
> I need to execute "*psexec.exe"* which is in C:\Windows\system32 through
> perl scirpt.
> .I am getting  the error "*'psexec' is not recognized as an internal or
> external command,operable program or batch file"*
>
> Below is the perl script.
>
> --- SNIP
> #!/usr/bin/perl -w
> use strict;
> use warnings;
> use File::Copy;
> use File::Find;
>
>  $ENV{path}= $ENV{path} . ';' .  'C:\\Windows\\System32';
> my  $cmd='psexec' . " " . '\\\\' . 10.1.1.121 . ' -u ' .  "Adminuser"  . '
> -p ' . "adminpassword" . ' -w ' .  "C:\\commands\\AutoIT\\ ".  "
> \"C:\\commands\\AutoIT\\sch.bat\"";
> system("$cmd") or die "$!";


Did you already try printing out $cmd before calling 'system" to
see what actually gets passed ...?

Also, in case of error, the error value will be found in $?
normally.
Only if the return is -1 can $! be inspected for the error. See
perldoc -f system.

And your expression reports an error only if the system return
is zero. That's backwards since a successful call exits with 0.
At least normally...

Here's what the doc (perldoc -f system) recommends:

system ($cmd );
if ($? == -1) {
print "failed to execute: $!\n";
}
elsif ($? & 127) {
printf "child died with signal %d, %s coredump\n",
($? & 127), ($? & 128) ? 'with' : 'without';
}
else {
printf "child exited with value %d\n", $? >> 8;
}

> PS: If i directly run the command on dos promt it gets executed, I am
> getting error if I run the command through perl script only.
> Please help me with the below.
>

--
Charles DeRykus

Bob goolsby

unread,
Dec 7, 2009, 2:32:35 PM12/7/09
to per...@gmail.com, begi...@perl.org
Funniest thing -- when I run psexc from the command line I get:


C:\Documents and Settings>psexec


'psexec' is not recognized as an internal or external command,

operable program or batch file.

C:\Documents and Settings>psexec.exe
'psexec.exe' is not recognized as an internal or external command,
operable program or batch file.

C:\Documents and Settings>


Rather odd, you think?

Further investigation yields the fact the PsExec is a "PsExec is a
light-weight telnet-replacement that lets you execute ... Just copy
PsExec onto your executable path. Typing "PsExec" displays its
usage...." (From the fine on-line documentation.)

Sounds like you have an installation issue ....


B

> --
> To unsubscribe, e-mail: beginners-...@perl.org
> For additional commands, e-mail: beginne...@perl.org
> http://learn.perl.org/
>
>
>

Perl Pra

unread,
Dec 8, 2009, 12:11:47 AM12/8/09
to begi...@perl.org
Hi All,

I have done the following.

printed the cmd variable and check what actually got passed. If I copy paste
the printed command on dos prompt it worked fine.

I think its not intallation issues as I could execute the command from the
dos prompt.

Thanks,
siva

Bob goolsby

unread,
Dec 8, 2009, 1:08:32 AM12/8/09
to perl pra, begi...@perl.org
Note the last-but-one line that I quoted from the PSexec Doc:

"Just copy PsExec onto your executable path. "

I strongly suspect that you are NOT running the same environment in
Perl as you are when you run from the command line. Check your PATH
variables and fix your installation to include ALL of the directories
that you need.


B

C.DeRykus

unread,
Dec 8, 2009, 12:45:30 AM12/8/09
to begi...@perl.org
On Dec 7, 9:11 pm, perl...@gmail.com (Perl Pra) wrote:
> Hi All,
>
> I have done the following.
>
> printed the cmd variable and check what actually got passed. If I copy paste
> the printed command on dos prompt it worked fine.
>
> I think its not intallation issues as I could execute the command from the
> dos prompt.
>

Did you correct the system call ? Examine the $? return...?

> system("$cmd") or die "$!"; # incorrect: see earlier response

--
Charles DeRykus

harish behl

unread,
Jan 19, 2010, 5:24:02 AM1/19/10
to begi...@perl.org
Hi All,
 
I am a newbie to Perl Script. I need your help in one of my perl Script.
My Script has to search a particular string like "\$Header\$" in files and if this string is found, it should replace it with "\$Header $NameofFile 01/01/2009". But it's making the files blanks.
 
Please look at the below code wriiten by me and guide me to correct the same:
 
#! /local/bin/perl -w
#use strict;
use File::Basename;

my $src_dir;
print "Specify top directory location where all product XML/JAVA files exist  ==> ";
$src_dir = validate_dir("s") or return;
sub validate_dir {
    my ($dir_type) = @_;
    my $dir;
    print "Directory is : $dir_type\n";
    while (1)
    {
        $dir = <STDIN>;
        chomp($dir);
        print "$dir\n";
        if ($dir_type eq 's') {
            if (!(-d $dir)) {
                print "ERROR : source directory $dir does not exist.\n";
                print "Specify valid source directory  ==> ";
            }
            else {
                last;
            }
        }
    }
    return $dir;
}
print "Source Directory : $src_dir\n";
update_attributesets();
sub update_attributesets {
    my %map;
    my $text;
    if (-f $src_dir) {
        @ARGV = ($src_dir);
    }
    else {
 @ARGV = qx!find $src_dir -name \'*.*\'!;
    }
foreach my $filename (@ARGV)
 {
 
        chomp($filename);
        print "$filename\n";
my $var='\$Header\$';
open(READFILE, "<$filename");
      my @lines = <READFILE>;
     my $modified=0;
      close READFILE;
     open(WRITEFILE,">$filename");
      foreach my $line (@lines)
       {
 my $fname = basename($filename );
 my $result=index($line,$var);
 if ($result!=-1)
 {
         s/$var/\$Header: $fname 120.0 2007\/11\/27 07:59:52 atgops1 noship \$/;
         print WRITEFILE $_;
    ++$modified;
 }
       }
       close WRITEFILE;
}
print ' Modified files are $modified\n';
}
 
Thanks,
Harish

 
 


The INTERNET now has a personality. YOURS! See your Yahoo! Homepage. http://in.yahoo.com/

Erez Schatz

unread,
Jan 19, 2010, 5:57:32 AM1/19/10
to harish behl, begi...@perl.org
2010/1/19 harish behl <harish_...@yahoo.com>:

> My Script has to search a particular string like "\$Header\$" in files and if this string is found, it should replace it with "\$Header
> $NameofFile 01/01/2009". But it's making the files blanks.

This means you have a problem in the part that is writing to the
files, mainly here:

>       foreach my $line (@lines)

You correctly assign each element from @lines to $line, but, the
substitution and print are made on the default variable ($_):

>          s/$var/\$Header: $fname 120.0 2007\/11\/27 07:59:52 atgops1 noship \$/;
>          print WRITEFILE $_;

Which means the substitution doesn't happen and the file's getting
overridden with whatever's in the default variable, in your case,
apparently nothing.

There are several other issues of correction and practice in your
program, and I do encourage you to uncomment 'use strict' in your
second line, but I believe that's the immediate solution to your
issue.

--
Erez

"The government forgets that George Orwell's 1984 was a warning, and
not a blueprint"
http://www.nonviolent-conflict.org/ -- http://www.whyweprotest.org/

harish behl

unread,
Jan 19, 2010, 7:10:06 AM1/19/10
to erez....@gmail.com, begi...@perl.org

--- On Tue, 19/1/10, Erez Schatz <moon...@gmail.com> wrote:


Hi Erez,
 
Thanks for your prompt reply. I have made the changes as per your suggestions.
like
foreach   (@lines)


       {
 my $fname = basename($filename );

         s/$var/\$Header: $fname 120.0 2007\/11\/27 07:59:52 atgops1 noship \$/;
         print WRITEFILE $_;
    
      }

Now, the script is working fine!!!!
 
Regards,
Harish
 
 


2010/1/19 harish behl <harish_...@yahoo.com>:

>       foreach my $line (@lines)

--
Erez


Send free SMS to your Friends on Mobile from your Yahoo! Messenger. Download Now! http://messenger.yahoo.com/download.php

0 new messages