469,362 Members | 2,447 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,362 developers. It's quick & easy.

Using SYSTEM() to run a PowerShell command

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,
Sep 16 '08 #1
4 8893
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
4,059 Expert 2GB
Look into IPC::Open2 or IPC::Open3 both come with perl

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

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\'" ';

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,
Sep 17 '08 #4
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.

Similar topics

8 posts views Thread by Siemel Naran | last post: by
8 posts views Thread by Nick Li | last post: by
1 post views Thread by whatmore | last post: by
reply views Thread by Willem | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.