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. On Wed, Oct 28, 2020 at 9:48 AM Uzair Ally <mua...@gmail.com> wrote: > 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 > > > On Wednesday, October 28, 2020 at 7:46:08 PM UTC+3 Uzair Ally wrote: > >> Hi Jake.. sorry for the confusion.. appreciate your input! >> >> >> On Wednesday, October 28, 2020 at 3:00:24 PM UTC+3 jake...@gmail.com >> wrote: >> >>> 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 ;-) >>> On Tuesday, October 27, 2020 at 2:18:25 PM UTC-4 mua...@gmail.com wrote: >>> >>>> 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? >>>> >>>> On Tuesday, October 27, 2020 at 8:48:54 PM UTC+3 jake...@gmail.com >>>> wrote: >>>> >>>>> It might help if you posted an actual runnable program, that you have >>>>> personally run, and the full output. >>>>> >>>>> On Tuesday, October 27, 2020 at 1:26:53 PM UTC-4 mua...@gmail.com >>>>> wrote: >>>>> >>>>>> 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. >>>>>> >>>>>> 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+unsubscr...@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 > <https://groups.google.com/d/msgid/golang-nuts/1a532275-eaee-4bbf-872e-11caf38ee1e2n%40googlegroups.com?utm_medium=email&utm_source=footer> > . > -- Kurtis Rader Caretaker of the exceptional canines Junior and Hank -- 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+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CABx2%3DD_Wn8WXA_QXG7sbXi6MgFcuE1KhZuq9%2BWXJK8Wyq%3DjWOg%40mail.gmail.com.