$dat = query user /server:$SERVER
此查询提供以下数据
USERNAME SESSIONNAME ID STATE IDLE TIME LOGON TIME
>vm82958 console 1 Active 1:28 2/9/2018 9:18 AM
adminhmc 2 Disc 1:28 2/13/2018 10:25 AM
nn82543 3 Disc 2:50 2/13/2018 3:07 PM
我希望获得每个独立用户详细信息,例如STATE
,USERNAME
,ID
详细信息。我试过下面的代码,但它没有提供任何数据
foreach($proc in $dat) {
$proc.STATE # This is not working this command not giving any data.
$proc.ID # This is not working this command not giving any data.
}
请帮我解决这个问题。
$dat.GetType()
的结果是:
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True String System.Object
答案 0 :(得分:0)
这与此StackOverflow post非常相似,但您的数据中有空白字段。
一个解决方案是先解决这个问题。下面的示例,但这可能会破坏与您的示例非常不同的给定数据。有关更强大和完整的解决方案,请参阅Matt's comment
# replace 20 spaces or more with TWO commas, because it signifies a missing field
$dat2 = $dat.Trim() -replace '\s{20,}', ',,'
# replace 2 spaces or more with a single comma
$datTable = $dat2.Trim() -replace '\s{2,}', ',,'
foreach($proc in $datTable) {
$proc.STATE
$proc.ID
}
答案 1 :(得分:0)
另一种选择是使用string.Insert
的固定列,如下所示:
$content = quser /server:$SERVER
$columns = 14,42,46,54,65 | Sort -Descending
$Delimiter = ','
$dat = $content | % {
$line = $_
$columns | % {
$line = $line.Insert($_, $Delimiter)
}
$line -replace '\s'
} |
ConvertFrom-Csv -Delimiter $Delimiter
然后:
foreach($proc in $dat) {
$proc.STATE
$proc.ID # Will show the relevant Data
}