diff --git a/.gitignore b/.gitignore index 79cb12b..95d26b2 100644 --- a/.gitignore +++ b/.gitignore @@ -8,4 +8,5 @@ c/** host.h __debug_bin **debug.test +pkg/powershell/remote_test.go diff --git a/native-powershell b/native-powershell index f0bcdaf..c5aa177 160000 --- a/native-powershell +++ b/native-powershell @@ -1 +1 @@ -Subproject commit f0bcdaf258d0d9089e243137e52230d5a71bc25f +Subproject commit c5aa177f3abcad0f0f124c5708a913656ec4beaf diff --git a/pkg/powershell/powershell.cpp b/pkg/powershell/powershell.cpp index f7d3c75..12f34bb 100644 --- a/pkg/powershell/powershell.cpp +++ b/pkg/powershell/powershell.cpp @@ -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); } diff --git a/pkg/powershell/powershell.h b/pkg/powershell/powershell.h index 12cd281..5fb55c5 100644 --- a/pkg/powershell/powershell.h +++ b/pkg/powershell/powershell.h @@ -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); diff --git a/pkg/powershell/runspace.go b/pkg/powershell/runspace.go index b4cf0a5..2ae2dee 100644 --- a/pkg/powershell/runspace.go +++ b/pkg/powershell/runspace.go @@ -12,6 +12,9 @@ package powershell */ import "C" import ( + "unsafe" + + "golang.org/x/sys/windows" "github.com/KnicKnic/go-powershell/pkg/logger" ) @@ -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)