Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Windows command in Perl...please help! 1

Status
Not open for further replies.

SgtB

IS-IT--Management
Oct 3, 2002
447
US
Need some help with windows perl scripting. I have a command in windows that'd I'd like to run in a perl script.

c:\winnt\system32\rmtshare.exe \\121cogentbdc04\test=d:\users\test /grant "bsdna\domain admins":f /grant bsdna\badmin:f /grant SYSTEM:f /remark:"User Share"

How can I run this in a perl script? I'd also like to substitute some variables in the above command. Is that possible?

I know I have to use the ` backticks, but I cannot get the command to run.

In perl I've done this:

print `c:/winnt/system32/rmtshare \\121cogentbdc04\test=d:\users\test /grant "bsdna\domain admins":f /grant bsdna\badmin:f /grant SYSTEM:f /remark:"User Share"`;

It calls the command correctly, but it then tells me that I've used the command wrong. Now If I take that and run it on command line in Windows it runs fine, so the syntax is fine. Is perl messing up all those backslashes in there?

I'm desperate! Help! ________________________________________
Check out
 
Try this:

@args = ("command", "arg1", "arg2");
system(@args) == 0
or die "system @args failed: $?"You can check all the failure possibilities by inspecting $? like this:

$exit_value = $? >> 8;
$signal_num = $? & 127;
$dumped_core = $? & 128;

Also:
'exec' command will not return a result. I am not sure how system works on a windows system. I have heard of problems with the 'fork'ing processes and win32. Which is how the system command works. The 'system' commands is a better option over 'exec'.

HTH
exec '/bin/echo', 'Your arguments are: ', @ARGV;
exec "sort $outfile | uniq";
 
Got it...

print `c:\\winnt\\system32\\rmtshare.exe \\\\121cogentbdc04\\$shre=d:\\users\\$fold /grant bsdna\\$usr:f /grant SYSTEM:f /grant "bsdna\\domain admins":f`;

Needed an extra backslah after every backslash. Ugly, but it works! ________________________________________
Check out
 
There are many quote and quote-like operators in perl (see the perlop man page). The q{} operator would let you write

[tt] my $cmd = q{c:\winnt\system32\rmtshare.exe \\121cogentbdc04\test=d:\users\test /grant "bsdna\domain admins":f /grant bsdna\badmin:f /grant SYSTEM:f /remark:"User Share"};

print "$cmd\n";[/tt]

Because that would have to be one long line, you might like to try the more legible:
[tt] my $cmd = q{
c:\winnt\system32\rmtshare.exe
\\121cogentbdc04
\test=d:\users\test
/grant "bsdna\domain admins":f
/grant bsdna\badmin:f
/grant SYSTEM:f
/remark:"User Share"
};

$cmd =~ s/\s+/ /g; # replace all whitespace combinations
# with single spaces

print "$cmd\n";[/tt]
 
You'll need to double up on the '\' characters in the command, for a start.
Try this, it should show you what I mean.

print "\\";
print "\n";
print "\\\\";


Is that bit of code meant to create a shared folder ? There are other ways of doing that. Have a look at the Win32::NetResource documentation, also look at Win32::perms.
Dave Roth has written a couple of excellent books designed to make the life of a Windows server administrator a lot easier. His web site is at:
There's code available there too.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top