如何在PowerShell中设置Win32_ProcessStartup参数?

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

How to set Win32_ProcessStartup parameters in Powershell?

问题

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

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

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

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

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

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

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

# 启动分离的记事本进程的示例代码

# 参考链接:
# https://learn.microsoft.com/en-us/windows/win32/cimwin32prov/win32-processstartup#properties
# https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-showwindow

$startup = [ciminstance]::new((Get-CimClass 'Win32_ProcessStartup'))
$startup.ShowWindow = 5        # 隐藏=0,正常=5,最小化=7,隐藏=12
$startup.PriorityClass = 16384 # 低于正常

$arguments = @{
    CommandLine = 'notepad.exe'
    CurrentDirectory = 'c:\windows\system32'
    ProcessStartupInformation = $startup
}
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.

$startup = Get-WmiObject Win32_ProcessStartup 
$arguments = @{
    CommandLine = 'notepad.exe' # or just 'notepad'?
    CurrentDirectory = 'c:\windows\system32'
    # ProcessStartupInformation = $startup
}
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:

Const HIDDEN_WINDOW = 12
strComputer = "."
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set objStartup = objWMIService.Get("Win32_ProcessStartup")
Set objConfig = objStartup.SpawnInstance_
objConfig.ShowWindow = HIDDEN_WINDOW
Set objProcess = GetObject("winmgmts:root\cimv2:Win32_Process")
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:

# sample code starting a detached notepad-process 

# referennce: 
# https://learn.microsoft.com/en-us/windows/win32/cimwin32prov/win32-processstartup#properties
# https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-showwindow

$startup = [ciminstance]::new((Get-CimClass 'Win32_ProcessStartup'))
$startup.ShowWindow = 5        # hidden=0, normal=5, minimize=7, hidden=12
$startup.PriorityClass = 16384 # below normal

$arguments = @{
    CommandLine = 'notepad.exe'
    CurrentDirectory = 'c:\windows\system32'
    ProcessStartupInformation = $startup
}
Invoke-CimMethod -ClassName Win32_Process -MethodName Create -Arguments $arguments

答案1

得分: 1

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

# 创建并配置启动对象
$processStartup = [ciminstance]::new((Get-CimClass 'Win32_ProcessStartup'))
$processStartup.ShowWindow = 12

# 附加到参数表
$arguments['ProcessStartupInformation'] = $processStartup
英文:

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

# create and configure startup object
$processStartup = [ciminstance]::new((Get-CimClass 'Win32_ProcessStartup'))
$processStartup.ShowWindow = 12

# attach to argument table
$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:

确定