Microsoft Virtualization Discussions

Powercli help in looping

heightsnj
2,595 Views

I came from a bash background, and need your help to create a looping to create snapshots. Say I have a file which contains multple lines of the volume name and also a snapshot name. How do I create such loop and take these two variable, then in the body to run the snapshot creating script:

New-NcSnapshot vol-name-variable1 snap-name-variable1

 

 

In addtion, what if I wanted to add a date to append to the variable, like snap-name-variable-01172018?

 

 

Thanks a lot!

1 REPLY 1

mbeattie
2,533 Views

Hi,

 

I'm assuming there is a reason you want to take a manual snapshot instead of using a snapshot policy? It's possbile to do but whenever possible let ontap handle snapshot creation.

Here is some code that will do what you want. You could simplify it as it contains error checking\comments\validatation. Also contains an inner do loop within the for loop to validate the line in the file contains the correct number of elements (vserver name, volume name, snapshot name)

 

Directory structure:

 

 

C:\Scripts\PowerShell\Projects\CreateVolumeSnapshots>dir /b
CreateVolumeSnapshots.csv
CreateVolumeSnapshots.ps1

C:\Scripts\PowerShell\Projects\CreateVolumeSnapshots>type CreateVolumeSnapshots.csv
VserverName,VolumeName,SnapshotName
vserver1,cifs_data_001,snapshot_001
vserver2,nfs_data_001,snapshot_001

PowerShell Code:

 

Param(
   [Parameter(Mandatory=$True, HelpMessage="The cluster name or IP Address")]   
   [String]$Cluster,
   [Parameter(Mandatory=$False, HelpMessage="The credentials to connect to the cluster")]   
   [System.Management.Automation.PSCredential]$Credentials
)
#'------------------------------------------------------------------------------
#'Prompt for credentials if not provided.
#'------------------------------------------------------------------------------
If(-Not($Credentials)){
   [System.Management.Automation.PSCredential]$Credentials = Get-Credential -Credential admin
}
#'------------------------------------------------------------------------------
#'Initialization Section. Define Global Variables.
#'------------------------------------------------------------------------------
[String]$scriptPath     = Split-Path($MyInvocation.MyCommand.Path)
[String]$scriptSpec     = $MyInvocation.MyCommand.Definition
[String]$scriptBaseName = (Get-Item $scriptSpec).BaseName
[String]$scriptName     = (Get-Item $scriptSpec).Name
[String]$fileSpec       = "$scriptPath\$scriptBaseName.csv"
#'------------------------------------------------------------------------------
#'Read the scripts configuration file.
#'------------------------------------------------------------------------------
If(Test-Path -Path $fileSpec){
   $lines = Get-Content -Path $fileSpec
}Else{
   Write-Warning "The file ""$fileSpec"" does not exist"
   Break;
}
#'------------------------------------------------------------------------------
#'Import the DataONTAP PowerShell Module.
#'------------------------------------------------------------------------------
Try{
   [String]$moduleName = "DataONTAP"
   Import-Module $moduleName -ErrorAction Stop
   Write-Host "Imported module ""$moduleName"""
}Catch{
   Write-Warning -Message "Failed importing module ""$moduleName"""
   Break;
}
#'------------------------------------------------------------------------------
#'Connect to the cluster.
#'------------------------------------------------------------------------------
Try{
   Connect-NcController -Name $Cluster -HTTPS -Credential $Credentials -ErrorAction Stop | Out-Null
   Write-Host "Connected to cluster ""$Cluster"""
}Catch{
   Write-Warning -Message $("Failed connecting to cluster ""$Cluster"". Error " + $_.Exception.Message)
   Break;
}
#'------------------------------------------------------------------------------
#'Process each line to create a snapshot on the vservers volume.
#'------------------------------------------------------------------------------
For($i = 1; ($i -le $lines.Count -1); $i++){
   Do{
      If($lines[$i].Contains(",")){
         $elements = $lines[$i].Split(",")
         If($elements.Count -eq 3){
            [String]$vserverName  = $elements[0] 
            [String]$volumeName   = $elements[1]
            [String]$snapshotName = $elements[2]
         }Else{
            Break;
         }
      }Else{
         Break;
      }
      If((-Not([String]::IsNullOrEmpty($vserverName))) -And `
         (-Not([String]::IsNullOrEmpty($volumeName)))  -And `
         (-Not([String]::IsNullOrEmpty($snapshotName)))){
         [String]$timeStamp = Get-Date -uformat "%Y_%m_%d_%H_%M_%S"
         [String]$command   = "New-NcSnapshot -Volume $VolumeName -Snapshot $snapshotName`_$timeStamp -VserverContext $vserverName -ErrorAction Stop"
         Try{
            Invoke-Expression -Command $command -ErrorAction Stop | Out-Null
            Write-Host "Executed Command`: $command" -ForegroundColor Cyan
         }Catch{
            Write-Warning -Message $("Failed Executing Command`: $command. Error " + $_.Exception.Message)
            Break;
         }
      }
   }Until($True)
}
#'------------------------------------------------------------------------------

PowerShell Output:

 

PS C:\Scripts\PowerShell\Projects\CreateVolumeSnapshots> .\CreateVolumeSnapshots.ps1 -Cluster cluster1.testlab.local -Credentials $credentials
Imported module "DataONTAP"
Connected to cluster "cluster1.testlab.local"
Executed Command: New-NcSnapshot -Volume cifs_data_001 -Snapshot snapshot_001_2018_01_18_16_22_51 -VserverContext vserver1 -ErrorAction Stop
Executed Command: New-NcSnapshot -Volume nfs_data_001 -Snapshot snapshot_001_2018_01_18_16_22_52 -VserverContext vserver2 -ErrorAction Stop

CLI Output:

 

cluster1::*> snap list -vserver vserver1 -volume cifs_data_001
                                                                 ---Blocks---
Vserver  Volume   Snapshot                                  Size Total% Used%
-------- -------- ------------------------------------- -------- ------ -----
vserver1 cifs_data_001
                  weekly.2018-01-07_0015                   460KB     0%   10%
                  weekly.2018-01-14_0015                   500KB     0%   11%
                  daily.2018-01-17_0010                    504KB     0%   11%
                  daily.2018-01-18_0010                    440KB     0%   10%
                  hourly.2018-01-18_1105                   316KB     0%    7%
                  hourly.2018-01-18_1205                    88KB     0%    2%
                  hourly.2018-01-18_1305                   296KB     0%    7%
                  hourly.2018-01-18_1405                    88KB     0%    2%
                  hourly.2018-01-18_1505                   316KB     0%    7%
                  hourly.2018-01-18_1605                    84KB     0%    2%
                  snapshot_001_2018_01_18_16_22_51         308KB     0%    7%
11 entries were displayed.

cluster1::*> snap list -vserver vserver2 -volume nfs_data_001
                                                                 ---Blocks---
Vserver  Volume   Snapshot                                  Size Total% Used%
-------- -------- ------------------------------------- -------- ------ -----
vserver2 nfs_data_001
                  weekly.2018-01-07_0015                   488KB     0%   50%
                  weekly.2018-01-14_0015                   500KB     0%   50%
                  daily.2018-01-17_0010                    484KB     0%   50%
                  daily.2018-01-18_0010                    416KB     0%   46%
                  hourly.2018-01-18_1105                   216KB     0%   31%
                  hourly.2018-01-18_1205                    88KB     0%   15%
                  hourly.2018-01-18_1305                   268KB     0%   35%
                  hourly.2018-01-18_1405                   260KB     0%   35%
                  hourly.2018-01-18_1505                    88KB     0%   15%
                  hourly.2018-01-18_1605                   252KB     0%   34%
                  snapshot_001_2018_01_18_16_22_52         248KB     0%   34%
11 entries were displayed.

 

Use that as an "example" and modify to meet your requirements.

 

/Matt

 

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