Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista Tutorial - Variable issues within pipeline using wmi

Reply
 
Old 11-14-2006   #1 (permalink)
Adam Murray
Guest


 
 

Variable issues within pipeline using wmi

Hi,

I have been messing around with trying to create a single line command to
list logged on users to a machine. I think this should be quiet easy but I
am struggling with the way powershell is handling my variable expansion
within the pipeline.

I expect this command to work, however it doesn't seem to expand the
variable correctly.

gwmi -query "Select logonid from Win32_LogonSession where logontype = 2 or
logontype = 10" -computername aupoza510 | foreach {gwmi -computername
aupoza510 -query 'Associators of {Win32_LogonSession.LogonId=$_.logonid}
Where AssocClass=Win32_LoggedOnUser Role=Dependent'}


The following command does work ... seems very strange to me.

gwmi -query "Select logonid from Win32_LogonSession where logontype = 2 or
logontype = 10" -computername aupoza510 | foreach {gwmi -computername
aupoza510 -query "Associators of{$_} Where AssocClass=Win32_LoggedOnUser
Role=Dependent"}

The other thing I have found strange which is not related to powershell is
that the logontype does not seem correct all the time. I ran the command
against the server where I was terminal serviced in (logontype 10) and it
didn't return my logon. I then checked on the server and my logontype was 0
which is meant to be reserved for the system. Any ideas?

Cheers,

Adam.



My System SpecsSystem Spec
Old 11-15-2006   #2 (permalink)
Keith Hill [MVP]
Guest


 
 

Re: Variable issues within pipeline using wmi

"Adam Murray" <muzzar78@msn.com> wrote in message
news:Ot4O9CGCHHA.204@TK2MSFTNGP04.phx.gbl...
> I have been messing around with trying to create a single line command to
> list logged on users to a machine. I think this should be quiet easy but I
> am struggling with the way powershell is handling my variable expansion
> within the pipeline.
>
> I expect this command to work, however it doesn't seem to expand the
> variable correctly.
>
> gwmi -query "Select logonid from Win32_LogonSession where logontype = 2 or
> logontype = 10" -computername aupoza510 | foreach {gwmi -computername
> aupoza510 -query 'Associators of {Win32_LogonSession.LogonId=$_.logonid}
> Where AssocClass=Win32_LoggedOnUser Role=Dependent'}


Using single quotes tells PowerShell to not perform variable substituion.
So on that last query (where you use $_.logonid) you probably want to use
double qoutes.

--
Keith


My System SpecsSystem Spec
Old 11-15-2006   #3 (permalink)
Keith Hill [MVP]
Guest


 
 

Re: Variable issues within pipeline using wmi

"Keith Hill [MVP]" <r_keith_hill@mailhot.moc.nospam> wrote in message
news:%23517AtHCHHA.4464@TK2MSFTNGP06.phx.gbl...
> "Adam Murray" <muzzar78@msn.com> wrote in message
> news:Ot4O9CGCHHA.204@TK2MSFTNGP04.phx.gbl...
>> gwmi -query "Select logonid from Win32_LogonSession where logontype = 2
>> or logontype = 10" -computername aupoza510 | foreach {gwmi -computername
>> aupoza510 -query 'Associators of {Win32_LogonSession.LogonId=$_.logonid}
>> Where AssocClass=Win32_LoggedOnUser Role=Dependent'}

>
> Using single quotes tells PowerShell to not perform variable substituion.
> So on that last query (where you use $_.logonid) you probably want to use
> double qoutes.


Oh yeah, to not only expand variables but then to reference a property on
that variable you need to do this:

$($_.logonid)

That tells PowerShell to evaluate the entire expression in $(...) instead of
evaluating $_ and then appending the string ".logonid" to it.

--
Keith


My System SpecsSystem Spec
Old 11-16-2006   #4 (permalink)
Adam Murray
Guest


 
 

Re: Variable issues within pipeline using wmi

Thanks Keith.


"Keith Hill [MVP]" <r_keith_hill@mailhot.moc.nospam> wrote in message
news:%23kcoaDJCHHA.4060@TK2MSFTNGP03.phx.gbl...
> "Keith Hill [MVP]" <r_keith_hill@mailhot.moc.nospam> wrote in message
> news:%23517AtHCHHA.4464@TK2MSFTNGP06.phx.gbl...
>> "Adam Murray" <muzzar78@msn.com> wrote in message
>> news:Ot4O9CGCHHA.204@TK2MSFTNGP04.phx.gbl...
>>> gwmi -query "Select logonid from Win32_LogonSession where logontype = 2
>>> or logontype = 10" -computername aupoza510 | foreach
>>> {gwmi -computername aupoza510 -query 'Associators of
>>> {Win32_LogonSession.LogonId=$_.logonid} Where
>>> AssocClass=Win32_LoggedOnUser Role=Dependent'}

>>
>> Using single quotes tells PowerShell to not perform variable substituion.
>> So on that last query (where you use $_.logonid) you probably want to use
>> double qoutes.

>
> Oh yeah, to not only expand variables but then to reference a property on
> that variable you need to do this:
>
> $($_.logonid)
>
> That tells PowerShell to evaluate the entire expression in $(...) instead
> of evaluating $_ and then appending the string ".logonid" to it.
>
> --
> Keith
>



My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Suggestion: New special variable for pipeline enumeration index PowerShell
Using the $_ pipeline with WMI PowerShell
A pipeline exercise PowerShell
Multiple trap handlers in a function: variable scoping issues PowerShell
How can I ensure that a variable is a built-in powershell variable? PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46