我正在尝试获取十六进制输出。这段代码:
get-wmiobject -class "Win32_LoggedOnUser" -namespace "root\CIMV2" -Property * | `
Select @{Name="Antecedent";Expression={$_.Antecedent.Split('=').replace("`"","")[2]}}, `
@{Name="Dependent";Expression={$_.Dependent.Split("`"")[1]}}将生成两列,登录名和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 事件查看器使用系统的十六进制表示形式,例如,‘logonID’将由0x3e7 vs 999或PowerShell表示
ps> '{0:x}' -f 999我正试着与之相匹配。
我正在试着按照这些思路做一些事情:
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]}}但到目前为止还没什么好结果。
发布于 2017-07-04 03:37:54
.Split()是一个字符串方法。因此,$_.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]}
}发布于 2017-07-04 03:42:32
来自@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]}}发布于 2017-07-04 03:31:39
以下是我认为你想要做的一种方法:
Get-WmiObject Win32_LogonSession | ForEach-Object {
[PSCustomObject] @{
"Account" = $_.GetRelated("Win32_Account").Name
"LogonId" = "{0:X}" -f ([Int] $_.LogonId)
}
}看起来LogonId属性是一个字符串,所以您必须强制转换为Int。如果需要,您可以添加0x前缀。
https://stackoverflow.com/questions/44892338
复制相似问题