We Can Assign Disks for you Wholesale (DiskAutoAssigner.ps1)

Following on from the subject matter of the previous post Understanding Disk Auto Assignment, here’s a Proof of Concept PowerShell script for those situations where disk Auto Assign won’t work. It’s a fairly simple script. Run it as>

.\DiskAutoAssigner.ps1 -Clusters {enter a comma separated list of clusters here - or just the one}

An example of using it is illustrated in the image below.

Image: Example of DiskAutoAssigner.ps1
First run: We target two clusters. We only have the credential for one cluster so the program just connects to that. We’ve not connected to this cluster before so we save a disk assignments XML file.

Second run: Like above, but since this is our second/a subsequent run, we have a disk history file for 10.3.5.50. The program checks for unassigned disks, and checks these unassigned disks against the previous run. If there are no unassigned disks that weren’t previously assigned the program does nothing.

Third run: I’d unassigned disk NET-2.20, and the program detects this and assigns it back to the original owner.

Note: This was tested against a NetApp Clustered ONTAP 2-node 8.3.2 SIM cluster.

The Script

Copy into a text editor and save as DiskAutoAssigner.ps1.


Param([Parameter(Mandatory=$True)][System.Array]$Clusters)

FUNCTION Wr{
  Param([String]$P="",[String]$C="WHITE")
  Write-Host $P -ForegroundColor $C
  [String]$Category = "INFO "
  If($C -eq "RED"){ $Category = "ERROR" }             
  ([String](Get-Date -uformat %c) + " | " + $Category + " | " + $P) >> "DiskAutoAssigner.log.txt"
}

If(!(Get-Module DataONTAP)){ [Void](Import-Module DataONTAP -ErrorAction SilentlyContinue) }
If(!(Get-Module DataONTAP)){ Wr "Failed to load DataONTAP PSTK!" RED; EXIT }
Wr "Loaded DataONTAP PSTK" GREEN

[System.Object]$FoundCredential = @{}
$Clusters | Foreach {
  [Boolean]$FoundCredential.$_ = $FALSE
  If(Get-NcCredential $_){ $FoundCredential.$_ = $TRUE }
  else{ Wr "No credential for cluster $_ - supply credentials with Add-NcCredential!" RED }
}

$Clusters | Foreach {
  $Global:CurrentNcController = $NULL
  If($FoundCredential.$_){
    [Void](Connect-NcController $_ -ErrorAction SilentlyContinue)
    If(!$Global:CurrentNcController){ Wr "Failed to connnect to $_!" RED }
    else{
      Wr "Connected to $_" GREEN
      $SavedDisks = $NULL
      If(Test-Path "$_.disklist.xml"){
        Wr "Found disk history file $_.disklist.xml" GREEN
        $SavedDisks = Import-Clixml "$_.disklist.xml"
        $DiskAttrs = Get-NcDisk -Template
        Initialize-NcObjectProperty -Object $DiskAttrs -Name DiskOwnershipInfo
        $DiskAttrs.DiskOwnershipInfo.OwnerNodeName = ""                   
        $Disks = Get-NcDisk -Attributes $DiskAttrs
        $Unassigned = $Disks | where{ $_.DiskOwnershipInfo.OwnerNodeName -eq $NULL }
        Foreach ($Disk in $Unassigned){
          $SavedDisk = $SavedDisks | Where{ $_.Name -eq $Disk.Name }
          If($SavedDisk.DiskOwnershipInfo.OwnerNodeName){
            [Void](Set-NcDiskOwner $Disk.Name -Owner $SavedDisk.DiskOwnershipInfo.OwnerNodeName)
            Wr ("Assigned Disk " + $Disk.Name + " to " + $SavedDisk.DiskOwnershipInfo.OwnerNodeName) CYAN
          }
        }
      }
      Get-NcDisk -Attributes $DiskAttrs | Export-Clixml "$_.disklist.xml"
      Wr "Saved disk history file $_.disklist.xml" GREEN
    }
  }
}


Comments