1

I'm trying to write a script to retrieve the username of logged in users from servers, (This is an exercise in working with unformatted string data I'm aware of other methods to get this data so please don't suggest them)

I'm trying to pass a numeric string from a WMI query into a where-object filter This is reading the WMI output from the Win32_LoggedOnUser class

$Name | where {$_.Dependent -like $ID } | select Antecedent

the problem seems to be in reading the ID variable, I've tried several variations which is a value concatenated with some wildcards for the filter format, I was tipped off to this when I converted the string to CSV format so I could more easily do a [regex]::Split, I get an error that the InputObject is null, If I run the above line alone I just get back a null result, if I manually enter the filter string I get the output I want.

[String]$ID = "'*"+$UserSessions.LogonId+"*'"

If I do a write-host I just get back '146771' which is what I seem to want,and get-member shows it to be a [System.String]

then I'm throwing this to a split that grabs the last token which is the username the whole script works fine if I manually enter the filter string just not with the variable in any format I've tried

${ID}   ($ID)   ""$ID""  $($ID)

Here's the full script for reference

$UserSessions = GWMI Win32_LogonSession | where { $_.LogonType -eq 2}

[String]$ID = "'*"+$UserSessions.LogonId+"*'"

$Name = GWMI Win32_LoggedOnUser

$Results = $Name | where {$_.Dependent -like $ID } | select Antecedent

$Split = $Results | ConvertTo-Csv

$Splat =  [regex]::Split($Split, """")[9]

Write-Host "User = $Splat"

1 Answer 1

2

gwmi Win32_LogonSession may produce more than one result, so you need to account for that. Also, splitting produces less fields than you expect.

This worked for me:

gwmi Win32_LogonSession | ? { $_.LogonType -eq 2 } | % {
  $ID = "*$($_.LogonId)*"
  gwmi Win32_LoggedOnUser | ? { $_.Dependent -like $ID } | select Antecedent
} | % {
  $name = ($_.Antecedent -split '"')[3]
  Write-Host "User = $name"
}
Sign up to request clarification or add additional context in comments.

2 Comments

The only real change I see in the var is you're using the $_ Last Pipeline generic variable, not sure why it didn't work for any of the variations, I'm not so sure about parsing multiple objects in the array being an issue, at least in the test circumstances I was using (local home pc) there was only 1 object coming out of that query after my filtering, though you basically did what I was going to ultimately and put it all in a ForEach loop to run against servers, so thanks for that.
Worth Noting, if yo run this against Servers, such as Citrix Xen Servers, like I encounter at work, you'll need to change the "LogonType" filter to "10" Win32_LogonSession class

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.