Thursday, 20 October 2016

Powershell: Transform object properties in the pipeline

You can use the pipeline to reduce data sets and view important information in powershell.

Take this line of vSphere PowerCLI for example:

$DStores | Select Name, FreeSpaceGB, CapacityGB

This will present some information on the datastores.

Wouldn't it be nice to be able to get the percent free on each of the datastores too? You can do this by transforming the properties using an expression as below:

$DStores | Select Name, FreeSpaceGB, CapacityGB,
        @{Name="FreePercent"; Expression={(($_.FreeSpaceGB/$_.CapacityGB)*100}} |
        Sort -Property FreePercent -Descending

This will create a new property in the pipeline called FreePercent that can be operated on in the same way as a normal property. In this example I sort using the new property.

Tuesday, 11 October 2016

Create Snapshots of an Azure RM VM


Powershell Functions for Creating Azure RM VM Snapshots


Load the Functions:
C:> . .\AzureSnapFunctions.PS1

Create a New snapshot for all VHDs in a VM
C:> Snap-AzureRMVM -VMName MyVM -SnapshotName "Snap 1"

View the snapshots for all VHDs on a VM
C:> Get-AzureRMVMSnap -VMName MyVM

Delete all snapshots for all VHDs on a VM
C:> Delete-AzureRMVMSnap -VMName MyVM

Revert to a snapshot for all VHDs on a VM
C:> Revert-AzureRMVMSnap -VMName MyVM