header
header Register : : Login header
header
divider
menuleft
menuright
submenu
left

[August 25th, 2008] Check the home page regarding PowerShell related news from a brand new sponsor: Idera

Formatting Question
Last Post 20 Sep 2008 04:00 AM by Elan. 2 Replies.
Printer Friendly
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
ElanUser is Offline
New Member
New Member
Posts:28
Avatar

--
20 Sep 2008 02:46 AM  

In the following code, why does it return the results as an integer.  Shouldn't it be showing me the full number such as 323.23456324324 which I should then be able to [math]::round(stuffhere,2)?

Get-WmiObject -class Win32_LogicalDisk | ForEach-Object {$_.Size = $_.Size/1GB; $_} | ForEach-Object {$_.FreeSpace = $_.FreeSpace/1GB; $_} | FT DeviceID,Size

If I do it into a regular foreach ($x in $variable {, it ends up working as I want so I can round it to the hundreth.

I guess the best way to do it anyways is:

Get-WmiObject -class Win32_LogicalDisk | Format-Table DeviceID,@{Label="Size";Expression={[math]::round($_.FreeSpace/1GB,2)}}

But still, I am curious as to why the first line doesn't work.

Elan Shudnow
http://www.shudnow.net
halr9000User is Offline
PowerShell MVP, Site Admin
Basic Member
Basic Member
Posts:335
Avatar

--
20 Sep 2008 03:36 AM  
The reason is because you are trying to set values in the WMI object which has got its own ideas of what you can and can't do with it. :) Use Get-Member and its pretty obvious (which is why I use it so much):

PS > gwmi win32_logicaldisk | gm size, freespace


   TypeName: System.Management.ManagementObject#root\cimv2\Win32_LogicalDisk

Name      MemberType Definition
----      ---------- ----------
Size      Property   System.UInt64 Size {get;set;}
FreeSpace Property   System.UInt64 FreeSpace {get;set;}


So anyway, don't do that, it's bad practice and will get you in trouble. I would use calculated properties as you did with your second example, or use intermediate statements where you create new variables to store the values you need.
Community Director, PowerShellCommunity.org
Co-host, PowerScripting Podcast
Author, TechProsaic
ElanUser is Offline
New Member
New Member
Posts:28
Avatar

--
20 Sep 2008 04:00 AM  

So taking a look at the following code:

$colItems = Get-WmiObject -class Win32_LogicalDisk

    foreach ($objItem in $colItems) {

            $objFreeSpace = $objItem.FreeSpace/1GB

            $objFreeSpace = [math]::round($objFreeSpace,2)
   }

How does $objFreeSpace | gm end up showing as System.Double and not as System.Management.ManagementObject#root\cimv2\Win32_LogicalDisk. 

isn't doing this the same as doing the foreach-object I initially did?

I am just not getting how foreach-object still tried to modify the object when it was in a state of System.Management.ManagementObject#root\cimv2\Win32_LogicalDisk while doing the foreach turned it into a System.Double which I could then round the way I wanted it to.

Elan Shudnow
http://www.shudnow.net
You are not authorized to post a reply.

Active Forums 4.1
right
   
footer Sponsored by Quest Software • SAPIEN Technologies • ShellTools, LLC • Microsoft Windows Server 2008 footer
footer