Created
December 10, 2018 18:51
-
-
Save SteveL-MSFT/57c06c2cf2b2b8ca11d4b8956b511d54 to your computer and use it in GitHub Desktop.
Create Process with NetCredentials only
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
param($commandLine, [pscredential]$credential) | |
$csharp = @' | |
using System; | |
using System.Runtime.InteropServices; | |
public class Advapi32 | |
{ | |
[DllImport("advapi32.dll", SetLastError=true, CharSet=CharSet.Unicode)] | |
public static extern bool CreateProcessWithLogonW( | |
String userName, | |
String domain, | |
String password, | |
LogonFlags logonFlags, | |
String applicationName, | |
String commandLine, | |
CreationFlags creationFlags, | |
UInt32 environment, | |
String currentDirectory, | |
ref STARTUPINFO startupInfo, | |
out PROCESS_INFORMATION processInformation); | |
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] | |
public struct STARTUPINFO | |
{ | |
public uint cb; | |
public string lpReserved; | |
public string lpDesktop; | |
public string lpTitle; | |
public uint dwX; | |
public uint dwY; | |
public uint dwXSize; | |
public uint dwYSize; | |
public uint dwXCountChars; | |
public uint dwYCountChars; | |
public uint dwFillAttribute; | |
public uint dwFlags; | |
public short wShowWindow; | |
public short cbReserved2; | |
public IntPtr lpReserved2; | |
public IntPtr hStdInput; | |
public IntPtr hStdOutput; | |
public IntPtr hStdError; | |
} | |
[StructLayout(LayoutKind.Sequential)] | |
public struct PROCESS_INFORMATION | |
{ | |
public IntPtr hProcess; | |
public IntPtr hThread; | |
public int dwProcessId; | |
public int dwThreadId; | |
} | |
[Flags] | |
public enum CreationFlags | |
{ | |
CREATE_SUSPENDED = 0x00000004, | |
CREATE_NEW_CONSOLE = 0x00000010, | |
CREATE_NEW_PROCESS_GROUP = 0x00000200, | |
CREATE_UNICODE_ENVIRONMENT = 0x00000400, | |
CREATE_SEPARATE_WOW_VDM = 0x00000800, | |
CREATE_DEFAULT_ERROR_MODE = 0x04000000, | |
} | |
[Flags] | |
public enum LogonFlags | |
{ | |
LOGON_WITH_PROFILE = 0x00000001, | |
LOGON_NETCREDENTIALS_ONLY = 0x00000002 | |
} | |
} | |
'@ | |
$type = Add-Type -TypeDefinition $csharp -PassThru | |
$cred = $credential.GetNetworkCredential() | |
$processInfo = $type[2]::new() | |
$type[0]::CreateProcessWithLogonW( | |
$cred.UserName, | |
$cred.Domain, | |
$cred.Password, | |
$type[4]::LOGON_WITH_PROFILE -bor $type[4]::LOGON_NETCREDENTIALS_ONLY, | |
$null, | |
$commandLine, | |
0, | |
0, | |
$null, | |
[ref] $type[1]::new(), | |
[ref] $processInfo | |
) | |
$processInfo |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment