CIFS Server Renamer

Recently I posted:

This post follows a similar theme. In this case we want a tool that easily allows us to rename the CIFS Server name of many CIFS Servers in one go. Not totally sure if there’s much use case for this, but migrations/transitions is a good one.

Firstly, you need a CSV with your vserver and new CIFS server name in. The column titles are “vserver” and “newname”.

Image: CSV used by CIFS Server Renamer
Then you run the script from down below - save as say CIFS-Renamer.ps1 - in PowerShell, answering the prompts. In the screenshots below, I’m running CIFS-Renamer.ps1 with parameters. The first run has everything green. In the second run, since we have the same data, CIFS-Renamer tells us that the CIFS servers are already correctly named.

Image: CIFS-Renamer run 1
Image: CIFS-Renamer run 2
The Script


#########################
## CIFS SERVER RENAMER ##
#########################

## INPUTS ##
Param(
  [Parameter(Mandatory=$True)]
  [String]$CSV_Name,
  [Parameter(Mandatory=$True)]
  [String]$Cluster_Mgmt_IP,
  [Parameter(Mandatory=$True)]
  [String]$Cluster_User,
  [Parameter(Mandatory=$True)]
  [Security.SecureString]$Cluster_Pass,
  [Parameter(Mandatory=$True)]
  [alias("domain")][String]$Qualified_Domain_Name,
  [Parameter(Mandatory=$True,HelpMessage="user@qualified.domain.name")]
  [String]$Domain_User,
  [Parameter(Mandatory=$True)]
  [Security.SecureString]$Domain_Pass
  # TIP: Can use $P = Read-Host -AsSecureString, as value for pass!
)

## SAVING USER AND PASSWORDS (can re-use) ##
$Global:CUser = $Cluster_User
$Global:CPass = $Cluster_Pass
$Global:DUser = $Domain_User
$Global:DPass = $Domain_Pass

## CREDENTIALS OBJECTS ##
$CCred = New-Object System.Management.Automation.PsCredential($Cluster_User,$Cluster_Pass)
$DCred = New-Object System.Management.Automation.PsCredential($Domain_User,$Domain_Pass)

## TITLE AND GENERIC DISPLAY FUNCTION ##
Function Wr{Param([String]$P,[String]$I = "WHITE");Write-Host $P -ForegroundColor $I}
Wr;Wr ">>>>> CIFS SERVER RENAMER <<<<<" MAGENTA;Wr

## CSV CHECK ##
If(Test-Path $CSV_Name){
  $CSV = Import-CSV $CSV_Name
  Wr "Imported the CSV $CSV_Name" GREEN;Wr
}else{
  Wr "Failed path test to $CSV_Name" RED;EXIT;PAUSE
}

## ONTAP PSTK CHECK ##
[Void](Import-Module DataONTAP)
If(Get-Module DataONTAP){
  Wr "Import-Module DataONTAP successful" GREEN;Wr
}else{
  Wr "Import-Module DataONTAP failed" RED;EXIT;PAUSE
}

## CONNECT TO CLUSTER ##
If(Connect-NcController -Name $Cluster_Mgmt_IP -Credential $CCred){
  Wr "Connected to $Cluster_Mgmt_IP" GREEN;Wr
}else{
  Wr "Failed to connect to $Cluster_Mgmt_IP" RED;EXIT;PAUSE
}

## SETUP TEMPLATES FOR ATTRIBUTES FOR QUICKER GETS ##
$VsAttrs = Get-NcVserver -Template
$VsAttrs.AllowedProtocols = ""
$CfAttrs = Get-NcCifsServer -Template
$CfAttrs.AdministrativeStatus = ""
$CfAttrs.CifsServer = ""
$CfAttrs.Domain = ""

## MAIN FUNCTION: CIFS-RENAME ##
Function CIFS-RENAME{
 
  ## CHECK FOR VSERVER ##
  $GetNcVserver = Get-NcVserver -Attributes $VsAttrs -Name $VserverName
  If(!$GetNcVserver){
    Wr "Vserver $VserverName does not exist" RED;RETURN
  }
  If($GetNcVserver.AllowedProtocols -notcontains "cifs"){
    Wr "Vserver $VserverName does not have cifs allowed" RED;RETURN
  }
  Wr "Vserver $VserverName check OK" GREEN
 
  ## CHECK FOR CIFS SERVER, STATUS, AND DOWN ##
  $GetNcCifsServer = Get-NcCifsServer -Attributes $CfAttrs -VserverContext $VserverName
  If(!$GetNcCifsServer){
    Wr "Vserver $VserverName has no CIFS server" RED;RETURN
  }
  If(($GetNcCifsServer.CifsServer -eq $CIFSserver) -and ($GetNcCifsServer.Domain -eq $Qualified_Domain_Name)){
    Wr "Vserver $VserverName's CIFS server is already called $CIFSserver in $Qualified_Domain_Name" RED;RETURN
  }
  If($GetNcCifsServer.AdministrativeStatus -eq "down"){
    Wr "Vserver $VserverName's CIFS server is already down" YELLOW  
  }else{
    [Void](Set-NcCifsServer -VserverContext $VserverName -AdministrativeStatus down -Domain $Qualified_Domain_Name -AdminCredential $DCred)
    $GetNcCifsServer = Get-NcCifsServer -Attributes $CfAttrs -VserverContext $VserverName
    If($GetNcCifsServer.AdministrativeStatus -ne "down"){
      Wr "Vserver $VserverName unable to down CIFS server" RED;RETURN
    }        
  }
  Wr "Vserver $VserverName's CIFS Server is down" GREEN
 
  ## CIFS SERVER RENAME ##
  [Void](Set-NcCifsServer -VserverContext $_.Vserver -CifsServer $_.NewName -Domain $Qualified_Domain_Name -AdminCredential $DCred -Force)
 
  ## RENAME SUCCESS CHECKING ##
  $GetNcCifsServer = Get-NcCifsServer -Attributes $CfAttrs -VserverContext $VserverName
  If($GetNcCifsServer.AdministrativeStatus -eq "up"){
    Wr "Vserver $VserverName's CIFS server up" GREEN
  }else{
    Wr "Vserver $VserverName's CIFS server is not up" RED
  }
  If(($GetNcCifsServer.CifsServer -eq $CIFSServer) -and ($GetNcCifsServer.Domain -eq $Qualified_Domain_Name)){
    Wr "Vserver $VserverName's CIFS server has been renamed $CIFSServer in domain $Qualified_Domain_Name" GREEN 
  }else{
    Wr "Vserver $VserverName's CIFS server failed to rename correctly" RED
  }
}

## MAIN PROGRAM ##
$CSV | Foreach{
  [String]$VserverName = $_.Vserver
  [String]$CIFSserver = $_.NewName
  Wr ("Start: " + [String](date -uformat %T)) CYAN
  CIFS-RENAME
  Wr ("Finir: " + [String](date -uformat %T)) CYAN;Wr
}


Comments