Hey All,

I was wondering if I am able to run a Powershell script from another program.

Right now this is my code; (This is Turing language)

Dir.Change (“C:/Users/jsargeant2/Documents/Scripts”)
if Error.Last = eNoError then
put “Directory changed”
else
put “Did not change the directory.”
put "Error: ", Error.LastMsg
end if
put "The current execution directory is ", Dir.Current

if not Sys.Exec (“Logging_Functions.ps1”) then
put “The Sys.Exec call failed”
put "Error: ", Error.LastMsg
else
end if

This program changes the directory to where my script is located and then runs it in the next “If” statement.

I got PowerShell to pop up, but there is no text in the window and then it closes shortly after.

I thought it was running but just not showing it. So to test this I had the script just create a text file in an empty folder.

When I run the script it works fine, but not through my program.

p.s I have changed the execution policy to every type and put it back to RemoteSigned for now.

So my question is should this be working or do you have to run PowerShell scripts only from within PowerShell and will not let any other application control it?

p.s I understand that almost no one will know Turing syntax but I thought it was still worth asking the question as I believe there is a “Setting” in PowerShell that I haven’t considered.

Thanks in advance!

6 Spice ups

If you post code, please use the ‘Insert Code’ button. Please and thank you!

codebutton_small.png

In theory yes, but depending on rights (as who the program is run as), execution policy and scope, your results may vary.

You could use your other program to call Powershell.exe and specify the -File parameter to run a .ps1 script.

1 Spice up

You should be able to do it the same way you launch a powershell script with Task Manager, you actually launch the shell, itself, with arguments pointing to your script:

"C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe -NoLogo -NonInteractive -ExecutionPolicy Bypass -File "C:\Scripts\YourScript.ps1"
1 Spice up

From a powershell console, you can do:

Start-Process PowerShell.exe -noexit -nologo SomeCommand -arguments Arg1 Arg2 …
The new console will still be open so you can observe the actions you run from it.

For example, I have a PowerShell script which is my wrapper; I run it from a PowerShell console having my admin rights in a manner similar to the illustration below.

The syntax used in this example affords me the opportunity to have spaces in the final argument which is passed to the new PS Console. It’s great fun!


$script = “\full path to my primary script.ps1”

$commandText = “-noexit -nologo -noprofile -executionPolicy bypass -command $script”

$stuffs = “Some array produced by a query of things needed to be processed”

foreach ($stuff in $stuffs) {

$run = $commandText + " " + “'” + $stuff + “'”

start-process powershell.exe -argument $run

}

I have just figured out the solution to my problem.

Powershell was not set to RemoteSigned… Well it was but not the 32bit version of Powershell which is what my program was trying to run.

Opened the 32bit version as Admin and set it to RemoteSigned as well and now everything works.

Holy crap that was soo easy.