By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,904 Members | 1,089 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,904 IT Pros & Developers. It's quick & easy.

Using SYSTEM() to run a PowerShell command

P: 2
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?

And I need all the quotes and double-quotes in order for the command to execute properly... at least I think.

This is the last piece to my two week puzzle.

Thanks all,
Dave
Sep 16 '08 #1
Share this Question
Share on Google+
4 Replies


P: 3
I would first create a variable with the entire command

$variable = ' command spaces, double_quotes, etc ... ';

Expand|Select|Wrap|Line Numbers
  1. $my_variable = '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'" ';
Notice I use single quotes to declare the variable, this ignores all double quotes or special characters.

Then I would use the system() function to execute the script. Keep in mind the system function will wait until the process is calling finishes.

hope this helps
Sep 16 '08 #2

KevinADC
Expert 2.5K+
P: 4,059
Look into IPC::Open2 or IPC::Open3 both come with perl

Wrong thread..... ignore the above suggestions
Sep 17 '08 #3

P: 2
Thanks...

I've used your suggestion, but get a CGI error on execution.
In order to get past the CGI error, and actually get some output, I escaped out the single quotes as such:

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


and then get this:

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.


Being that this will be run by the IIS service (or whatever) do I need to worry about permissions and such?

Thanks again,
Dave
Sep 17 '08 #4

P: 3
The problem is in the single quotes, you can only use single quote (') to open and close the variable.

This variable should work:
$ps_exec = 'C:\WINDOWS\system32\windowspowershell\v1.0\powers hell.exe -PSConsoleFile "C:\Program Files\Microsoft\Exchange Server\bin\exshell.psc1" -command ". \" C:\Program Files\Microsoft\Exchange Server\bin\getuserdata.ps1\" " ';
Jan 11 '10 #5

Post your reply

Sign in to post your reply or Sign up for a free account.