![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| 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 Specs![]() |
| | #2 (permalink) |
| 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 Specs![]() |
| | #3 (permalink) |
| 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 Specs![]() |
| | #4 (permalink) |
| 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 Specs![]() |
![]() |
| 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 | |||