{"id":91,"date":"2013-10-07T20:27:25","date_gmt":"2013-10-07T20:27:25","guid":{"rendered":"http:\/\/dollarunderscore.azurewebsites.net\/?p=91"},"modified":"2013-10-09T12:10:14","modified_gmt":"2013-10-09T12:10:14","slug":"a-powershell-gotcha-numbers-becomes-strings","status":"publish","type":"post","link":"https:\/\/p0wershell.com\/?p=91","title":{"rendered":"A PowerShell &#8216;Gotcha!&#8217;, numbers becomes strings&#8230;"},"content":{"rendered":"<p>During our migration project from Lotus Notes to Office 365, we were doing an inventory of mailbox sizes to determine which ones to migrate first.<\/p>\n<p>So our Lotus Notes admins gave us a CSV file with the users and their mailbox size in bytes. The goal was to fetch the mailboxes under 1 GB of size that would be a part of a Pre-Pilot. Could&#8217;nt be easier, right?<\/p>\n<p>I imported the file, verified it got parsed correctly and went ahead and looked for the &lt; 1 gb mailboxes. The results I expected were that most of the ~100 mailboxes would be under this limit.<\/p>\n<p>But I got four&#8230;<\/p>\n<p><a href=\"https:\/\/p0wershell.com\/wp-content\/uploads\/2013\/10\/MailboxContents.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-101\" alt=\"MailboxContents\" src=\"https:\/\/p0wershell.com\/wp-content\/uploads\/2013\/10\/MailboxContents-300x131.png\" width=\"300\" height=\"131\" srcset=\"https:\/\/p0wershell.com\/wp-content\/uploads\/2013\/10\/MailboxContents-300x131.png 300w, https:\/\/p0wershell.com\/wp-content\/uploads\/2013\/10\/MailboxContents-624x273.png 624w, https:\/\/p0wershell.com\/wp-content\/uploads\/2013\/10\/MailboxContents.png 668w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>What has happened? I tried to find out how big the total was by adding them all togheter:<\/p>\n<p><a href=\"https:\/\/p0wershell.com\/wp-content\/uploads\/2013\/10\/TotalSize.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-111\" alt=\"TotalSize\" src=\"https:\/\/p0wershell.com\/wp-content\/uploads\/2013\/10\/TotalSize-300x64.png\" width=\"300\" height=\"64\" srcset=\"https:\/\/p0wershell.com\/wp-content\/uploads\/2013\/10\/TotalSize-300x64.png 300w, https:\/\/p0wershell.com\/wp-content\/uploads\/2013\/10\/TotalSize.png 446w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>Ok&#8230; So things are becoming really weird, I have 99 mailboxes in this file, with 4 of them being under 1 gb in size, but the total is 83,6? What has happened?<\/p>\n<p>The answer comes now:<br \/>\n<a href=\"https:\/\/p0wershell.com\/wp-content\/uploads\/2013\/10\/SystemString.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-121\" alt=\"SystemString\" src=\"https:\/\/p0wershell.com\/wp-content\/uploads\/2013\/10\/SystemString-300x94.png\" width=\"300\" height=\"94\" srcset=\"https:\/\/p0wershell.com\/wp-content\/uploads\/2013\/10\/SystemString-300x94.png 300w, https:\/\/p0wershell.com\/wp-content\/uploads\/2013\/10\/SystemString.png 530w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>Look at the definition of the Size-property&#8230; System.String!!!<\/p>\n<p>Why would PowerShell do that with something that clearly only contains numbers?<\/p>\n<p>Let&#8217;s force it to handle them correctly&#8230;<\/p>\n<p><a href=\"https:\/\/p0wershell.com\/wp-content\/uploads\/2013\/10\/ToBigForInt.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-131\" alt=\"ToBigForInt\" src=\"https:\/\/p0wershell.com\/wp-content\/uploads\/2013\/10\/ToBigForInt-300x79.png\" width=\"300\" height=\"79\" srcset=\"https:\/\/p0wershell.com\/wp-content\/uploads\/2013\/10\/ToBigForInt-300x79.png 300w, https:\/\/p0wershell.com\/wp-content\/uploads\/2013\/10\/ToBigForInt-624x164.png 624w, https:\/\/p0wershell.com\/wp-content\/uploads\/2013\/10\/ToBigForInt.png 977w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>That did not work at all&#8230; But now we have the explanation! Some of the mailbox sizes in this file are simply to big to be stored as an [int] since it&#8217;s 32-bit!<\/p>\n<p>We need to convert this to a 64-bit value, pretty obvious when you think about it, but since PowerShell usually takes care of these things for us, it&#8217;s easy to forget&#8230;<\/p>\n<p>&nbsp;<\/p>\n<p>So how to fix it?<\/p>\n<p>We simply tell PowerShell that Size is of the type [long]. Let&#8217;s go back the Where-clause we had in the beginning:<\/p>\n<p><a href=\"https:\/\/p0wershell.com\/wp-content\/uploads\/2013\/10\/ConvertToLong.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-141\" alt=\"ConvertToLong\" src=\"https:\/\/p0wershell.com\/wp-content\/uploads\/2013\/10\/ConvertToLong-300x293.png\" width=\"300\" height=\"293\" srcset=\"https:\/\/p0wershell.com\/wp-content\/uploads\/2013\/10\/ConvertToLong-300x293.png 300w, https:\/\/p0wershell.com\/wp-content\/uploads\/2013\/10\/ConvertToLong-624x610.png 624w, https:\/\/p0wershell.com\/wp-content\/uploads\/2013\/10\/ConvertToLong.png 726w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>Works perfectly&#8230; Let&#8217;s do a count:<br \/>\n<a style=\"line-height: 1.714285714; font-size: 1rem;\" href=\"https:\/\/p0wershell.com\/wp-content\/uploads\/2013\/10\/NumberOfSmallOnes.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-151\" alt=\"NumberOfSmallOnes\" src=\"https:\/\/p0wershell.com\/wp-content\/uploads\/2013\/10\/NumberOfSmallOnes-300x23.png\" width=\"300\" height=\"23\" srcset=\"https:\/\/p0wershell.com\/wp-content\/uploads\/2013\/10\/NumberOfSmallOnes-300x23.png 300w, https:\/\/p0wershell.com\/wp-content\/uploads\/2013\/10\/NumberOfSmallOnes.png 616w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>Now it makes sense!<\/p>\n<p>Conclusion:<\/p>\n<p>PowerShell does an awesome job on creating objects for us, guessing the property types and so on, &#8220;it just works&#8221; most of the time.<\/p>\n<p>But when working with larger numbers, make sure you aren&#8217;t working with strings!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>During our migration project from Lotus Notes to Office 365, we were doing an inventory of mailbox sizes to determine which ones to migrate first. So our Lotus Notes admins gave us a CSV file with the users and their mailbox size in bytes. The goal was to fetch the mailboxes under 1 GB of [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false}}},"categories":[31,21],"tags":[71,81,91,1061,1071],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p3Zj0A-1t","_links":{"self":[{"href":"https:\/\/p0wershell.com\/index.php?rest_route=\/wp\/v2\/posts\/91"}],"collection":[{"href":"https:\/\/p0wershell.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/p0wershell.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/p0wershell.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/p0wershell.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=91"}],"version-history":[{"count":0,"href":"https:\/\/p0wershell.com\/index.php?rest_route=\/wp\/v2\/posts\/91\/revisions"}],"wp:attachment":[{"href":"https:\/\/p0wershell.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=91"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/p0wershell.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=91"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/p0wershell.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=91"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}