0

我正在尝试执行.ps1我用 GoLang 制作的脚本。为了让脚本工作,我需要将执行策略更改为Unrestricted. 为了设置我需要 PowerShell 管理员权限。

下面是我的代码:

package main

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

func main() {
    app := "powershell.exe"
    restrict := "powershell start-process powershell -verb runas"
    path, err := os.Getwd()
    if err != nil {
        log.Println(err)
    }
    path = path + `\script\vpn.ps1`
    fmt.Println(path)
    restriction := "Set-ExecutionPolicy -ExecutionPolicy Unrestricted"
    confirmRestriction := "Get-ExecutionPolicy"

    power := exec.Command(restrict, restriction)
    std, err := power.Output()
    if err != nil {
        fmt.Println(err.Error())
        return
    }
    fmt.Println(string(std))

    verify := exec.Command(restrict, confirmRestriction)
    ver, err := verify.Output()
    if err != nil {
        fmt.Println(err.Error())
        return
    }
    fmt.Println(string(ver))

    fmt.Println(string(std))
    fmt.Println("ELEVATED: Set Execution Policy to Unrestricted")

    arg0 := "-file"
    cmd := exec.Command(app, arg0, path)
    stdout, err := cmd.Output()
    fmt.Println("Connecting to VPN")

    if err != nil {
        fmt.Println(err.Error())
        return
    }

    // Print the output
    fmt.Println(string(stdout))
}

每当我运行它时,它都会失败,当我powershell start-process powershell -verb runas直接在命令提示符下尝试 PowerShell 命令 () 时,我会收到以下错误:

-verb : The term '-verb' is not recognized as the name of a cmdlet, function, script file, or operable program. Check
the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ -verb runas
+ ~~~~~
    + CategoryInfo          : ObjectNotFound: (-verb:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

我目前正在使用 go1.15.15 windows/amd64。

4

0 回答 0