Skip to content

Commit

Permalink
add initial remote powershell support
Browse files Browse the repository at this point in the history
  • Loading branch information
KnicKnic committed Aug 22, 2019
1 parent 378fc27 commit f3284a3
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ c/**
host.h
__debug_bin
**debug.test
pkg/powershell/remote_test.go

2 changes: 1 addition & 1 deletion native-powershell
9 changes: 8 additions & 1 deletion pkg/powershell/powershell.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,14 @@ NativePowerShell_RunspaceHandle CreateRunspaceHelper(unsigned long long context,
commandPtr = nullptr;
}
return NativePowerShell_CreateRunspace((void*)context, commandPtr, loggerPtr);
// return CreateRunspace(nullptr, Command, Logger);
}

NativePowerShell_RunspaceHandle CreateRemoteRunspaceHelper(unsigned long long context, char useLogger, const wchar_t * remoteMachine, const wchar_t * userName, const wchar_t * password ){
NativePowerShell_LogString loggerPtr = Logger;
if(useLogger == 0){
loggerPtr = nullptr;
}
return NativePowerShell_CreateRemoteRunspace((void*)context, loggerPtr, remoteMachine, userName, password);
}


Expand Down
1 change: 1 addition & 0 deletions pkg/powershell/powershell.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const wchar_t* MallocCopy(const wchar_t* str);
void Logger(const wchar_t* s);

NativePowerShell_RunspaceHandle CreateRunspaceHelper(unsigned long long, char useLogger, char useCommand);
NativePowerShell_RunspaceHandle CreateRemoteRunspaceHelper(unsigned long long context, char useLogger, const wchar_t * remoteMachine, const wchar_t * userName, const wchar_t * password );

void SetGenericPowershellString(NativePowerShell_GenericPowerShellObject* object, wchar_t *value,char autoRelease);
void SetGenericPowerShellHandle(NativePowerShell_GenericPowerShellObject* object, NativePowerShell_PowerShellObject handle,char autoRelease);
Expand Down
38 changes: 38 additions & 0 deletions pkg/powershell/runspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ package powershell
*/
import "C"
import (
"unsafe"

"golang.org/x/sys/windows"
"github.com/KnicKnic/go-powershell/pkg/logger"
)

Expand Down Expand Up @@ -78,6 +81,41 @@ func CreateRunspace(loggerCallback logger.Simple, callback CallbackHolder) Runsp
return context.recreateRunspace()
}


// CreateRemoteRunspace creates a runspace in which to run powershell commands
//
// This function allows you to specify a logging callback
//
// For more details see logger.Simple.
//
// Specify "" for username to not send username and password
//
// You must call Close when done with this object
func CreateRemoteRunspace(loggerCallback logger.Simple, remoteMachine string, username string, password string) Runspace {
context := &runspaceContext{log: logger.MakeLoggerFull(loggerCallback),
callback: nil,
}
context.contextLookup = storeRunspaceContext(context)

var useLogger C.char = 1
if loggerCallback == nil {
useLogger = 0
}


cRemoteMachine, _ := windows.UTF16PtrFromString(remoteMachine)
ptrRemoteMachine := unsafe.Pointer(cRemoteMachine)

cUsername, _ := windows.UTF16PtrFromString(username)
ptrUsername := unsafe.Pointer(cUsername)

cPassword, _ := windows.UTF16PtrFromString(password)
ptrPassword := unsafe.Pointer(cPassword)

context.handle = C.CreateRemoteRunspaceHelper(C.ulonglong(context.contextLookup), useLogger, (*C.wchar_t)(ptrRemoteMachine), (*C.wchar_t)(ptrUsername), (*C.wchar_t)(ptrPassword))
return context.recreateRunspace()
}

// Close and free a Runspace
func (runspace Runspace) Close() {
deleteRunspaceContextLookup(runspace.contextLookup)
Expand Down

0 comments on commit f3284a3

Please sign in to comment.