evetsleep

Convert-StringToInt

Aug 20th, 2013
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Function Convert-StringToInt
  2. {
  3.     process
  4.     {
  5.         #Store for easier use.
  6.         $object = $_
  7.  
  8.         #The properties that we'll support converting.
  9.         $convertables = 'TotalDeletedItemSize','TotalItemSize','DatabaseSize','FolderSize','FolderAndSubfolderSize','TopSubjectSize'
  10.  
  11.         #Collect the property list.
  12.         $properties = $object | Get-Member -MemberType Property | select -ExpandProperty name
  13.        
  14.         #Create a new object.
  15.         $newObject = New-Object -TypeName PSObject
  16.        
  17.         #Go through each property and convert if necessary.
  18.         foreach ($property in $properties)
  19.         {
  20.             if($convertables -contains $property)
  21.             {
  22.                
  23.                 $oldValue = $object.$property
  24.                
  25.                 #Pull out the stuff we're intersted in and remove commas and the like.
  26.                 $oldValue -match '\(\w.*' | out-null
  27.                 $m = $($matches[0] -replace '^\(|\s.*|,')
  28.                
  29.                 #If our result is null we store a 0
  30.                 if($m -match 'temp' -or $m -like $null)
  31.                 {
  32.                     $newObject | Add-Member -Name $property -Value 0 -MemberType 'NoteProperty'
  33.                     continue
  34.                 }
  35.                
  36.                 #Convet the string to bytes
  37.                 [int64]$bytes = $m
  38.                 $newObject | Add-Member -Name $property -Value $bytes -MemberType 'NoteProperty'
  39.             }
  40.             else
  41.             {
  42.                 $newObject | Add-Member -Name $property -Value $object.$property -MemberType 'NoteProperty'        
  43.             }
  44.         }
  45.  
  46.         #Return our new object.
  47.         Write-Output $newObject
  48.     }
  49. } #END Convert-StringToInt
Advertisement
Add Comment
Please, Sign In to add comment