我正在尝试对脚本进行排序,该脚本将检索流程的所有实例以及流程的各个所有者。

我有一个脚本来获取进程名称和开始时间:

get-process -name notepad | select-object starttime,name

我有一个脚本来获取流程所有者:
$process = (Get-CimInstance Win32_Process -Filter "name = 'notepad.exe'")
$owner = Invoke-CimMethod -InputObject $process -MethodName GetOwner | select user | ft -HideTableHeaders

但是,当我创建一个属性并将其放在一起时,我得到的结果几乎可以肯定与格式化有关:
$process = (Get-CimInstance Win32_Process -Filter "name = 'notepad.exe'")
$owner = Invoke-CimMethod -InputObject $process -MethodName GetOwner | select user | ft -HideTableHeaders

get-process -name notepad | select-object starttime,name,@{n='Owner';e={$owner}}

结果:
StartTime                                                               Name                                                                   Owner
---------                                                               ----                                                                   -----
31/01/2017 14:44:57                                                     notepad                                                                {Microsoft.PowerShell.Commands.Internal.Format.FormatStartData, Mic...

从阅读的内容来看,它似乎与$ owner的格式有关,但是我一辈子都无法弄清楚。有任何想法吗?

最佳答案

Format-Table将您的对象转换为格式化的字符串,这对于显示和输出到文本文件非常有用,它将使您要传递的所有对象弄乱。因此,请谨慎使用任何format命令。另外,由于您可能想扩展users属性。

$process = (Get-CimInstance Win32_Process -Filter "name = 'notepad.exe'")
$owner = Invoke-CimMethod -InputObject $process -MethodName GetOwner | select -ExpandProperty user
get-process -name notepad | select-object starttime,name,@{n='Owner';e={$owner}}

关于powershell - 获取流程和流程所有者,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41960800/

10-13 08:52