Launching a PowerShell script from a 3rd party application.

by newbie at 2012-09-14 09:49:15

I’m trying to launch a PowerShell script as part of a "Control Action" that my application executes. The script itself is simple, it dumps the directory contents to a file.

The script executes successfully from the command line when I run "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -file C:\testing.ps1".

However, when I execute the script as the Control Action, it looks as though the script is opened by notepad, rather than executed by PowerShell (no file with directory contents is created):

23-08-2012 13:49:01,977 CEST INFO [Thread-55] [FileControlPlugin@505] doCommand args=[C:\Windows\winsxs\amd64_microsoft-windows-gpowershell-exe_31bf3856ad364e35_6.1.7600.16385_none_94861149bb66249c\powershell_ise.exe, -file, c:\windows\system32\notepad.exe, C]

The Windows Registry entry is: [HKEY_CLASSES_ROOT\Microsoft.PowerShellScript.1\Shell\Open\Command] @=""C:\Windows\System32\notepad.exe" "%1""

I’ve read the documentation, but can’t figure this out. Is there a way to run the script from my application without associating it with notepad (which I assume is the problem)?
by poshoholic at 2012-09-14 10:33:02
Even if ps1 files are associated with notepad on your system, you still should be able to invoke PowerShell.exe with the -file parameter and your script path to run that script.

Something that has me curious is the fact that your args show powershell_ise.exe, not powershell.exe, which may be the root issue here. You can’t invoke a script while opening powershell_ise.exe from the command line. That is not currently supported.
by newbie at 2012-09-14 17:14:50
Kirk –

Thanks for your reply.

You’re right – I’d copied the wrong ouptut to the forum article, I do apologize. The correct output is below; that said, shows basically the same output:

15-09-2012 02:06:18,586 CEST INFO [Thread-55] [FileControlPlugin@505] doCommand args=[C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe, -file, c:\windows\system32\notepad.exe, C]

We can close the ticket since I made that mistake when posting the article, and it could have something to do with the way the 3rd party application is trying to execute the script.

I’ll see what else I can find out on my side.

Thanks again.
by newbie at 2012-09-14 17:45:59
I finally got it working – at least I get the directory listing in a file.

I added another occurrence of the script "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -file C:\testing.ps1 C:\testing.ps1" in the app, and the log entry now looks like the following – weird, but it appears to have worked:

15-09-2012 02:36:29,558 CEST DEBUG [Thread-74] [Execute@348] Done exit=0 C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -File C:\testing.ps1 c:\windows\system32\notepad.exe C:\testing.ps1
by poshoholic at 2012-09-14 18:53:25
That is weird indeed. It seems like something is modifying your command line when it sees a file that can be opened automatically in notepad. That might explain why you need two (one to spoof whatever is trying to do that, and the other to pass into the file parameter). It definitely feels awkward, but if it’s working for you, that’s at least a step in the right direction.

One other thing that might work for you would be to use the -Command parameter of PowerShell.exe instead and pass it a string like "& C:\testing.ps1". I wonder if that would work without double-passing the filename, because the filename itself would no longer be a parameter on its own to be snatched up by whatever is thinking you want to open the file. Just speculating on that one.

For future reference, when you see a reply on a question that you are happy with, you can click the little green checkmark button in the toolbar for a thread reply to indicate that it resolves your issue. I can also do that (and will since you requested I do so), but I wanted to let you know how to do it as well.