win32_process 创建失败,服务帐户用户出现未知故障

win32_process create fails with Unknown failure for service account user

我有一个 windows 服务帐户用户,我正尝试使用它创建一个使用 WMI win32_proces 的后台进程。但因未知故障而失败。 (与管理员、非管理员、域管理员、域非管理员用户一起尝试过。工作正常)

 $process = [WMICLASS]"\$computername\ROOT\CIMV2:win32_process"
 $processinfo = $process.Create("powershell.exe -WindowStyle Hidden test.ps1")
 Write-Host $processinfo.returncode

正如此 msdn 博客 post 中所述:Win32_Process.Create fails if user profile is not loaded,WMI 调用被硬编码为通过注册表访问用户配置文件。

如果用户配置文件尚未加载到 HKU,WMI 会尝试使用 RegLoadKey.

将其加载到注册表中

除非相关用户帐户在本地计算机上具有以下权限,否则这将失败:

  • SeRestorePrivilege
  • SeBackupPrivilege

所以,要么

  1. 将这些权限授予相关帐户
  2. 在致电 Win32_Process.Create
  3. 之前,先为相关用户致电 LoadUserProfile
  4. 或者使用 Start-Process 而不是 WMI!

# Set up service account credentials
$Username = "domain\svcaccount"
$Password = "7oPs3çürEûN1c0deZ"
$Credential = New-Object pscredential -ArgumentList $Username,$(ConvertTo-SecureString $Password -AsPlainText -Force)

# Establish a session on the remote machine
$RemoteSession = New-PSSession -ComputerName $computername -Credential $Credential

# Launch the process with Start-Process -LoadUserProfile
Invoke-Command -Session $RemoteSession {
    Start-Process 'powershell.exe' -LoadUserProfile:$true -Argumentlist 'test.ps1' -WindowStyle Hidden 
}

# Cleanup
Remove-PSSession -Session $RemoteSession
Remove-Variable -Name Username,Password,Credential

在下面的评论中给 Mathias 建议:

Start-Process 仅在通过交互式提示调用时才在后台运行。如果 运行 来自 .ps1 脚本,则通过 start-process 创建的进程会在 .ps1 脚本退出时退出。

在你的脚本中。您可以创建一个 New-PSsession,然后将此会话传递给 invoke-command 以触发 start-process。

但是要再次使用 New-PSsession 和 ExitPSSession,如果您没有权限,则必须启用 Enable-PSRemoting 和其他设置。 http://blogs.msdn.com/b/powershell/archive/2009/11/23/you-don-t-have-to-be-an-administrator-to-run-remote-powershell-commands.aspx