Trying to call powershell script from Go

1,284 views
Skip to first unread message

Uzair Ally

unread,
Oct 27, 2020, 12:25:32 PM10/27/20
to golang-nuts
Hi,

I am getting the following error when I try to call a powershell script from go.

undefined: script

Here is the code:

cmdName := "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe"
out, err := exec.Command("cmdName", script.ps1).Output()
if err != nil {
fmt.Fprintln(os.Stderr, "Error creating StdoutPipe for Cmd", err)


It looks like go doesn't recognize the powershell script. How do I resolve this error? Any help or guidance will be appreciated. 

Ian Lance Taylor

unread,
Oct 27, 2020, 12:34:47 PM10/27/20
to Uzair Ally, golang-nuts
Your code fragment refers to a variable named "script". Where is that
variable defined?

Can you show us a complete self-contained program, not just a code fragment?

Ian

Uzair Ally

unread,
Oct 27, 2020, 1:12:44 PM10/27/20
to golang-nuts
Hi Ian,

Yes of course, here is the program. I am trying to use the os/exec package to call powershell then a script which is script.ps1. I have the script.ps1 in the same folder as main.go

package main

import (
"fmt"
"os"
"os/exec"
)

func main() {
cmdName := "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe"
out, err := exec.Command("cmdName", script.ps1).Output()
if err != nil {
fmt.Fprintln(os.Stderr, "Error creating StdoutPipe for Cmd", err)
}
fmt.Println(string(out))
}



Marvin Renich

unread,
Oct 27, 2020, 1:22:14 PM10/27/20
to golang-nuts
* Uzair Ally <mua...@gmail.com> [201027 12:25]:
> Hi,
>
> I am getting the following error when I try to call a powershell script
> from go.
>
> undefined: script
>
> Here is the code:
>
> cmdName := "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe"
> out, err := exec.Command("cmdName", script.ps1).Output()

Perhaps you what you intended was:
out, err := exec.Command("cmdName", "script.ps1").Output()

Is your script named script.ps1 or is script a variable with a field
named ps1 containing the name of the script?

> if err != nil {
> fmt.Fprintln(os.Stderr, "Error creating StdoutPipe for Cmd", err)
>
>
> It looks like go doesn't recognize the powershell script. How do I resolve
> this error? Any help or guidance will be appreciated.

...Marvin

Uzair Ally

unread,
Oct 27, 2020, 1:26:53 PM10/27/20
to golang-nuts
Hi Marvin,

If I add script.ps1 in double quotes and try to run, it tells me cmdName declared but no used.
Yes, the script is named script.ps1. The script is not a variable.

Marvin Renich

unread,
Oct 27, 2020, 1:39:34 PM10/27/20
to golang-nuts
* Uzair Ally <mua...@gmail.com> [201027 13:27]:
> Hi Marvin,
>
> If I add script.ps1 in double quotes and try to run, it tells me cmdName
> declared but no used.
> Yes, the script is named script.ps1. The script is not a variable.

I should have recognized that "cmdName" should be cmdName without the
quotes:

out, err := exec.Command(cmdName, "script.ps1").Output()

I was just answering too fast.

Ian Lance Taylor

unread,
Oct 27, 2020, 1:40:58 PM10/27/20
to Uzair Ally, golang-nuts
On Tue, Oct 27, 2020 at 10:27 AM Uzair Ally <mua...@gmail.com> wrote:
>
>
> If I add script.ps1 in double quotes and try to run, it tells me cmdName declared but no used.
> Yes, the script is named script.ps1. The script is not a variable.

Your program has basic issues with using the Go language. I recommend
that you take a look at the Go tour (https://tour.golang.org) or other
Go documentation to learn how to use the language correctly.

You are getting the error "cmdName declared but not used" because you
have declared cmdName but you have not used it. You have a string
"cmdName", but that is a string, not a reference to the variable. The
Go language is not a scripting language.

Ian



> On Tuesday, October 27, 2020 at 8:22:14 PM UTC+3 Marvin Renich wrote:
>>
>> * Uzair Ally <mua...@gmail.com> [201027 12:25]:
>> > Hi,
>> >
>> > I am getting the following error when I try to call a powershell script
>> > from go.
>> >
>> > undefined: script
>> >
>> > Here is the code:
>> >
>> > cmdName := "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe"
>> > out, err := exec.Command("cmdName", script.ps1).Output()
>>
>> Perhaps you what you intended was:
>> out, err := exec.Command("cmdName", "script.ps1").Output()
>>
>> Is your script named script.ps1 or is script a variable with a field
>> named ps1 containing the name of the script?
>>
>> > if err != nil {
>> > fmt.Fprintln(os.Stderr, "Error creating StdoutPipe for Cmd", err)
>> >
>> >
>> > It looks like go doesn't recognize the powershell script. How do I resolve
>> > this error? Any help or guidance will be appreciated.
>>
>> ...Marvin
>>
> --
> 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.
> To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/7868522a-17b1-429e-8ebc-bbcb7c678bbdn%40googlegroups.com.

jake...@gmail.com

unread,
Oct 27, 2020, 1:48:54 PM10/27/20
to golang-nuts
It might help if you posted an actual runnable program, that you have personally run, and the full output.

Uzair Ally

unread,
Oct 27, 2020, 2:15:52 PM10/27/20
to golang-nuts
Hi Ian,

Thanks for your response!
Yeah I'm only getting the  error "cmdName declared but not used"  when I add script.ps1 in double quotes. If I remove the double quotes from script.ps1 then the only error I see is undefined: script.
The variable cmdName is set to call powershell.exe from it's path on the OS. I am then using that variable in out,err to run exec.Command, calling the first argument which is cmdName (the powershell process), and the second argument is set to call the powershell script (script.ps1). This works if I add one powershell command as the second argument, but what I am trying to do here is call an external script. 
I know Go is not a scripting language but I am trying to wrap the powershell script in a "simple" Go program to create an exe. 

Uzair Ally

unread,
Oct 27, 2020, 2:18:25 PM10/27/20
to golang-nuts
Hi Jake,

The code I posted is the runnable go program just missing the powershell script which is a separate file. Maybe I'm miss understanding? Is there something else I can provide to help you understand further?

Marvin Renich

unread,
Oct 27, 2020, 2:38:32 PM10/27/20
to golang-nuts
* Uzair Ally <mua...@gmail.com> [201027 14:19]:
> Hi Jake,
>
> The code I posted is the runnable go program just missing the powershell
> script which is a separate file. Maybe I'm miss understanding? Is there
> something else I can provide to help you understand further?

Based on the your original message and the description you gave later, I
believe my second message will do what you want; i.e.

out, err := exec.Command(cmdName, "script.ps1").Output()

without quotes around cmdName (a variable containing the string
"C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe") and
with quotes around "script.ps1" (the name of the script to run as a
string, not a variable containing a string).

> On Tuesday, October 27, 2020 at 1:26:53 PM UTC-4 mua...@gmail.com wrote:
>> If I add script.ps1 in double quotes and try to run, it tells me cmdName
>> declared but no used.
>> Yes, the script is named script.ps1. The script is not a variable.

In your original code, the compiler recognizes that script is an
identifier that has not been declared and tells you so. At that point,
it has not yet done the analysis to determine if all declared variables
are being used; that analysis happens at a later step in the
compilation, which never occurs if errors are found in prior compilation
steps.

Once you fix the errors in the prior compilation steps (in this case
changing script.ps1 from an undeclared identifier to a string), now the
compiler can perform its "unused variable" analysis, and finds cmdName
declared, but not used. This is because where you intended to use it,
you put it in quotes, making it a string rather than a variable
reference.

...Marvin

jake...@gmail.com

unread,
Oct 28, 2020, 8:00:24 AM10/28/20
to golang-nuts
Technically your code is not runnable, since it does not compile. I misunderstood, and thought you were having a problem with running a powershell script from Go, but actually you are having a problem compiling the code. Very different problems. So never-mind ;-)

Uzair Ally

unread,
Oct 28, 2020, 12:46:08 PM10/28/20
to golang-nuts
Hi Jake.. sorry for the confusion.. appreciate your input!

Uzair Ally

unread,
Oct 28, 2020, 12:47:22 PM10/28/20
to golang-nuts
Hi Marvin,

That was the issue. I removed the quotes from cmdName and the compiler identified it as a variable instead of a string. 
So I updated the code, removing the quotes from cmdName and it compiled, but I'm seeing the err output now and exit status 1 instead of actual output from the commands in the powershell script. Any ideas why the program is giving me the error out instead of running the powershell script? 

CODE:
package main
import (
"fmt"
"os"
"os/exec"
)
func main() {
cmdName := "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe"
out, err := exec.Command(cmdName, "script.ps1").Output()
if err != nil {
fmt.Fprintln(os.Stderr, "Error creating StdoutPipe for Cmd", err)
}
fmt.Println(string(out))
}

ERROR:
Error creating StdoutPipe for Cmd exit status 1

Kurtis Rader

unread,
Oct 28, 2020, 10:42:46 PM10/28/20
to Uzair Ally, golang-nuts
Have you tried running the command directly from a Windows shell prompt? That is, opening a Windows cmd or powershell session and typing this:

    c:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe script.ps1

When I do that, after I create a file named "script.ps1" in my CWD that contains "echo hello", I see a diagnostic error from powershell telling me it did not recognize "script.ps1" as a script file (among other possibilities). When I explicitly make it a relative path (i.e., ".\script.ps1") I see a different error:

    File C:\Users\krade\script.ps1 cannot be loaded because running scripts is disabled on this system.

When I modify your program to use "CombinedOutput()" and print the "out" var regardless of whether an error is returned I see those error messages and the "err" var is "exit status 1".

I'm betting your current problem has nothing to do with Go and instead is due to a misunderstanding of how to use PowerShell on Windows.

--
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.


--
Kurtis Rader
Caretaker of the exceptional canines Junior and Hank

Kurtis Rader

unread,
Oct 28, 2020, 10:56:32 PM10/28/20
to Uzair Ally, golang-nuts
I ran, in an interactive administrative PowerShell session, this command:

    Set-ExecutionPolicy -Scope CurrentUser Unrestricted

After doing that I was able to successfully run your program after changing "script.ps1" to ".\\script.ps1". That is, I saw "hello" and "err" was nil.

On Wed, Oct 28, 2020 at 9:48 AM Uzair Ally <mua...@gmail.com> wrote:
--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/1a532275-eaee-4bbf-872e-11caf38ee1e2n%40googlegroups.com.

Uzair Ally

unread,
Nov 2, 2020, 2:39:46 AM11/2/20
to golang-nuts
Hi Kurtis,

The issue on my end was the same, I had to bypass execution policy for powershell and change script.ps1 to \\script.ps1. Thanks for your help with this, much appreciated! 

Reply all
Reply to author
Forward
0 new messages