![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
|
Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
br> br> |
| |||||||
![]() |
| | Thread Tools | Display Modes |
| | #1 (permalink) |
| Guest | Why is the precedence for the property operator not being honored In the below statement Powershell I think should be binding the $_ to DeviceID and then evaluating the variable. What its doing is expanding the $_ and concatenating the .DeviceID. Is the current behavior what is expected? Get-WmiObject Win32_logicaldisk | % {if ($_.DriveType -eq 3) {"The amount of free space on drive $_.DeviceID is $_.FreeSpace"}} thx bob |
My System Specs![]() |
| | #2 (permalink) | ||||||||||||
| Guest | Re: Why is the precedence for the property operator not being honored Try this... you have to put $( ) around Variables to let them expand. Get-WmiObject Win32_logicaldisk | % {if ($_.DriveType -eq 3) {"The amount of free space on drive $($_.DeviceID) is $($_.FreeSpace)"}} "Bob Landau" <BobLandau@xxxxxx> wrote in message news:8B55372C-692B-4C35-BA1D-664BE3BD3B06@xxxxxx
| ||||||||||||
My System Specs![]() | |||||||||||||
| | #3 (permalink) | ||||||||||||
| Guest | Re: Why is the precedence for the property operator not being honored Oh... another option Get-WmiObject Win32_logicaldisk | % {if ($_.DriveType -eq 3){"The amount of free space on drive {0} is {1}" -f ($_.DeviceID),($_.FreeSpace)}} More Info about the -f operator here http://bsonposh.com/modules/wordpress/?p=35 "Bob Landau" <BobLandau@xxxxxx> wrote in message news:8B55372C-692B-4C35-BA1D-664BE3BD3B06@xxxxxx
| ||||||||||||
My System Specs![]() | |||||||||||||
| | #4 (permalink) | ||||||||||||
| Guest | Re: Why is the precedence for the property operator not being honored "Brandon Shell" <tshell.mask@xxxxxx> wrote in message news:eoDTQkj8HHA.4880@xxxxxx
do variable expansion i.e. $_ is expanded to the string representation of a Win32_LogicalDisk instance: \\KEITH1\root\cimv2:Win32_LogicalDisk.DeviceID="C:" However double-quoted string expansion doesn't support evaluating an expression by default, which is what accessing properties of a variable would require. You probably wouldn't want this anyway otherwise something like this: "The date is $date." would error because there is a property operator (.) after the $date object but you specified no property. So as Brandon points out, the $() in a string allows you evaluate one or more expressions within the string and the results are converted to their string representations and placed into the string. BTW it is pretty easy to get tripped up on this. I used to do it a lot in my scripts. I think I'm finally getting to the point where I don't make this mistake very often. -- Keith | ||||||||||||
My System Specs![]() | |||||||||||||
| | #5 (permalink) | ||||||||||||||||||||||||
| Guest | Re: Why is the precedence for the property operator not being hono Thank you Brandon, I like both of your ideas. But I still don't understand whether the current behavior is expected or not. It seems a shame to need to require anything special simply to get the value of a property. Both the QuickStart wiki and "Windows Powershell in Action" both state the member operator is high up on the order of precedence. "Brandon Shell" wrote:
| ||||||||||||||||||||||||
My System Specs![]() | |||||||||||||||||||||||||
| | #6 (permalink) | ||||||||||||||||||||||||||||||||||||
| Guest | Re: Why is the precedence for the property operator not being hono Keith just explained it well... it is expected. "Bob Landau" <BobLandau@xxxxxx> wrote in message news:E5AE901A-EADF-4EF6-B815-21992E492DFA@xxxxxx
| ||||||||||||||||||||||||||||||||||||
My System Specs![]() | |||||||||||||||||||||||||||||||||||||
| | #7 (permalink) | ||||||||||||||||||||||||
| Guest | Re: Why is the precedence for the property operator not being hono Thanks, I of course hadn't thought about the even more common usage you described below. bob "Keith Hill" wrote:
| ||||||||||||||||||||||||
My System Specs![]() | |||||||||||||||||||||||||
| | #8 (permalink) |
| Guest | Re: Why is the precedence for the property operator not being honored Alternatively you could concatenate the string: Get-WmiObject Win32_logicaldisk | % {if ($_.DriveType -eq 3) {"The amount of free space on drive " + $_.DeviceID + " is " + $_.FreeSpace}} -- Kiron |
My System Specs![]() |
| | #9 (permalink) | ||||||||||||
| Guest | Re: Why is the precedence for the property operator not being honored Some more examples... # When -format operator is used you can remove the braces on ($_.property) Get-WmiObject Win32_logicaldisk | foreach {if ($_.DriveType -eq 3){"The amount of free space on drive {0} is {1}" -f $_.DeviceID,$_.FreeSpace}} #narrow down returned properties using the where clause (aka server side processing) Get-WmiObject -query "select DeviceID,FreeSpace from Win32_logicaldisk where DriveType = 3" | foreach {"Free space on drive {0} is {1}" -f $_.DeviceID,$_.FreeSpace} #narrow down returned properties using the -filter parameter (server side processing) Get-WmiObject -class Win32_logicaldisk -filter "DriveType = 3" | foreach {"Free space on drive {0} is {1}" -f $_.DeviceID,$_.FreeSpace} Shay http://scriptolog.blogspot.com
| ||||||||||||
My System Specs![]() | |||||||||||||
![]() |
| Thread Tools | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| -f operator | Tibor Soos | PowerShell | 3 | 05-28-2008 07:36 AM |
| Use my custom TypeDescriptor to obtains default Value on property inXAML Property Editor of Visual Studio 2008 | azerty | Avalon | 0 | 04-14-2008 06:14 AM |
| What does the $() operator do? | Kevin Buchan | PowerShell | 3 | 02-08-2008 02:05 PM |
| Gotcha: unexpected -or and -and precedence | Roman Kuzmin | PowerShell | 9 | 06-29-2007 11:04 AM |
| Windows Media Player - DRM for downloaded content not honored ? | musctpmd9 | Vista music pictures video | 11 | 01-08-2007 08:50 PM |