Microsoft Virtualization Discussions

Powercli help in looping


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!





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

C:\Scripts\PowerShell\Projects\CreateVolumeSnapshots>type CreateVolumeSnapshots.csv

PowerShell Code:


   [Parameter(Mandatory=$True, HelpMessage="The cluster name or IP Address")]   
   [Parameter(Mandatory=$False, HelpMessage="The credentials to connect to the cluster")]   
#'Prompt for credentials if not provided.
   [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
   Write-Warning "The file ""$fileSpec"" does not exist"
#'Import the DataONTAP PowerShell Module.
   [String]$moduleName = "DataONTAP"
   Import-Module $moduleName -ErrorAction Stop
   Write-Host "Imported module ""$moduleName"""
   Write-Warning -Message "Failed importing module ""$moduleName"""
#'Connect to the cluster.
   Connect-NcController -Name $Cluster -HTTPS -Credential $Credentials -ErrorAction Stop | Out-Null
   Write-Host "Connected to cluster ""$Cluster"""
   Write-Warning -Message $("Failed connecting to cluster ""$Cluster"". Error " + $_.Exception.Message)
#'Process each line to create a snapshot on the vservers volume.
For($i = 1; ($i -le $lines.Count -1); $i++){
         $elements = $lines[$i].Split(",")
         If($elements.Count -eq 3){
            [String]$vserverName  = $elements[0] 
            [String]$volumeName   = $elements[1]
            [String]$snapshotName = $elements[2]
      If((-Not([String]::IsNullOrEmpty($vserverName))) -And `
         (-Not([String]::IsNullOrEmpty($volumeName)))  -And `
         [String]$timeStamp = Get-Date -uformat "%Y_%m_%d_%H_%M_%S"
         [String]$command   = "New-NcSnapshot -Volume $VolumeName -Snapshot $snapshotName`_$timeStamp -VserverContext $vserverName -ErrorAction Stop"
            Invoke-Expression -Command $command -ErrorAction Stop | Out-Null
            Write-Host "Executed Command`: $command" -ForegroundColor Cyan
            Write-Warning -Message $("Failed Executing Command`: $command. Error " + $_.Exception.Message)

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
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
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.




If this post resolved your issue, help others by selecting ACCEPT AS SOLUTION or adding a KUDO.
Register for Insight 2021 Digital

INSIGHT 2021 Digital: Meet the Specialists 2

On October 20-22, gear up for a fully digital, totally immersive virtual experience with a downright legendary lineup of world-renowned specialists. Tune in for visionary conversations, solution deep dives, technical sessions and more.

NetApp on Discord Image

We're on Discord, are you?

Live Chat, Watch Parties, and More!

Explore Banner

Meet Explore, NetApp’s digital sales platform

Engage digitally throughout the sales process, from product discovery to configuration, and handle all your post-purchase needs.

NetApp Insights to Action
I2A Banner