Skip to main content

Keeping a Numeric Data Type a Number after Formatting

I’m up in Toronto, Canada tonight.  I’m delivering a custom 3 day PowerShell class.  Yes, 3 days.  Not a lot of time.  For that reason, no question is off the table.  Normally when a question is asked that is covered later in the course, I ask for them to hold off so we can build our knowledge base up a bit more so we can answer the question in more depth.  Did I mention that we only had 3 days!!!!

So we had a question after I used Get-Volume.  It turns out that we have some PowerShell V4 users on Windows 7 in the audience.  Get-Volume is from the Storage module that comes from the Windows 8 operating system, they could not use it.  Since we were about to cover calculated properties, I went ahead and used that as an opportunity to take a question and make a lesson out of it.  Here is our code.

1

2

3

4

GWMI Win32_Volume |

Select -Property DriveLetter,

    @{N = "SizeGB";E = {$_.Capacity/1gb}},

    @{N = "FreeSpaceSizeGB"; E = {$_.FreeSpace/1gb}} 

Yes I know, I used short hand.  A rarity but they wanted to see it as compressed as possible.  Nothing note worthy here.  The out bothered them a bit.

DriveLetter                                                SizeGB                 FreeSpaceSizeGB

-----------                                                ------                 ---------------

                                                0.341793060302734              0.0870437622070313

E:                                               19.9970664978027                12.6184005737305

C:                                               9.65624618530273                1.36867141723633

D:                                               3.98966598510742                               0

 

 

They asked for 2 decimal places on the custom properties.  So, I showed them a few ways to do this.

1

2

3

4

GWMI Win32_Volume |

Select -Property DriveLetter,

    @{N = "SizeGB";E = {($_.Capacity/1gb).ToString("#.##")}},

    @{N = "FreeSpaceSizeGB"; E = {"{0:N2}" -f ($_.FreeSpace/1gb)}}

And here is the output:

DriveLetter                      SizeGB                           FreeSpaceSizeGB               

-----------                      ------                           ---------------               

                                 .34                              0.09                          

E:                               20                               12.62                         

C:                               9.66                             1.37                          

D:                               3.99                             0.00                          

 

 

Looks good.  The problem is that both calculated properties are now System.String objects, not System.Double which is what they originally were.  OK, problem.  Fortunately we have the –as operator.  The –as operator will convert from one data type to another if it is possible.  Since we had a string that was nothing but numbers, it was possible.  Here is the original Get-Member information without the use of the –as operator.

TypeName: Selected.System.Management.ManagementObject

 

Name            MemberType   Definition                        

----            ----------   ----------                        

Equals          Method       bool Equals(System.Object obj)    

GetHashCode     Method       int GetHashCode()                 

GetType         Method       type GetType()                    

ToString        Method       string ToString()                 

DriveLetter     NoteProperty string DriveLetter=D:             

FreeSpaceSizeGB NoteProperty System.String FreeSpaceSizeGB=68.88

SizeGB          NoteProperty System.String SizeGB=238.34       

 

 

I highlighted in yellow the problem. Here is the code using the –as- operator.

 

1

2

3

4

GWMI Win32_Volume |

Select -Property DriveLetter,

    @{N = "SizeGB";E = {($_.Capacity/1gb).ToString("#.##") -as [Double]}},

    @{N = "FreeSpaceSizeGB"; E = {"{0:N2}" -f ($_.FreeSpace/1gb) -as [Double]}} 

 

Finally, here is our object after being piped to Get-Member.  Again, I highlighted in yellow what you should be interested in,

    TypeName: Selected.System.Management.ManagementObject

 

Name            MemberType   Definition                        

----            ----------   ----------                        

Equals          Method       bool Equals(System.Object obj)    

GetHashCode     Method       int GetHashCode()                 

GetType         Method       type GetType()                    

ToString        Method       string ToString()                 

DriveLetter     NoteProperty string DriveLetter=D:             

FreeSpaceSizeGB NoteProperty System.Double FreeSpaceSizeGB=68.88

SizeGB          NoteProperty System.Double SizeGB=238.34 

 

I have always stressed in my PowerShell classes the importance of knowing what your object is doing in the pipeline.  Get-Member is a great way to not only know what properties and methods you have at your disposal, but also if any of them changed in a way that you do not want them to.

 

 

Comments

Popular posts from this blog

Sticky Key problem between Windows Server 2012 and LogMeIn

This week I instructed my first class using Windows Server 2012 accessed via LogMeIn and discovered a Sticky Key problem every time you press the Shift key. Here is my solution to resolve this.  First off, in the Preferences of LogMeIn for the connection to the Windows Server, click General . Change the Keyboard and mouse priority to Host side user and click Apply at the bottom. On the Windows 2012 server, open the Control Panel – Ease of Access – Change how your keyboard works . Uncheck Turn on Sticky Keys . Click Set up Sticky Keys . Uncheck Turn on Sticky Keys when SHIFT is pressed five times . Click OK twice. If you are using Windows Server 2012 as a Hyper-V host, you will need to redo the Easy of Use settings on each guest operating system in order to avoid the Sticky Key Problem. Updated Information: March 20, 2013 If you continue to have problems, Uncheck Turn on Filter Keys .

Where did a User’s Account Get Locked Out?

Updated: May 15, 2015 When this article was originally published, two extra carriage returns were add causing the code to malfunction.  The code below is correct.   My client for this week’s PowerShell class had a really interesting question. They needed to know where an account is being locked out at. OK, interesting. Apparently users hop around clients and forget to log off, leading to eventual lock out of their accounts. The accounts can be unlocked, but are then relocked after Active Directory replication. This problem is solved in two parts. The first one is to modify the event auditing on the network. The second part is resolved with PowerShell. The first part involves creating a group policy that will encompass your Domain Controllers. In this GPO, make these changes. Expand Computer Configuration \ Policies \ Windows Settings \ Security Settings \ Advanced Audit Policy Configuration \ Audit Policies \ Account Management Double click User Account Management C...

Backup and Restore AD LDS with DSDBUTIL.exe

Active Directory Lightweight Directory Services allow you to create a directory service that allows applications to have access to user accounts, groups, and authentication similar to Active Directory Domain Services.  The big advantage here is that the schema of the directory service will not be bound by the rules of an Active Directory database.  Exchange 2007/2010, for example, use an instance of AD LDS on the Edge Transport Server to provide for user authentication from the internet.  Because your Active Directory database is not exposed to the internet, this is more secure. Applications will handle most of the dirty work should they require AD LDS.  You may want to make sure the database is being backed up and also have a restore plan in place.  Should the database become corrupt, the application that uses that database will fail.  This document will walk you through backing up and restoring an instance of AD LDS using the dsdbutil.exe command. Fi...