Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Mute
- Printer Friendly Page
Need peice of Powershell code to convert bytes to highest unit
2016-02-10
07:34 AM
6,297 Views
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
Solved! See The Solution
1 ACCEPTED SOLUTION
netappwala has accepted the solution
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
7 REPLIES 7
netappwala has accepted the solution
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I think DOT 7 Mode doesn't have that property 😞
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
