使用从蝙蝠文件调用的Powershell,需要获取Windows进程的所有者/用户,但所有者未显示



我需要在远程计算机上获取某些进程的路径和所有者,并使用批处理文件来获取它。 到目前为止,我已经想出了一些东西,给了我路径,但没有给所有者:

powershell -command "Get-WmiObject -Class win32_process -ComputerName myserver.mydomain.local -Filter 'name like """"%%myprocess%%""""' | select path,GetOwner"

为此,GetOwner 是空白的。

如何使其工作或使用另一个 PS 查询或批处理命令实现相同的目标?

GetOwner(( 是一个方法。 用户是 getowner(( 返回的属性,可能是您想要的。

set myprocess=powershell
powershell "Get-WmiObject win32_process -computername myserver.mydomain.local | where name -match %myprocess% | select path,@{n='Owner';e={$_.getowner().user}}"
Path                                                      Owner
----                                                      -----
C:WindowsSystem32WindowsPowerShellv1.0powershell.exe admin

或者你可以使用 get-process。 编辑:嗯,看起来-computername和-include用户名不能放在一起。 我可以使用"%计算机名%"而不是"本地主机"进行测试。

powershell "get-process -computername %computername% -includeusername *%myprocess%* | select path,username"
Get-Process : Parameter set cannot be resolved using the specified named parameters.
At line:1 char:1
+ get-process -computername DESKTOP-JQ7B7TZ -includeusername *powershel ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidArgument: (:) [Get-Process], ParameterBindingException
+ FullyQualifiedErrorId : AmbiguousParameterSet,Microsoft.PowerShell.Commands.GetProcessCommand

如果远程电源外壳正常工作:

powershell "invoke-command myserver.mydomain.local { get-process -includeusername *%myprocess%* | select path,username }"
Path           : C:WindowsSystem32WindowsPowerShellv1.0powershell.exe
UserName       : DESKTOP-JQ7B7TZadmin
PSComputerName : myserver.mydomain.local
RunspaceId     : b1bc87a7-1c21-4f91-a238-bbb68978ea6c

最新更新