1

In the PS command below I'm getting the name, creationTimeStamp, Disk_Size and storageBytes of yesterday's snapshots in my gcp project and outputting the data to a csv file (which is later converted to HTML and emailed):

$csv = gcloud --project $gcpProject compute snapshots list --format="csv(name,creationTimestamp,diskSizeGb,storageBytes)" --filter="creationTimestamp.date('%Y-%m-%d')=$yesterday" | Out-File C:\data.csv

The result looks something like this (the number of snapshots displayed varies):

+---------------------------+-------------------------------+--------------+---------------+
|               NAME        |       CREATION_TIMESTAMP      | DISK_SIZE_GB | STORAGE_BYTES |
+---------------------------+-------------------------------+--------------+---------------+
| snapshot1-us-central1     | 2019-10-24T19:24:09.061-07:00 | 50           | 1250586048    |
| snapshot2-data-us-east1   | 2019-10-24T19:01:49.791-07:00 | 150          | 425018600     |
+---------------------------+-------------------------------+--------------+---------------+

This is good except that the STORAGE_BYTES data is all in bytes thus making it hard to read. How can I record this data in MB instead in the csv file (or just replacing this data in the csv file that's in bytes with MB)

yorkman
  • 119
  • 2
  • 16
  • If you want to convert the bytes to the closest readable format (depending on the actual size), you might want to invoke this [Format-ByteSize](https://stackoverflow.com/a/57535324/1701026) function. – iRon Oct 28 '19 at 18:27

2 Answers2

1
$data=Get-Content C:\data.csv
$NewData=@($data[0..2])#Adding Header in a new Variable
$Data[3..$($data.Count - 2)]|Foreach{
$startRange=$_.LastIndexOf("| ")+1
$length=$_.LastIndexOf("  |") - $_.LastIndexOf("| ")
#Converting Bytes into MB and replacing in the line
$NewData+=$_.Replace(($_.Substring($startRange,$length)).Trim(),"$([math]::Round(($_.Substring($startRange,$length)/1MB),2))   ")
}
#Adding last line
$NewData+=$data[$data.Count - 1]
#Modifying exsiting file
$NewData |Set-Content C:\data.csv -Force
iMMi
  • 11
  • 1
  • Add some descriptive texts explaining the code. Maybe you could read https://stackoverflow.com/help/how-to-answer – Prasoon Karunan V Oct 29 '19 at 18:07
  • I get errors: "Exception calling "Replace" with "2" argument(s): "String cannot be of zero length. Parameter name: oldValue" Remember result above is just printed to the screen like that. That's not how it's saved in the csv file. That table is produced solely by the gcloud command and is only output in PS. Thank you for the alternative method which I'm sure we could get it to work but as I said I already have it working using get-gcesnapshot with property matching using -match for yesterday's date (thanks Prasoon). – yorkman Nov 05 '19 at 15:43
0

I think, it should be simple using GCP PowerShell cmdlets.

Get-GceSnapshot -Project $gcpProject

I never used it, but you can use calculated properties to convert the size in oneline, an example below

Get-Process | Select-Object -First 3 -Property Name,@{E={$_.STORAGE_BYTES/1024};L='Size'}

PS: assuming the size property name is STORAGE_BYTES

https://googlecloudplatform.github.io/google-cloud-powershell/#/google-compute-engine/GceSnapshot/Get-GceSnapshot

Prasoon Karunan V
  • 2,916
  • 2
  • 12
  • 26
  • Thanks, but what you've provided is a totally different way of displaying all the snapshots with a lot of data I don't need. It's also not in CSV format and it wouldn't get filtered by yesterday's snapshots only. So this wouldn't work with my current script and would require I'd come up with a new script of how to capture and filter only what I need, not to mention it wouldn't be displayed the same way so this doesn't work for me. – yorkman Oct 28 '19 at 17:39
  • I figured out how to export only the data I want using your Get-GceSnapshot command to a CSV file but I'm not able to filter the data by yesterday's date. I tried with Get-GceSnapshot -Project $gcpProject -Filter "$yesterdaysDate" but it says a parameter cannot be found that matches parameter name 'Filter'. I have $yesterdaysDate = [datetime]::Today.AddDays(-1).ToString('yyyy-MM-dd') – yorkman Oct 28 '19 at 18:08
  • 2
    Well you'd probably do this in 2 mins but after a couple of hrs I finally got everything I needed with get-gcesnapshot. The Select-Object -Property Name...was also very helpful. Thanks! – yorkman Oct 28 '19 at 18:53