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!

Calling PowerShell from Perl

Status
Not open for further replies.

superdave67

IS-IT--Management
Sep 19, 2008
3
US
How can I run...

C:\WINDOWS\system32\windowspowershell\v1.0\powersh ell.exe -PSConsoleFile "C:\Program Files\Microsoft\Exchange Server\bin\exshell.psc1" -command ". 'C:\Program Files\Microsoft\Exchange Server\bin\getuserdata.ps1'"

from a Perl script?

I've tried:

system('C:\WINDOWS\system32\windowspowershell\v1.0\powersh ell.exe -PSConsoleFile "C:\Program Files\Microsoft\Exchange Server\bin\exshell.psc1" -command ". 'C:\Program Files\Microsoft\Exchange Server\bin\getuserdata.ps1'"');

but the process has trouble with the mixed quotes and such. The command itself runs fine from a CMD/DOS prompt, but when I attempt to execute it from the system() or exec() command, I get an error displayed on the webpage running the script.

WARNING: The following errors occurred when loading console C:\Program Files\Microsoft\Exchange Server\bin\exshell.psc1: Cannot load Windows PowerShell snap-in Microsoft.Exchange.Management.PowerShell.Admin because of the following error: No Windows PowerShell Snap-ins are available for version 1. Command ". 'C:\Program Files\Microsoft\Exchange Server\bin\getuserdata.ps1'" could not be executed because some Windows PowerShell snap-ins did not load.

I'm thinking it's an IIS permission issue with the command execution... but not quite sure how to verify or rule that out.

Thanks all,
Dave
 
Debugging idea: Try writing what you're trying to run to a file (.bat file perhaps) and examine the file. If you can copy/paste the output into a command window and it works, then you'll at least know that the OS is ok with what you're trying to do. Next see if the .bat file will run.
 
helluva idea... HTML->PERL->BAT->POWERSHELL->PERL->HTML

I'll give that a try!

Thanks,
Dave
 
You can use qx and then any thing you want instead of "'s if that will help you
qx~~ or qx!!
but remember it's like it's surrounded by double quotes.




~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
[URL unfurl="true"]http://search.cpan.org/~rgarcia/perl-5.10.0/pod/perlop.pod#Quote_and_Quote-like_Operators____________________[/url]

qx is for backtick system calls (``), if you want to continue using the system() function, use qq instead.

so...

Code:
system (qq~C:\WINDOWS\system32\windowspowershell\v1.0\powersh ell.exe -PSConsoleFile "C:\Program Files\Microsoft\Exchange Server\bin\exshell.psc1" -command ". 'C:\Program Files\Microsoft\Exchange Server\bin\getuserdata.ps1'"~);

# OR

qx~C:\WINDOWS\system32\windowspowershell\v1.0\powersh ell.exe -PSConsoleFile "C:\Program Files\Microsoft\Exchange Server\bin\exshell.psc1" -command ". 'C:\Program Files\Microsoft\Exchange Server\bin\getuserdata.ps1'"~;

With the qx syntax you don't need the system() thing around it, because backticks are a different kind of system call (the difference is, system calls in backticks return the standard output of the program being called, whereas system calls in system() only return the exit status of the program being called).

-------------
Cuvou.com | My personal homepage
Code:
perl -e '$|=$i=1;print" oo\n<|>\n_|_";x:sleep$|;print"\b",$i++%2?"/":"_";goto x;'
 
Thanks... That didn't work either.

I think this is simply a permissions issue with IIS. The command runs filn from the batch file, as long as I run it from a CMD prompt while logged in as me. But the IIS service (anonymous web account or whatever) may not have the appropriate rights/permissions to run the extended commands.

I've posted the issue on iis.net, so hopefully someone will be able to help me there.

Thank you everyone!
Dave
 
One last idea to get your hopes up only to be dashed by a hidden caveat of Win... Open a CMD window and check out 'help start', specifically the '/I' flag. If you're not already familiar with 'start', it's a command you can use to help control the execution environment of commands you're trying to run.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top