如何在PowerShell中设置Win32_ProcessStartup参数?

huangapple go评论83阅读模式
英文:

How to set Win32_ProcessStartup parameters in Powershell?

问题

这是我迄今为止的PS代码片段。启动分离的进程运行良好,但我无法设置ProcessStartupInformation。如何实现?此外,这段代码有时在新的PS实例中出现错误代码8或21。到目前为止,我找不到这个错误的规律。但是,当我连续多次运行此代码而不关闭先前创建的记事本进程时,它会发生。

  1. $startup = Get-WmiObject Win32_ProcessStartup
  2. $arguments = @{
  3. CommandLine = 'notepad.exe' # 或者只是 'notepad'?
  4. CurrentDirectory = 'c:\windows\system32'
  5. # ProcessStartupInformation = $startup
  6. }
  7. Invoke-CimMethod -ClassName Win32_Process -MethodName Create -Arguments $arguments

出于某种原因,在最新的Windows 11中,命令 "Get-CimInstance -ClassName Win32_ProcessStartup" 没有返回任何内容。

MSDN有这个VB示例代码,但我不知道如何将其转换为Powershell:

  1. Const HIDDEN_WINDOW = 12
  2. strComputer = "."
  3. Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
  4. Set objStartup = objWMIService.Get("Win32_ProcessStartup")
  5. Set objConfig = objStartup.SpawnInstance_
  6. objConfig.ShowWindow = HIDDEN_WINDOW
  7. Set objProcess = GetObject("winmgmts:root\cimv2:Win32_Process")
  8. errReturn = objProcess.Create("Notepad.exe", null, objConfig, intProcessID)

或者我应该使用其他(更可靠的)选项来启动分离的进程?

更新:
这是我现在使用的代码,但是当多次运行它时仍然会出现烦人的错误代码8和21:

  1. # 启动分离的记事本进程的示例代码
  2. # 参考链接:
  3. # https://learn.microsoft.com/en-us/windows/win32/cimwin32prov/win32-processstartup#properties
  4. # https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-showwindow
  5. $startup = [ciminstance]::new((Get-CimClass 'Win32_ProcessStartup'))
  6. $startup.ShowWindow = 5 # 隐藏=0,正常=5,最小化=7,隐藏=12
  7. $startup.PriorityClass = 16384 # 低于正常
  8. $arguments = @{
  9. CommandLine = 'notepad.exe'
  10. CurrentDirectory = 'c:\windows\system32'
  11. ProcessStartupInformation = $startup
  12. }
  13. Invoke-CimMethod -ClassName Win32_Process -MethodName Create -Arguments $arguments

希望这有所帮助。

英文:

This is the PS-snippet that I have so far. Starting the detached process works fine, but I cannot set the ProcessStartupInformation. How can this be done? Also this code sometimes comes up with error code 8 or 21 in a fresh PS instance. I could not find a pattern for this error till now. But it happens, when I run this code multiple times in a row without closing the previously created notepad-process.

  1. $startup = Get-WmiObject Win32_ProcessStartup
  2. $arguments = @{
  3. CommandLine = 'notepad.exe' # or just 'notepad'?
  4. CurrentDirectory = 'c:\windows\system32'
  5. # ProcessStartupInformation = $startup
  6. }
  7. Invoke-CimMethod -ClassName Win32_Process -MethodName Create -Arguments $arguments

For any reason, the command "Get-CimInstance -ClassName Win32_ProcessStartup" is not returning anything in latest Windows 11 here.

MSDN has this VB-sample code, but I dont know how to convert this into Powershell:

  1. Const HIDDEN_WINDOW = 12
  2. strComputer = "."
  3. Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
  4. Set objStartup = objWMIService.Get("Win32_ProcessStartup")
  5. Set objConfig = objStartup.SpawnInstance_
  6. objConfig.ShowWindow = HIDDEN_WINDOW
  7. Set objProcess = GetObject("winmgmts:root\cimv2:Win32_Process")
  8. errReturn = objProcess.Create("Notepad.exe", null, objConfig, intProcessID)

Or should I better use any other (more reliable) option to start a detached process?

Update:
This is the code I am now using, but still getting this annoying error codes 8 and 21 when running it multiple times:

  1. # sample code starting a detached notepad-process
  2. # referennce:
  3. # https://learn.microsoft.com/en-us/windows/win32/cimwin32prov/win32-processstartup#properties
  4. # https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-showwindow
  5. $startup = [ciminstance]::new((Get-CimClass 'Win32_ProcessStartup'))
  6. $startup.ShowWindow = 5 # hidden=0, normal=5, minimize=7, hidden=12
  7. $startup.PriorityClass = 16384 # below normal
  8. $arguments = @{
  9. CommandLine = 'notepad.exe'
  10. CurrentDirectory = 'c:\windows\system32'
  11. ProcessStartupInformation = $startup
  12. }
  13. Invoke-CimMethod -ClassName Win32_Process -MethodName Create -Arguments $arguments

答案1

得分: 1

创建一个 [ciminstance] 对象,并将类作为第一个参数传递:

  1. # 创建并配置启动对象
  2. $processStartup = [ciminstance]::new((Get-CimClass 'Win32_ProcessStartup'))
  3. $processStartup.ShowWindow = 12
  4. # 附加到参数表
  5. $arguments['ProcessStartupInformation'] = $processStartup
英文:

Create a [ciminstance] object and pass the class as the first argument:

  1. # create and configure startup object
  2. $processStartup = [ciminstance]::new((Get-CimClass 'Win32_ProcessStartup'))
  3. $processStartup.ShowWindow = 12
  4. # attach to argument table
  5. $arguments['ProcessStartupInformation'] = $processStartup

huangapple
  • 本文由 发表于 2023年6月5日 19:20:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76405918.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定