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

7

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.

1
  • 1
    Thanks, it worked perfectly. I'm just used to Python.
    – malogos
    Commented Jun 9, 2014 at 17:21

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.