3

I am parsing all logins for a Windows system ($system). I'm trying to get a tuple/array of domain, user, session id.

$logged_on_users = Get-WmiObject Win32_LoggedOnUser -ComputerName $system

I'm then calling the type's function to pull out two variables (Antecedent and Depedent) within each value:

$all_user_sessions = @()
foreach ($x in $logged_on_users){
  $t = @()
  $t += $x.GetPropertyValue("Antecedent").tostring()
  $t += $x.GetPropertyValue("Dependent").tostring()
  $all_user_sessions += $t
}

What is the syntax for a one-line Array Comprehension (if it's even possible)? I have:

$all_user_sessions = ($logged_on_users | % { @($_.GetPropertyValue("Antecedent").tostring(), $_.GetPropertyValue("Dependent").tostring() ) })

When I add

foreach ($s in $all_user_sessions){
  echo $s.GetType().FullName
}

The type is a String, not an Array.

1 Answer 1

8

This is going to sound a little weird, but you need to add a comma before the array in your ForEach-Object loop:

$all_user_sessions = ($logged_on_users | % { ,@($_.GetPropertyValue("Antecedent").tostring(), $_.GetPropertyValue("Dependent").tostring() ) })

As your code is written you're adding everything to a single array. Placing the comma like that will create an array (actually an object) of each index.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, it worked perfectly. I'm just used to Python.

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.