我有相同的代码。
通过WMI创建新流程时如何传递新的环境更改($ ai.EnvironmentVariables =?)
我的示例不起作用,我发出了无效参数(21)
$procClass=[WMICLass]"win32_processstartup"
$ai = $procClass.createinstance()
$ai.ShowWindow = 1
$ai.EnvironmentVariables = @('name', 'testvar')
$ai.X = 0
$ai.Y = 0
$wmiclass = [wmiclass]"win32_process"
$wmiMethodParameters = $wmiclass.psbase.GetMethodParameters("Create")
$wmiMethodParameters.CommandLine = "test "
$wmiMethodParameters.CurrentDirectory = "."
$wmiMethodParameters.ProcessStartupInformation = $ai
$pInfoCreate = $wmiclass.psbase.InvokeMethod("Create",$wmiMethodParameters,$null);
$tempId = $pInfoCreate.ProcessId
$pInfoObject = get-WmiObject -query "select * from win32_process where processid=$tempId"