Windows Vista Forums

Rounding off numbers to 2 decimal places.
  1. #1


    Don Pedro Guest

    Rounding off numbers to 2 decimal places.

    I have the following script to retrieve mailbox database sizes and number of
    users.

    However the numbers returned on the mailbox database size is to around 12
    decimal places.



    I would like

    1. $DBSize be displayed to 2 decimal places.
    2. $TotalDBSize to be displayed to 2 decimal places.

    Any ideas how to modify below script?


    FUNCTION RetrieveData
    {
    param ($X, $Y)

    $db = Get-MailboxDatabase -server $X | sort-object identity
    foreach ($objItem in $db)
    {
    $userCount = 0
    $userDatabase = $objitem | Get-Mailbox
    Foreach ($user in $userDatabase) { $userCount++ }
    $TotalUsers = $TotalUsers + $userCount
    $edbfilepath = $objItem.edbfilepath
    $path = "`\`\" + $Y + "`\" +
    $objItem.EdbFilePath.DriveName.Remove(1).ToString() + "$"+
    $objItem.EdbFilePath.PathName.Remove(0,2)
    $dbpath = Get-ChildItem $path
    $DBSize = ($dbpath.Length/1048576KB)
    $TotalDBSize = $TotalDBSize+$DBSize;

    Write-Host $X, $objitem.name, $DBSize, $usercount
    }
    Write-Host "Total DB Size is $TotalDBSize"
    Write-Host "Total users is $TotalUsers"
    }
    # Call Function
    RetrieveData "SERVER01" "SERVER01.CONTOSO.ORG"

      My System SpecsSystem Spec

  2. #2


    Chris Dent Guest

    Re: Rounding off numbers to 2 decimal places.


    A couple of ways for you here. If you plan to sort on field at all you
    should ensure it remains numeric, and that would make the Round method
    more appropriate.


    $Value = 2.3456
    # Numeric output
    $NewValue = [Math]::Round($Value, 2)
    $NewValue
    $NewValue.GetType()
    # String output
    $NewValue = '{0:N2}' -f $Value
    $NewValue
    $NewValue.GetType()


    HTH

    Chris

    Don Pedro wrote:

    > I have the following script to retrieve mailbox database sizes and number of
    > users.
    >
    > However the numbers returned on the mailbox database size is to around 12
    > decimal places.
    >
    > I would like
    >
    > 1. $DBSize be displayed to 2 decimal places.
    > 2. $TotalDBSize to be displayed to 2 decimal places.
    >
    > Any ideas how to modify below script?
    >
    >
    > FUNCTION RetrieveData
    > {
    > param ($X, $Y)
    >
    > $db = Get-MailboxDatabase -server $X | sort-object identity
    > foreach ($objItem in $db)
    > {
    > $userCount = 0
    > $userDatabase = $objitem | Get-Mailbox
    > Foreach ($user in $userDatabase) { $userCount++ }
    > $TotalUsers = $TotalUsers + $userCount
    > $edbfilepath = $objItem.edbfilepath
    > $path = "`\`\" + $Y + "`\" +
    > $objItem.EdbFilePath.DriveName.Remove(1).ToString() + "$"+
    > $objItem.EdbFilePath.PathName.Remove(0,2)
    > $dbpath = Get-ChildItem $path
    > $DBSize = ($dbpath.Length/1048576KB)
    > $TotalDBSize = $TotalDBSize+$DBSize;
    >
    > Write-Host $X, $objitem.name, $DBSize, $usercount
    > }
    > Write-Host "Total DB Size is $TotalDBSize"
    > Write-Host "Total users is $TotalUsers"
    > }
    > # Call Function
    > RetrieveData "SERVER01" "SERVER01.CONTOSO.ORG"
    >
    --
    Blog: http://www.indented.co.uk
    DnsShell: http://code.msdn.microsoft.com/dnsshell

      My System SpecsSystem Spec

  3. #3


    Larry__Weiss Guest

    Re: Rounding off numbers to 2 decimal places.

    Try

    Write-Host "Total DB Size is $('{0:N2}' -f $TotalDBSize)"
    Write-Host "Total users is $('{0:N2}' -f $TotalUsers)"

    - Larry


    On 5/13/2010 8:05 AM, Don Pedro wrote:

    > I have the following script to retrieve mailbox database sizes and number of
    > users.
    >
    > However the numbers returned on the mailbox database size is to around 12
    > decimal places.
    >
    > I would like
    >
    > 1. $DBSize be displayed to 2 decimal places.
    > 2. $TotalDBSize to be displayed to 2 decimal places.
    >
    > Any ideas how to modify below script?
    >
    >
    > FUNCTION RetrieveData
    > {
    > param ($X, $Y)
    >
    > $db = Get-MailboxDatabase -server $X | sort-object identity
    > foreach ($objItem in $db)
    > {
    > $userCount = 0
    > $userDatabase = $objitem | Get-Mailbox
    > Foreach ($user in $userDatabase) { $userCount++ }
    > $TotalUsers = $TotalUsers + $userCount
    > $edbfilepath = $objItem.edbfilepath
    > $path = "`\`\" + $Y + "`\" +
    > $objItem.EdbFilePath.DriveName.Remove(1).ToString() + "$"+
    > $objItem.EdbFilePath.PathName.Remove(0,2)
    > $dbpath = Get-ChildItem $path
    > $DBSize = ($dbpath.Length/1048576KB)
    > $TotalDBSize = $TotalDBSize+$DBSize;
    >
    > Write-Host $X, $objitem.name, $DBSize, $usercount
    > }
    > Write-Host "Total DB Size is $TotalDBSize"
    > Write-Host "Total users is $TotalUsers"
    > }
    > # Call Function
    > RetrieveData "SERVER01" "SERVER01.CONTOSO.ORG"

      My System SpecsSystem Spec

  4. #4


    Don Pedro Guest

    Re: Rounding off numbers to 2 decimal places.


    Larry, your solution worked first time so I went with that one.

    Now, if I could get the output to appear with headers per server that would
    be the icing on the cake...

    Desired output would be:

    SERVER01
    ------------
    Database Size (GB) Qty Mbxs
    MBX01 10.00 10
    MBX02 20.00 20
    MBX03 30.00 30

    -----------------------------------------
    Total Database Size: 60.00 GB
    Total number of users: 60

    SERVER02
    ------------
    Database Size (GB) Qty Mbxs
    MBX01 10.00 10
    MBX02 20.00 20
    MBX03 30.00 30

    -----------------------------------------
    Total Database Size: 60.00 GB
    Total number of users: 60

    If you could help that would be awesome.


    "Larry__Weiss" wrote:

    > Try
    >
    > Write-Host "Total DB Size is $('{0:N2}' -f $TotalDBSize)"
    > Write-Host "Total users is $('{0:N2}' -f $TotalUsers)"
    >
    > - Larry
    >
    >
    > On 5/13/2010 8:05 AM, Don Pedro wrote:

    > > I have the following script to retrieve mailbox database sizes and number of
    > > users.
    > >
    > > However the numbers returned on the mailbox database size is to around 12
    > > decimal places.
    > >
    > > I would like
    > >
    > > 1. $DBSize be displayed to 2 decimal places.
    > > 2. $TotalDBSize to be displayed to 2 decimal places.
    > >
    > > Any ideas how to modify below script?
    > >
    > >
    > > FUNCTION RetrieveData
    > > {
    > > param ($X, $Y)
    > >
    > > $db = Get-MailboxDatabase -server $X | sort-object identity
    > > foreach ($objItem in $db)
    > > {
    > > $userCount = 0
    > > $userDatabase = $objitem | Get-Mailbox
    > > Foreach ($user in $userDatabase) { $userCount++ }
    > > $TotalUsers = $TotalUsers + $userCount
    > > $edbfilepath = $objItem.edbfilepath
    > > $path = "`\`\" + $Y + "`\" +
    > > $objItem.EdbFilePath.DriveName.Remove(1).ToString() + "$"+
    > > $objItem.EdbFilePath.PathName.Remove(0,2)
    > > $dbpath = Get-ChildItem $path
    > > $DBSize = ($dbpath.Length/1048576KB)
    > > $TotalDBSize = $TotalDBSize+$DBSize;
    > >
    > > Write-Host $X, $objitem.name, $DBSize, $usercount
    > > }
    > > Write-Host "Total DB Size is $TotalDBSize"
    > > Write-Host "Total users is $TotalUsers"
    > > }
    > > # Call Function
    > > RetrieveData "SERVER01" "SERVER01.CONTOSO.ORG"
    > .
    >

      My System SpecsSystem Spec

  5. #5


    Chris Dent Guest

    Re: Rounding off numbers to 2 decimal places.


    I would change it a bit, and I would use [Math]::Round in this scenario.
    I hope you find this useful


    Function RetrieveData($Server)
    {
    $ShortName = ($Server -Replace '\..*').ToUpper()
    Write-Host $ShortName
    Write-Host $("-" * $ShortName.Length)

    Get-MailboxDatabase -Server $Server | Sort-Object Identity | %{
    $UserCount = ($_ | Get-Mailbox | Measure-Object).Count

    $Path = [String]::Format('\\{0}\{1}$\{2}',
    $Server,
    $_.EdbFilePath.DriveName.Remove(1).ToString(),
    $_.EdbFilePath.PathName.Remove(0,2))
    $DBSize = [Math]::Round($((Get-Item $Path).Length / 1Gb), 2)

    $TotalUsers += $UserCount
    $TotalDBSize += $DBSize

    $_ | Select-Object `
    @{n='Database';e={ $_.Name }}, `
    @{n='Size (GB)';e={ $DBSize }}, `
    @{n='Qty Mbxs';e={ ($_ | Get-Mailbox | Measure-Object).Count }}
    }
    Write-Host ""
    Write-Host "Total DB Size is $TotalDBSize GB"
    Write-Host "Total users is $TotalUsers"
    }
    # Call the function
    RetrieveData "SERVER01.CONTOSO.ORG"


    Don Pedro wrote:

    > Larry, your solution worked first time so I went with that one.
    >
    > Now, if I could get the output to appear with headers per server that would
    > be the icing on the cake...
    >
    > Desired output would be:
    >
    > SERVER01
    > ------------
    > Database Size (GB) Qty Mbxs
    > MBX01 10.00 10
    > MBX02 20.00 20
    > MBX03 30.00 30
    >
    > -----------------------------------------
    > Total Database Size: 60.00 GB
    > Total number of users: 60
    >
    > SERVER02
    > ------------
    > Database Size (GB) Qty Mbxs
    > MBX01 10.00 10
    > MBX02 20.00 20
    > MBX03 30.00 30
    >
    > -----------------------------------------
    > Total Database Size: 60.00 GB
    > Total number of users: 60
    >
    > If you could help that would be awesome.
    >
    >
    > "Larry__Weiss" wrote:
    >
    >

    >> Try
    >>
    >> Write-Host "Total DB Size is $('{0:N2}' -f $TotalDBSize)"
    >> Write-Host "Total users is $('{0:N2}' -f $TotalUsers)"
    >>
    >> - Larry
    >>
    >>
    >> On 5/13/2010 8:05 AM, Don Pedro wrote:
    >>

    >>> I have the following script to retrieve mailbox database sizes and number of
    >>> users.
    >>>
    >>> However the numbers returned on the mailbox database size is to around 12
    >>> decimal places.
    >>>
    >>> I would like
    >>>
    >>> 1. $DBSize be displayed to 2 decimal places.
    >>> 2. $TotalDBSize to be displayed to 2 decimal places.
    >>>
    >>> Any ideas how to modify below script?
    >>>
    >>>
    >>> FUNCTION RetrieveData
    >>> {
    >>> param ($X, $Y)
    >>>
    >>> $db = Get-MailboxDatabase -server $X | sort-object identity
    >>> foreach ($objItem in $db)
    >>> {
    >>> $userCount = 0
    >>> $userDatabase = $objitem | Get-Mailbox
    >>> Foreach ($user in $userDatabase) { $userCount++ }
    >>> $TotalUsers = $TotalUsers + $userCount
    >>> $edbfilepath = $objItem.edbfilepath
    >>> $path = "`\`\" + $Y + "`\" +
    >>> $objItem.EdbFilePath.DriveName.Remove(1).ToString() + "$"+
    >>> $objItem.EdbFilePath.PathName.Remove(0,2)
    >>> $dbpath = Get-ChildItem $path
    >>> $DBSize = ($dbpath.Length/1048576KB)
    >>> $TotalDBSize = $TotalDBSize+$DBSize;
    >>>
    >>> Write-Host $X, $objitem.name, $DBSize, $usercount
    >>> }
    >>> Write-Host "Total DB Size is $TotalDBSize"
    >>> Write-Host "Total users is $TotalUsers"
    >>> }
    >>> # Call Function
    >>> RetrieveData "SERVER01" "SERVER01.CONTOSO.ORG"
    >>>
    >> .
    >>
    >>
    --
    Blog: http://www.indented.co.uk
    DnsShell: http://code.msdn.microsoft.com/dnsshell

      My System SpecsSystem Spec

  6. #6


    Larry__Weiss Guest

    Re: Rounding off numbers to 2 decimal places.

    Is there a commonly used reference book that details these .NET
    methods?

    Even with a method as seemingly simple as [Math]::Round
    I get all these possibilities listed by using
    Steven Murawski's function get-staticmethoddefinition
    available at http://poshcode.org/968

    PS C:> [math] | get-staticmethoddefinition round
    static double Round(double a),
    static double Round(double value, int digits),
    static double Round(double value, System.MidpointRounding mode),
    static double Round(double value, int digits, System.MidpointRounding mode),
    static decimal Round(decimal d),
    static decimal Round(decimal d, int decimals),
    static decimal Round(decimal d, System.MidpointRounding mode),
    static decimal Round(decimal d, int decimals, System.MidpointRounding mode)
    PS C:>

    After using Google on "System.MidpointRounding" to find
    http://msdn.microsoft.com/en-us/libr...8VS.85%29.aspx
    and the especially useful "Community" comment there by Thomas Lee
    I now can code the variations in PowerShell like

    PS C:> [Math]::Round(3.45, 1, [System.MidPointRounding]::ToEven)
    3.4
    PS C:> [Math]::Round(3.45, 1, [System.MidPointRounding]::AwayFromZero)
    3.5
    PS C:>

    But I'd sure like to find a reference text that shows me all the
    possibilities. Or, does the complexity of these frameworks take
    them beyond the possibility of being documented with traditionally
    bound printed volumes?

    - Larry


    On 5/13/2010 11:13 AM, Chris Dent wrote:

    >
    > I would change it a bit, and I would use [Math]::Round in this scenario.
    > I hope you find this useful
    >
    >
    > Function RetrieveData($Server)
    > {
    > $ShortName = ($Server -Replace '\..*').ToUpper()
    > Write-Host $ShortName
    > Write-Host $("-" * $ShortName.Length)
    >
    > Get-MailboxDatabase -Server $Server | Sort-Object Identity | %{
    > $UserCount = ($_ | Get-Mailbox | Measure-Object).Count
    >
    > $Path = [String]::Format('\\{0}\{1}$\{2}',
    > $Server,
    > $_.EdbFilePath.DriveName.Remove(1).ToString(),
    > $_.EdbFilePath.PathName.Remove(0,2))
    > $DBSize = [Math]::Round($((Get-Item $Path).Length / 1Gb), 2)
    >
    > $TotalUsers += $UserCount
    > $TotalDBSize += $DBSize
    >
    > $_ | Select-Object `
    > @{n='Database';e={ $_.Name }}, `
    > @{n='Size (GB)';e={ $DBSize }}, `
    > @{n='Qty Mbxs';e={ ($_ | Get-Mailbox | Measure-Object).Count }}
    > }
    > Write-Host ""
    > Write-Host "Total DB Size is $TotalDBSize GB"
    > Write-Host "Total users is $TotalUsers"
    > }
    > # Call the function
    > RetrieveData "SERVER01.CONTOSO.ORG"
    >
    >
    > Don Pedro wrote:

    >> Larry, your solution worked first time so I went with that one.
    >>
    >> Now, if I could get the output to appear with headers per server that
    >> would be the icing on the cake...
    >>
    >> Desired output would be:
    >>
    >> SERVER01
    >> ------------
    >> Database Size (GB) Qty Mbxs
    >> MBX01 10.00 10
    >> MBX02 20.00 20
    >> MBX03 30.00 30
    >>
    >> -----------------------------------------
    >> Total Database Size: 60.00 GB
    >> Total number of users: 60
    >>
    >> SERVER02
    >> ------------
    >> Database Size (GB) Qty Mbxs
    >> MBX01 10.00 10
    >> MBX02 20.00 20
    >> MBX03 30.00 30
    >>
    >> -----------------------------------------
    >> Total Database Size: 60.00 GB
    >> Total number of users: 60
    >>
    >> If you could help that would be awesome.
    >>
    >>
    >> "Larry__Weiss" wrote:
    >>

    >>> Try
    >>>
    >>> Write-Host "Total DB Size is $('{0:N2}' -f $TotalDBSize)"
    >>> Write-Host "Total users is $('{0:N2}' -f $TotalUsers)"
    >>>
    >>> - Larry
    >>>
    >>>
    >>> On 5/13/2010 8:05 AM, Don Pedro wrote:
    >>>> I have the following script to retrieve mailbox database sizes and
    >>>> number of
    >>>> users.
    >>>>
    >>>> However the numbers returned on the mailbox database size is to
    >>>> around 12
    >>>> decimal places.
    >>>>
    >>>> I would like
    >>>>
    >>>> 1. $DBSize be displayed to 2 decimal places.
    >>>> 2. $TotalDBSize to be displayed to 2 decimal places.
    >>>>
    >>>> Any ideas how to modify below script?
    >>>>
    >>>>
    >>>> FUNCTION RetrieveData
    >>>> {
    >>>> param ($X, $Y)
    >>>>
    >>>> $db = Get-MailboxDatabase -server $X | sort-object identity
    >>>> foreach ($objItem in $db)
    >>>> {
    >>>> $userCount = 0
    >>>> $userDatabase = $objitem | Get-Mailbox
    >>>> Foreach ($user in $userDatabase) { $userCount++ }
    >>>> $TotalUsers = $TotalUsers + $userCount
    >>>> $edbfilepath = $objItem.edbfilepath
    >>>> $path = "`\`\" + $Y + "`\" +
    >>>> $objItem.EdbFilePath.DriveName.Remove(1).ToString() + "$"+
    >>>> $objItem.EdbFilePath.PathName.Remove(0,2)
    >>>> $dbpath = Get-ChildItem $path
    >>>> $DBSize = ($dbpath.Length/1048576KB)
    >>>> $TotalDBSize = $TotalDBSize+$DBSize;
    >>>>
    >>>> Write-Host $X, $objitem.name, $DBSize, $usercount
    >>>> }
    >>>> Write-Host "Total DB Size is $TotalDBSize"
    >>>> Write-Host "Total users is $TotalUsers"
    >>>> }
    >>>> # Call Function
    >>>> RetrieveData "SERVER01" "SERVER01.CONTOSO.ORG"
    >>> .
    >>>
    >

      My System SpecsSystem Spec

  7. #7


    Chris Dent Guest

    Re: Rounding off numbers to 2 decimal places.


    You'd be pushed to find a better resource than MSDN itself, this
    particular static method is fully documented here:

    http://msdn.microsoft.com/en-us/libr...h_members.aspx

    The values for the MidPointRounding enumeration are here:

    http://msdn.microsoft.com/en-us/libr...trounding.aspx

    In PS you can get the members of the Enumeration with:

    [Enum]::GetNames([MidPointRounding])

    Or both names and values with this (or some variation of this):

    [Enum]::GetNames([MidPointRounding]) | Select-Object @{n='Name';e={ $_
    }}, @{n='Value';e={ [Int][MidPointRounding]$_ }}

    You'd still be short of the meaning, so MSDN isn't really cut out of the
    loop with that.

    Chris

    Larry__Weiss wrote:

    > Is there a commonly used reference book that details these .NET
    > methods?
    >
    > Even with a method as seemingly simple as [Math]::Round
    > I get all these possibilities listed by using
    > Steven Murawski's function get-staticmethoddefinition
    > available at http://poshcode.org/968
    >
    > PS C:> [math] | get-staticmethoddefinition round
    > static double Round(double a),
    > static double Round(double value, int digits),
    > static double Round(double value, System.MidpointRounding mode),
    > static double Round(double value, int digits,
    > System.MidpointRounding mode),
    > static decimal Round(decimal d),
    > static decimal Round(decimal d, int decimals),
    > static decimal Round(decimal d, System.MidpointRounding mode),
    > static decimal Round(decimal d, int decimals,
    > System.MidpointRounding mode)
    > PS C:>
    >
    > After using Google on "System.MidpointRounding" to find
    > http://msdn.microsoft.com/en-us/libr...8VS.85%29.aspx
    >
    > and the especially useful "Community" comment there by Thomas Lee
    > I now can code the variations in PowerShell like
    >
    > PS C:> [Math]::Round(3.45, 1, [System.MidPointRounding]::ToEven)
    > 3.4
    > PS C:> [Math]::Round(3.45, 1, [System.MidPointRounding]::AwayFromZero)
    > 3.5
    > PS C:>
    >
    > But I'd sure like to find a reference text that shows me all the
    > possibilities. Or, does the complexity of these frameworks take
    > them beyond the possibility of being documented with traditionally
    > bound printed volumes?
    >
    > - Larry
    >
    >
    > On 5/13/2010 11:13 AM, Chris Dent wrote:

    >>
    >> I would change it a bit, and I would use [Math]::Round in this scenario.
    >> I hope you find this useful
    >>
    >>
    >> Function RetrieveData($Server)
    >> {
    >> $ShortName = ($Server -Replace '\..*').ToUpper()
    >> Write-Host $ShortName
    >> Write-Host $("-" * $ShortName.Length)
    >>
    >> Get-MailboxDatabase -Server $Server | Sort-Object Identity | %{
    >> $UserCount = ($_ | Get-Mailbox | Measure-Object).Count
    >>
    >> $Path = [String]::Format('\\{0}\{1}$\{2}',
    >> $Server,
    >> $_.EdbFilePath.DriveName.Remove(1).ToString(),
    >> $_.EdbFilePath.PathName.Remove(0,2))
    >> $DBSize = [Math]::Round($((Get-Item $Path).Length / 1Gb), 2)
    >>
    >> $TotalUsers += $UserCount
    >> $TotalDBSize += $DBSize
    >>
    >> $_ | Select-Object `
    >> @{n='Database';e={ $_.Name }}, `
    >> @{n='Size (GB)';e={ $DBSize }}, `
    >> @{n='Qty Mbxs';e={ ($_ | Get-Mailbox | Measure-Object).Count }}
    >> }
    >> Write-Host ""
    >> Write-Host "Total DB Size is $TotalDBSize GB"
    >> Write-Host "Total users is $TotalUsers"
    >> }
    >> # Call the function
    >> RetrieveData "SERVER01.CONTOSO.ORG"
    >>
    >>
    >> Don Pedro wrote:

    >>> Larry, your solution worked first time so I went with that one.
    >>>
    >>> Now, if I could get the output to appear with headers per server that
    >>> would be the icing on the cake...
    >>>
    >>> Desired output would be:
    >>>
    >>> SERVER01
    >>> ------------
    >>> Database Size (GB) Qty Mbxs
    >>> MBX01 10.00 10
    >>> MBX02 20.00 20
    >>> MBX03 30.00 30
    >>>
    >>> -----------------------------------------
    >>> Total Database Size: 60.00 GB
    >>> Total number of users: 60
    >>>
    >>> SERVER02
    >>> ------------
    >>> Database Size (GB) Qty Mbxs
    >>> MBX01 10.00 10
    >>> MBX02 20.00 20
    >>> MBX03 30.00 30
    >>>
    >>> -----------------------------------------
    >>> Total Database Size: 60.00 GB
    >>> Total number of users: 60
    >>>
    >>> If you could help that would be awesome.
    >>>
    >>>
    >>> "Larry__Weiss" wrote:
    >>>
    >>>> Try
    >>>>
    >>>> Write-Host "Total DB Size is $('{0:N2}' -f $TotalDBSize)"
    >>>> Write-Host "Total users is $('{0:N2}' -f $TotalUsers)"
    >>>>
    >>>> - Larry
    >>>>
    >>>>
    >>>> On 5/13/2010 8:05 AM, Don Pedro wrote:
    >>>>> I have the following script to retrieve mailbox database sizes and
    >>>>> number of
    >>>>> users.
    >>>>>
    >>>>> However the numbers returned on the mailbox database size is to
    >>>>> around 12
    >>>>> decimal places.
    >>>>>
    >>>>> I would like
    >>>>>
    >>>>> 1. $DBSize be displayed to 2 decimal places.
    >>>>> 2. $TotalDBSize to be displayed to 2 decimal places.
    >>>>>
    >>>>> Any ideas how to modify below script?
    >>>>>
    >>>>>
    >>>>> FUNCTION RetrieveData
    >>>>> {
    >>>>> param ($X, $Y)
    >>>>>
    >>>>> $db = Get-MailboxDatabase -server $X | sort-object identity
    >>>>> foreach ($objItem in $db)
    >>>>> {
    >>>>> $userCount = 0
    >>>>> $userDatabase = $objitem | Get-Mailbox
    >>>>> Foreach ($user in $userDatabase) { $userCount++ }
    >>>>> $TotalUsers = $TotalUsers + $userCount
    >>>>> $edbfilepath = $objItem.edbfilepath
    >>>>> $path = "`\`\" + $Y + "`\" +
    >>>>> $objItem.EdbFilePath.DriveName.Remove(1).ToString() + "$"+
    >>>>> $objItem.EdbFilePath.PathName.Remove(0,2)
    >>>>> $dbpath = Get-ChildItem $path
    >>>>> $DBSize = ($dbpath.Length/1048576KB)
    >>>>> $TotalDBSize = $TotalDBSize+$DBSize;
    >>>>>
    >>>>> Write-Host $X, $objitem.name, $DBSize, $usercount
    >>>>> }
    >>>>> Write-Host "Total DB Size is $TotalDBSize"
    >>>>> Write-Host "Total users is $TotalUsers"
    >>>>> }
    >>>>> # Call Function
    >>>>> RetrieveData "SERVER01" "SERVER01.CONTOSO.ORG"
    >>>> .
    >>>>
    >>
    --
    Blog: http://www.indented.co.uk
    DnsShell: http://code.msdn.microsoft.com/dnsshell

      My System SpecsSystem Spec

  8. #8


    Don Pedro Guest

    Re: Rounding off numbers to 2 decimal places.

    Thanks to you both.... Excellent solutions.

    "Chris Dent" wrote:

    >
    > I would change it a bit, and I would use [Math]::Round in this scenario.
    > I hope you find this useful
    >
    >
    > Function RetrieveData($Server)
    > {
    > $ShortName = ($Server -Replace '\..*').ToUpper()
    > Write-Host $ShortName
    > Write-Host $("-" * $ShortName.Length)
    >
    > Get-MailboxDatabase -Server $Server | Sort-Object Identity | %{
    > $UserCount = ($_ | Get-Mailbox | Measure-Object).Count
    >
    > $Path = [String]::Format('\\{0}\{1}$\{2}',
    > $Server,
    > $_.EdbFilePath.DriveName.Remove(1).ToString(),
    > $_.EdbFilePath.PathName.Remove(0,2))
    > $DBSize = [Math]::Round($((Get-Item $Path).Length / 1Gb), 2)
    >
    > $TotalUsers += $UserCount
    > $TotalDBSize += $DBSize
    >
    > $_ | Select-Object `
    > @{n='Database';e={ $_.Name }}, `
    > @{n='Size (GB)';e={ $DBSize }}, `
    > @{n='Qty Mbxs';e={ ($_ | Get-Mailbox | Measure-Object).Count }}
    > }
    > Write-Host ""
    > Write-Host "Total DB Size is $TotalDBSize GB"
    > Write-Host "Total users is $TotalUsers"
    > }
    > # Call the function
    > RetrieveData "SERVER01.CONTOSO.ORG"
    >
    >
    > Don Pedro wrote:

    > > Larry, your solution worked first time so I went with that one.
    > >
    > > Now, if I could get the output to appear with headers per server that would
    > > be the icing on the cake...
    > >
    > > Desired output would be:
    > >
    > > SERVER01
    > > ------------
    > > Database Size (GB) Qty Mbxs
    > > MBX01 10.00 10
    > > MBX02 20.00 20
    > > MBX03 30.00 30
    > >
    > > -----------------------------------------
    > > Total Database Size: 60.00 GB
    > > Total number of users: 60
    > >
    > > SERVER02
    > > ------------
    > > Database Size (GB) Qty Mbxs
    > > MBX01 10.00 10
    > > MBX02 20.00 20
    > > MBX03 30.00 30
    > >
    > > -----------------------------------------
    > > Total Database Size: 60.00 GB
    > > Total number of users: 60
    > >
    > > If you could help that would be awesome.
    > >
    > >
    > > "Larry__Weiss" wrote:
    > >
    > >

    > >> Try
    > >>
    > >> Write-Host "Total DB Size is $('{0:N2}' -f $TotalDBSize)"
    > >> Write-Host "Total users is $('{0:N2}' -f $TotalUsers)"
    > >>
    > >> - Larry
    > >>
    > >>
    > >> On 5/13/2010 8:05 AM, Don Pedro wrote:
    > >>
    > >>> I have the following script to retrieve mailbox database sizes and number of
    > >>> users.
    > >>>
    > >>> However the numbers returned on the mailbox database size is to around 12
    > >>> decimal places.
    > >>>
    > >>> I would like
    > >>>
    > >>> 1. $DBSize be displayed to 2 decimal places.
    > >>> 2. $TotalDBSize to be displayed to 2 decimal places.
    > >>>
    > >>> Any ideas how to modify below script?
    > >>>
    > >>>
    > >>> FUNCTION RetrieveData
    > >>> {
    > >>> param ($X, $Y)
    > >>>
    > >>> $db = Get-MailboxDatabase -server $X | sort-object identity
    > >>> foreach ($objItem in $db)
    > >>> {
    > >>> $userCount = 0
    > >>> $userDatabase = $objitem | Get-Mailbox
    > >>> Foreach ($user in $userDatabase) { $userCount++ }
    > >>> $TotalUsers = $TotalUsers + $userCount
    > >>> $edbfilepath = $objItem.edbfilepath
    > >>> $path = "`\`\" + $Y + "`\" +
    > >>> $objItem.EdbFilePath.DriveName.Remove(1).ToString() + "$"+
    > >>> $objItem.EdbFilePath.PathName.Remove(0,2)
    > >>> $dbpath = Get-ChildItem $path
    > >>> $DBSize = ($dbpath.Length/1048576KB)
    > >>> $TotalDBSize = $TotalDBSize+$DBSize;
    > >>>
    > >>> Write-Host $X, $objitem.name, $DBSize, $usercount
    > >>> }
    > >>> Write-Host "Total DB Size is $TotalDBSize"
    > >>> Write-Host "Total users is $TotalUsers"
    > >>> }
    > >>> # Call Function
    > >>> RetrieveData "SERVER01" "SERVER01.CONTOSO.ORG"
    > >>>
    > >> .
    > >>
    > >>
    >
    > --
    > Blog: http://www.indented.co.uk
    > DnsShell: http://code.msdn.microsoft.com/dnsshell
    > .
    >

      My System SpecsSystem Spec

  9. #9


    Larry__Weiss Guest

    .NET and PowerShell

    Thanks!

    Now Microsoft needs to add a PowerShell column to the examples
    in the Syntax and Examples section of those webpages like

    http://msdn.microsoft.com/en-us/libr...h_members.aspx

    I suppose there should be a fairly mechanical way to translate the
    C# examples to PowerShell. Has anyone written up a description
    of how to translate C# to PowerShell?

    I did see this video by Doug Finke of an edit session of translation
    of C# to PowerShell and some iterative refactoring thereafter.

    http://dougfinke.com/video/CSharpToPowerShell..html

    - Larry


    On 5/14/2010 4:05 AM, Chris Dent wrote:

    >
    > You'd be pushed to find a better resource than MSDN itself, this
    > particular static method is fully documented here:
    >
    > http://msdn.microsoft.com/en-us/libr...h_members.aspx
    >
    > The values for the MidPointRounding enumeration are here:
    >
    > http://msdn.microsoft.com/en-us/libr...trounding.aspx
    >
    > In PS you can get the members of the Enumeration with:
    >
    > [Enum]::GetNames([MidPointRounding])
    >
    > Or both names and values with this (or some variation of this):
    >
    > [Enum]::GetNames([MidPointRounding]) | Select-Object @{n='Name';e={ $_
    > }}, @{n='Value';e={ [Int][MidPointRounding]$_ }}
    >
    > You'd still be short of the meaning, so MSDN isn't really cut out of the
    > loop with that.
    >
    > Chris
    >
    > Larry__Weiss wrote:

    >> Is there a commonly used reference book that details these .NET
    >> methods?
    >>
    >> Even with a method as seemingly simple as [Math]::Round
    >> I get all these possibilities listed by using
    >> Steven Murawski's function get-staticmethoddefinition
    >> available at http://poshcode.org/968
    >>
    >> PS C:> [math] | get-staticmethoddefinition round
    >> static double Round(double a),
    >> static double Round(double value, int digits),
    >> static double Round(double value, System.MidpointRounding mode),
    >> static double Round(double value, int digits, System.MidpointRounding
    >> mode),
    >> static decimal Round(decimal d),
    >> static decimal Round(decimal d, int decimals),
    >> static decimal Round(decimal d, System.MidpointRounding mode),
    >> static decimal Round(decimal d, int decimals, System.MidpointRounding
    >> mode)
    >> PS C:>
    >>
    >> After using Google on "System.MidpointRounding" to find
    >> http://msdn.microsoft.com/en-us/libr...8VS.85%29.aspx
    >>
    >> and the especially useful "Community" comment there by Thomas Lee
    >> I now can code the variations in PowerShell like
    >>
    >> PS C:> [Math]::Round(3.45, 1, [System.MidPointRounding]::ToEven)
    >> 3.4
    >> PS C:> [Math]::Round(3.45, 1, [System.MidPointRounding]::AwayFromZero)
    >> 3.5
    >> PS C:>
    >>
    >> But I'd sure like to find a reference text that shows me all the
    >> possibilities. Or, does the complexity of these frameworks take
    >> them beyond the possibility of being documented with traditionally
    >> bound printed volumes?
    >>
    >> - Larry
    >>

      My System SpecsSystem Spec

  10. #10


    Chris Dent Guest

    Re: .NET and PowerShell


    A fair number do have PS examples, especially in the DirectoryServices
    branches. I was trying to write some for the old iADS interfaces, but it
    never seems to successfully submit.

    I should add a few examples to the System.Net.Sockets members since I
    enjoy using those so much.

    Chris

    Larry__Weiss wrote:

    > Thanks!
    >
    > Now Microsoft needs to add a PowerShell column to the examples
    > in the Syntax and Examples section of those webpages like
    >
    > http://msdn.microsoft.com/en-us/libr...h_members.aspx
    >
    > I suppose there should be a fairly mechanical way to translate the
    > C# examples to PowerShell. Has anyone written up a description
    > of how to translate C# to PowerShell?
    >
    > I did see this video by Doug Finke of an edit session of translation
    > of C# to PowerShell and some iterative refactoring thereafter.
    >
    > http://dougfinke.com/video/CSharpToPowerShell..html
    >
    > - Larry
    >
    >
    > On 5/14/2010 4:05 AM, Chris Dent wrote:

    >>
    >> You'd be pushed to find a better resource than MSDN itself, this
    >> particular static method is fully documented here:
    >>
    >> http://msdn.microsoft.com/en-us/libr...h_members.aspx
    >>
    >> The values for the MidPointRounding enumeration are here:
    >>
    >> http://msdn.microsoft.com/en-us/libr...trounding.aspx
    >>
    >> In PS you can get the members of the Enumeration with:
    >>
    >> [Enum]::GetNames([MidPointRounding])
    >>
    >> Or both names and values with this (or some variation of this):
    >>
    >> [Enum]::GetNames([MidPointRounding]) | Select-Object @{n='Name';e={ $_
    >> }}, @{n='Value';e={ [Int][MidPointRounding]$_ }}
    >>
    >> You'd still be short of the meaning, so MSDN isn't really cut out of the
    >> loop with that.
    >>
    >> Chris
    >>
    >> Larry__Weiss wrote:

    >>> Is there a commonly used reference book that details these .NET
    >>> methods?
    >>>
    >>> Even with a method as seemingly simple as [Math]::Round
    >>> I get all these possibilities listed by using
    >>> Steven Murawski's function get-staticmethoddefinition
    >>> available at http://poshcode.org/968
    >>>
    >>> PS C:> [math] | get-staticmethoddefinition round
    >>> static double Round(double a),
    >>> static double Round(double value, int digits),
    >>> static double Round(double value, System.MidpointRounding mode),
    >>> static double Round(double value, int digits, System.MidpointRounding
    >>> mode),
    >>> static decimal Round(decimal d),
    >>> static decimal Round(decimal d, int decimals),
    >>> static decimal Round(decimal d, System.MidpointRounding mode),
    >>> static decimal Round(decimal d, int decimals, System.MidpointRounding
    >>> mode)
    >>> PS C:>
    >>>
    >>> After using Google on "System.MidpointRounding" to find
    >>> http://msdn.microsoft.com/en-us/libr...8VS.85%29.aspx
    >>>
    >>>
    >>> and the especially useful "Community" comment there by Thomas Lee
    >>> I now can code the variations in PowerShell like
    >>>
    >>> PS C:> [Math]::Round(3.45, 1, [System.MidPointRounding]::ToEven)
    >>> 3.4
    >>> PS C:> [Math]::Round(3.45, 1, [System.MidPointRounding]::AwayFromZero)
    >>> 3.5
    >>> PS C:>
    >>>
    >>> But I'd sure like to find a reference text that shows me all the
    >>> possibilities. Or, does the complexity of these frameworks take
    >>> them beyond the possibility of being documented with traditionally
    >>> bound printed volumes?
    >>>
    >>> - Larry
    >>>
    --
    Blog: http://www.indented.co.uk
    DnsShell: http://code.msdn.microsoft.com/dnsshell

      My System SpecsSystem Spec

Page 1 of 2 12 LastLast
Rounding off numbers to 2 decimal places. problems?

Similar Threads
Thread Thread Starter Forum Replies Last Post
Rounding of a [datetime] RickB PowerShell 5 05 Sep 2008
Stock Gadget decimal places Qualnhick Vista General 0 24 Nov 2007
Rounding. Andrew Savinykh PowerShell 0 05 Mar 2007
Rounding off numbers with .NET Marco Shaw PowerShell 3 11 Dec 2006
Question regarding negative (hexa)decimal numbers =?Utf-8?B?ZHJlZXNjaGtpbmQ=?= PowerShell 3 19 Sep 2006