Software Development Kit (SDK) and API Discussions

Need peice of Powershell code to convert bytes to highest unit

netappwala
4,873 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
4,866 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
4,867 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
4,836 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
4,833 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
4,797 Views

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

asulliva
4,760 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
4,723 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
4,706 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