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

时间:2015-05-18 10:16:18

标签: powershell wmi

我有一个Windows服务帐户用户,我正在尝试使用WMI win32_proces创建后台进程。但是因未知失败而失败。 (尝试使用管理员,非管理员,域管理员,域非管理员用户。工作正常)

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

2 个答案:

答案 0 :(得分:4)

正如此msdn博客文章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!
  5. # 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
    

答案 1 :(得分:0)

以下评论中的Mathias建议:

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

在您的脚本中。您可以创建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