Search Your Compellent Storage Center Using Windows PowerShell

Using Windows PowerShell and the Compellent Storage Center Command Set for PowerShell, I have created a simple script that performs searches for server and volume objects providing results of each in addition to their related mappings.  This can be helpful if you have multiple levels of server or volume folders.

# Name:       Storage Center Search
# Usage:      Search for volume or server objects across a Storage Center
# Author:     Justin Braun
# Date:              February 17, 2010

# This sample script is provided "as is" with no warranty whatsoever; 
# you assume all responsibilty of the consequences of use.

$scname = Read-Host "What Storage Center do you want to search? (IP or hostname)"
$scusername = Read-Host "What is the username for your account on this Storage Center?"
$scpassword = Read-Host -AsSecureString -Prompt "What is the password?"
$findsource = Read-Host "What type of object are you looking for? (S=server, V=volume)"
$searchterm = Read-Host "Enter the text you want to search for (wildcards OK)"

if($findsource -ne "S" -and $findsource -ne "V")
{
       # Invalid parameter entered.
       Write-Host "`nInvalid search source specified.`n" -ForegroundColor Red
       Break
}

# Valid parameter found - start script

# Instantiate connection
Write-Host "`nOpening connection...";

try
{
       $conn = Get-SCConnection -HostName $scname -User $scusername -Password $scpassword;
}
catch
{
       Write-Host "Error: " $_;
       Break;
}

Write-Host "Connection established to $scname...";

# What are we searching for?
switch ($findsource)
{
       "S"
       {
              # Find a server
              $servers = Get-SCServer -Connection $conn | where { $_.Name -like $searchterm }

              if($servers -ne $null)
              {
                     # Found some objects that might match
                     Write-Host "`nThe following servers might match your query:"
                     $servers | ft -AutoSize Name, LogicalPath, WorldWideNames
              }
              else
              {
                     # No objects found
                     Write-Host "`nSorry, no objects were found that match your query."
              }
       }

       "V"
       {
              # Find a volume
              $vols = Get-SCVolume -Connection $conn | where { $_.Name -like $searchterm }

              if($vols -ne $null)
              {
                     # Found some objects that might match
                     Write-Host "`nThe following volumes might match your query:"

                     foreach($vol in $vols)
                     {
                           # Create an output object for table formatting
                           $obj = new-Object PSObject;

                           # Add member properties with their name and value pair
                           $obj | Add-Member NoteProperty VolumeName($vol.Name);
                           $obj | Add-Member NoteProperty LogicalPath($vol.LogicalPath);

                           #Get Mappings to volume
                           $mappings = Get-SCVolumeMap -VolumeIndex $vol.Index -Connection $conn

                           if($mappings -ne $null)
                           {
                                  $maps = ""
                                  foreach($mapping in $mappings)
                                  {
                                         $maps += $mapping.ServerName + " "
                                  }

                                  $obj | Add-Member NoteProperty Mappings($maps);
                           }
                           else
                           {
                                  # No mappings found
                                  $obj | Add-Member NoteProperty Mappings("None");
                           }

                           # Write the output to the screen
                           Write-Output $obj;
                     }
              }
              else
              {
                     # No objects found
                     Write-Host "`nSorry, no objects were found that match your query."
              }
       }
}

If you have ideas for scripts or would like to share your creations with other Compellent customers, check out the Forums at the Compellent Customer Portal. [authentication required]