su

package module
v0.2.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 29, 2022 License: MIT Imports: 2 Imported by: 2

README

go-windows-su

Switch or test administrator's mode

Switch

package main

import (
    "os"
    "path/filepath"

    "github.com/nyaosorg/go-windows-su"
)

func main() {
    windir := os.Getenv("SystemRoot")
    hosts := filepath.Join(windir, `system32\drivers\etc\hosts`)

    su.ShellExecute(su.RUNAS,
        "notepad",
        hosts,
        `C:\`)
}

If you call the executable on the network by su.ShellExecute, give the parameter the path filtered by filepath.EvalSymlinks.

Test

package main

import (
    "fmt"
    "os"

    "github.com/nyaosorg/go-windows-su"
)

func main() {
    flag, err := su.IsElevated()
    if err != nil {
        fmt.Fprintln(os.Stderr, err.Error())
        os.Exit(2)
    } else if flag {
        fmt.Println("Administrator mode")
        os.Exit(0)
    } else {
        fmt.Println("Not administrator mode")
        os.Exit(1)
    }
}

Sample Application

source

sucopy SRC DST

Execute copy built in CMD.EXE in Administrator-mode. sucopy keeps network-drives.

Documentation

Index

Constants

View Source
const (
	// EDIT is the action "edit" for ShellExecute
	EDIT = "edit"
	// EXPLORE is the action "explore" for ShellExecute
	EXPLORE = "explore"
	// OPEN is the action "open" for ShellExecute
	OPEN = "open"
	// PRINT is the action "print" for ShellExecute
	PRINT = "print"
	// PROPERTIES is the action "properties" for ShellExecute
	PROPERTIES = "properties"
	// RUNAS is the action "runas" for ShellExecute
	RUNAS = "runas"
)
View Source
const (
	HIDE            = 0
	SHOWNORMAL      = 1
	SHOWMINIMIZED   = 2
	MAXIMIZE        = 3
	SHOWNOACTIVATE  = 4
	SHOW            = 5
	MINIMIZE        = 6
	SHOWMINNOACTIVE = 7
	SHOWNA          = 8
	RESTORE         = 9
	SHOWDEFAULT     = 10
	FORCEMINIMIZE   = 11
)

Variables

This section is empty.

Functions

func IsElevated

func IsElevated() (bool, error)

func IsElevationRequired added in v0.1.2

func IsElevationRequired(err error) bool

IsElevationRequired reports whether err is the error object "os/exec".Command.Run() returns when the elevation is required.

func ShellExecute

func ShellExecute(action, path, param, directory string) (int, error)

Types

type Param added in v0.1.3

type Param struct {
	Action    string
	Path      string
	Param     string
	Directory string
	Show      int
}

func (Param) ShellExecute added in v0.1.3

func (i Param) ShellExecute() (int, error)

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL