Calling Powershell script with parameters from Go.

5,028 views
Skip to first unread message

Arulraja Livingston

unread,
Jul 27, 2015, 4:46:29 PM7/27/15
to golang-nuts
One of my requirement is to call a  power shell script (.ps1) from go. 

I am using the following statement  and getting an error "exec: "powershell": executable file not found in %PATH%"

out, err := exec.Command("powershell", scriptwithparameter).Output()

I have added the  powershell.exe location (C:\Windows\System32\WindowsPowerShell\v1.0\) to the  path environmental variable  as well.

Thanks
Arul

Matt Harden

unread,
Jul 27, 2015, 6:46:50 PM7/27/15
to Arulraja Livingston, golang-nuts
Is the PATHEXT environment variable set by any chance? Does it contain .EXE?

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Matt Harden

unread,
Jul 27, 2015, 6:49:39 PM7/27/15
to Arulraja Livingston, golang-nuts
Does it work if you specify `C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe` instead of "powershell"?

Arulraja Livingston

unread,
Jul 28, 2015, 9:04:41 AM7/28/15
to golang-nuts, arulraja....@gmail.com
no luck. thanks for the idea though.

Arulraja Livingston

unread,
Jul 28, 2015, 12:28:51 PM7/28/15
to golang-nuts, arulraja....@gmail.com
sorry misunderstood your suggestion. I  used the full path for the powershell.exe file  as you have suggested in the exec.command and   it's working. now I am getting  the below error and investigating it now.

script file cannot be loaded because running scripts is disabled on this system. For more

information, see about_Execution_Policies at

http://go.microsoft.com/fwlink/?LinkID=135170.

Arulraja Livingston

unread,
Jul 28, 2015, 12:43:26 PM7/28/15
to golang-nuts, arulraja....@gmail.com
had to set the policy (Set-ExecutionPolicy Unrestricted)  to Unrestricted to make it work.

Konstantin Khomoutov

unread,
Jul 28, 2015, 12:58:55 PM7/28/15
to Arulraja Livingston, golang-nuts
On Tue, 28 Jul 2015 09:28:51 -0700 (PDT)
Arulraja Livingston <arulraja....@gmail.com> wrote:

> sorry misunderstood your suggestion. I used the full path for the
> powershell.exe file as you have suggested in the exec.command and
> it's working. now I am getting the below error and investigating it
> now.
>
> script file cannot be loaded because running scripts is disabled on
> this system.

Yes, in its infinite wisdom Microsoft decided that running scripts in
Powershell is something so unusual it has to be explicitly enabled
like explained in, say, [1].

1. http://superuser.com/a/106363
Reply all
Reply to author
Forward
0 new messages