Run Commands as Administrator in Golang on Windows with 'runas'
In Go, you can use the exec package to run a command with elevated privileges using the runas command on Windows.\n\nHere's an example:\n\ngo\npackage main\n\nimport (\n\t"log"\n\t"os/exec"\n)\n\nfunc main() {\n\tcmd := exec.Command("runas", "/user:Administrator", "cmd", "/c", "echo Hello, World!")\n\terr := cmd.Run()\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n}\n\n\nIn this example, we create a new exec.Cmd object with the runas command and pass the desired command to be executed with elevated privileges as arguments. The /user:Administrator option specifies that the command should be run as the Administrator user.\n\nNote that when you run this code, a UAC (User Account Control) prompt will appear asking for permission to run the command with elevated privileges.
原文地址: https://www.cveoy.top/t/topic/qz9O 著作权归作者所有。请勿转载和采集!