Software Development Kit (SDK) and API Discussions

Need peice of Powershell code to convert bytes to highest unit

netappwala
5,959 Views

Hi

 

I am writing a script for volume reporting. Can anyone tell me best way converting bytes to higest possible unit.

 

and is $vol.SnapshotBlocksReserved return size in blocks? do I have to divide this with 1024 to convert into bytes?

 

 

 

Thanks

Charan

1 ACCEPTED SOLUTION

asulliva
5,952 Views

Use the "ConvertTo-FormattedNumber" cmdlet which ships with the NetApp PowerShell toolkit.  This will convert bytes to a human readable number...

 

PS C:\> ConvertTo-FormattedNumber -Value 1024000 -Type DataSize -NumberFormatString 0
1000 KB

PS C:\> ConvertTo-FormattedNumber -Value 10240000 -Type DataSize -NumberFormatString 0
10 MB

PS C:\> ConvertTo-FormattedNumber -Value 10240000 -Type DataSize -NumberFormatString 0.0
9.8 MB PS C:\> ConvertTo-FormattedNumber -Value 102400000 -Type DataSize -NumberFormatString 0 98 MB

 

Notice that it will round based on the "-NumberFormatString" value.

 

If the result is in blocks, then each one represents 4096 bytes (4KB).  You'll need to multiply by 4096.  

 

Hope that helps.

 

Andrew

If this post resolved your issue, please help others by selecting ACCEPT AS SOLUTION or adding a KUDO.

View solution in original post

7 REPLIES 7

asulliva
5,953 Views

Use the "ConvertTo-FormattedNumber" cmdlet which ships with the NetApp PowerShell toolkit.  This will convert bytes to a human readable number...

 

PS C:\> ConvertTo-FormattedNumber -Value 1024000 -Type DataSize -NumberFormatString 0
1000 KB

PS C:\> ConvertTo-FormattedNumber -Value 10240000 -Type DataSize -NumberFormatString 0
10 MB

PS C:\> ConvertTo-FormattedNumber -Value 10240000 -Type DataSize -NumberFormatString 0.0
9.8 MB PS C:\> ConvertTo-FormattedNumber -Value 102400000 -Type DataSize -NumberFormatString 0 98 MB

 

Notice that it will round based on the "-NumberFormatString" value.

 

If the result is in blocks, then each one represents 4096 bytes (4KB).  You'll need to multiply by 4096.  

 

Hope that helps.

 

Andrew

If this post resolved your issue, please help others by selecting ACCEPT AS SOLUTION or adding a KUDO.

netappwala
5,922 Views

Hi Andrew,

 

Is there any cmdlet to check volume and snapshot used capacity[the value return by "df -h" command's output("Capacity" field)] in percentage?

 

 

 

Thanks,

Charan

asulliva
5,919 Views

It's returned by Get-NcVol in the "VolumeSpaceAttributes" property...

 

Get-NcVol | Select Name,@{'N' = "PercentageSnapShotReserveUsed"; 'E' = { $_.VolumeSpaceAttributes.PercentageSnapshotReserveUsed }}

Andrew

If this post resolved your issue, please help others by selecting ACCEPT AS SOLUTION or adding a KUDO.

netappwala
5,883 Views

I think DOT 7 Mode doesn't have that property 😞

asulliva
5,846 Views

7-mode objects don't directly return as much information, but we can still get the data by querying for the oldest snapshot and using it's cumulative total:

 

Get-NaVol | %{ 
    $data = "" | Select Filesystem,Total,Used,Avail,Capacity
    
    $snapReserve = $_.SnapshotBlocksReserved * 1024
    $snapUsed = ($_ | Get-NaSnapshot | Sort-Object -Property Created | Select-Object -First 1).CumulativeTotal

    $data.Filesystem = "/vol/$($_.Name)/.snapshot"
    $data.Total = ConvertTo-FormattedNumber $snapReserve DataSize
    $data.Used = ConvertTo-FormattedNumber $snapUsed DataSize
    $data.Avail = ConvertTo-FormattedNumber ($snapReserve - $snapUsed) DataSize
    $data.Capacity = ConvertTo-FormattedNumber (($snapUsed / $snapReserve) * 100) Percent

    $data
}

Andrew

If this post resolved your issue, please help others by selecting ACCEPT AS SOLUTION or adding a KUDO.

netappwala
5,809 Views

Thats really useful Andrew. Below is what I found an alternative way.

 

((get-naefficiency Vol0).snapusage).Reserve

 

((get-naefficiency Vol0).snapusage).used

 

Now need help to create an array of snapshots created dates for a volume.  I know Its more powershell related query than Data ONTAP.

 

Eg: If the Vol0 has 10 snapshots, I want to create a string array of size 10 that each eliment carries one snapshot's created date.

 

 

Thanks

Kabir Charan

asulliva
5,792 Views

The DateTime object is somewhat more useful...you can do comparisons and intervals and such much easier...

 

 

$snapTimes= (Get-NaSnapshot myvol).AccessTimeDT

Plus you can simply pipe to Out-String to get an actual string if needed...

 

 

$snapTimes= (Get-NaSnapshot myvol).AccessTimeDT | Out-String

Hope that helps.

 

Andrew

If this post resolved your issue, please help others by selecting ACCEPT AS SOLUTION or adding a KUDO.
Public