powershell对象中的十六进制格式

powershell对象中的十六进制格式,powershell,Powershell,我正在尝试获取十六进制输出。此代码: get-wmiobject -class "Win32_LoggedOnUser" -namespace "root\CIMV2" -Property * | ` Select @{Name="Antecedent";Expression={$_.Antecedent.Split('=').replace("`"","")[2]}}, ` @{Name="Dependent";Expression={$_.Dependent.

我正在尝试获取十六进制输出。此代码:

    get-wmiobject -class "Win32_LoggedOnUser" -namespace "root\CIMV2" -Property * | `
      Select @{Name="Antecedent";Expression={$_.Antecedent.Split('=').replace("`"","")[2]}}, `
      @{Name="Dependent";Expression={$_.Dependent.Split("`"")[1]}}
将生成两列,login name和logonID,如下所示:

Antecedent      Dependent
----------      ----------
SYSTEM          999      
LOCAL SERVICE   997      
NETWORK SERVICE 996      
<user>          347528   
<user>          6842494  
<user>          46198354 
<user>          46171169 
DWM-2           223575   
DWM-2           223551  
我正试图与之相匹配

我正试图按照以下思路做一些事情:

    get-wmiobject -class "Win32_LoggedOnUser" -namespace "root\CIMV2" -Property * | `
      Select @{Name="Antecedent";Expression={$_.Antecedent.Split('=').replace("`"","")[2]}}, `
      @{Name="Dependent";Expression={'{0:x}' -f $_.Dependent.Split("`"")[1]}}

但到目前为止运气不好。

这里有一种方法可以做我认为你想做的事情:

Get-WmiObject Win32_LogonSession | ForEach-Object {
  [PSCustomObject] @{
    "Account" = $_.GetRelated("Win32_Account").Name
    "LogonId" = "{0:X}" -f ([Int] $_.LogonId)
  }
}
似乎
LogonId
属性是一个字符串,因此必须转换为
Int
。如果需要,可以添加
0x
前缀。

.Split()
是一种字符串方法。因此,
$\uu.Dependent.Split()
的输出将是一个
[string]
,您需要将
int
转换为
hex
。因此,在转换之前,您需要转换为
int
。快速查看
Dependent
也会发现,对于
[int32]
来说,数字太大,因此
[int64]
最好。正如@LotPings所指出的,您需要在格式化字符串中添加
0x
前缀

 Get-WmiObject -Class "Win32_LoggedOnUser" -Namespace "root\CIMV2" -Property Antecedent,Dependent |
      Select @{
           Name = 'Antecedent'
           Expression = {$_.Antecedent.Split('=').replace('"','')[2]}
      }, @{
           Name = 'Dependent'
           Expression = {'0x{0:x}' -f [int64]$_.Dependent.Split('"')[1]}
      }

从@LotPings&@BenH更新的最终代码

get-wmiobject -class "Win32_LoggedOnUser" -namespace "root\CIMV2" -Property * | `
  Select @{Name="Antecedent";Expression={$_.Antecedent.Split('=').replace("`"","")[2]}}, `
  @{Name="Dependent";Expression={'0x{0:x}' -f [int64]$_.Dependent.Split("`"")[1]}}

在上一次
Get WmiObject
调用中,您想做什么?您只需要先转换为int。请尝试以下操作:
'{0:x}'-f[int64]$\u.Dependent.Split(“)[1]}
@BenH若要使用前缀0x,必须将其插入格式字符串
'0x{0:x}'-f[int64]$\u.Dependent.Split(“
”)[1]}@LotPings中,谢谢!这起作用了,除了DWM-2帐户没有普及什么是“DWM-2帐户”?DWM代表桌面窗口管理器。它们是新帐户Win8+这两种情况都使用WMI和相同的类。也许可以将
Name
属性更改为其他属性,例如
Caption
get-wmiobject -class "Win32_LoggedOnUser" -namespace "root\CIMV2" -Property * | `
  Select @{Name="Antecedent";Expression={$_.Antecedent.Split('=').replace("`"","")[2]}}, `
  @{Name="Dependent";Expression={'0x{0:x}' -f [int64]$_.Dependent.Split("`"")[1]}}