modules-gopwsh/powershell.go

46 lines
629 B
Go
Raw Normal View History

2024-10-13 11:38:04 -05:00
package gopwsh
type Powershell struct{}
func NewPowershellHandler() Powershell {
return Powershell{}
}
func RunPowershellCommand(cmd string) (string, error) {
ps := NewPowershellHandler()
shell, err := ps.New()
if err != nil {
return "", err
}
defer shell.Exit()
sout, err := shell.Execute(cmd)
if err != nil {
return sout, err
}
return sout, nil
}
func (ps *Powershell) New() (Shell, error) {
handle, stdIn, stdOut, err := StartProcess(
"powershell.exe",
"-NoExit",
"-NoProfile",
"-Command",
"-",
)
if err != nil {
return nil, err
}
return &shell{
handle,
stdIn,
stdOut,
}, nil
}