Functions for Name-Mapping Creates, UNIX Users, and UNIX Groups

FUNCTION Create-UnixGroup {
  Param([String]$SVMNAME,[String]$GROUPNAME,[String]$GROUPID)
  [Void](New-NcNameMappingUnixGroup -Name "$GROUPNAME" -GroupId "$GROUPID" -VserverContext "$SVMNAME")
  $q = Get-NcNameMappingUnixGroup -Template
  $q.GroupName = $GROUPNAME
  $q.GroupId = $GROUPID
  $q.Vserver = $SVMNAME
  IF(Get-NcNameMappingUnixGroup -Query $q){ RETURN $TRUE }
  ELSE{ RETURN $FALSE }   
}
FUNCTION Check-CreateUnixGroup{
  Param([String]$SVMNAME,[String]$GROUPNAME,[String]$GROUPID,[String]$LOGFILE)
  [Boolean]$Check = Create-UnixGroup "$SVMNAME" "$GROUPNAME" "$GROUPID"
  IF($Check){ Wr ("Created Unix Group $GROUPNAME with ID $GROUPID") GREEN; Wr }
  ELSE{ Wr ("FAILED to create Unix Group $GROUPNAME with ID $GROUPID") RED -LogFile $LOGFILE; Wr }
}

FUNCTION Create-UnixUser {
  Param([String]$NAME,[String]$USERID,[String]$GROUPID,[String]$FULLNAME,[String]$SVMNAME)
  [Void](New-NcNameMappingUnixUser -Name "$NAME" -UserId "$USERID" -GroupId "$GROUPID" -FullName "$FULLNAME" -VserverContext "$SVMname")
  $q = Get-NcNameMappingUnixUser -Template
  $q.UserName = $NAME
  $q.UserId  = $USERID
  $q.GroupId = $GROUPID
  $q.Vserver = $SVMNAME
  IF(Get-NcNameMappingUnixUser -Query $q){ RETURN $TRUE }
  ELSE{ RETURN $FALSE }   
}
FUNCTION Check-CreateUnixUser {
  Param([String]$NAME,[String]$USERID,[String]$GROUPID,[String]$FULLNAME,[String]$SVMNAME,[String]$LOGFILE)
  [Boolean]$Check = Create-UnixUser "$NAME" "$USERID" "$GROUPID" "$FULLNAME" "$SVMNAME"
  IF($Check){ Wr ("Created Unix User $NAME with ID $USERID in group $GROUPID") GREEN; Wr}
  ELSE{ Wr ("FAILED to create Unix User $NAME with ID $USERID in group $GROUPID") RED -LogFile $LOGFILE; Wr}
}

FUNCTION Create-NameMapping {
  Param([String]$DIRECTION,[String]$POSITION,[String]$PATTERN,[String]$REPLACEMENT,[String]$SVMNAME)
  [Void](New-NcNameMapping -Direction $DIRECTION -Position $POSITION -Pattern $PATTERN -Replacement $REPLACEMENT -VserverContext $SVMNAME)
  $q = Get-NcNameMapping -Template
  $q.Direction   = $DIRECTION # e.g win_unix/unix_win
  $q.Position    = $POSITION
  $q.Pattern     = $PATTERN
  $q.Replacement = $REPLACEMENT
  $q.Vserver     = $SVMNAME
  IF(Get-NcNameMapping -Query $q){ RETURN $TRUE }
  ELSE{ RETURN $FALSE }
}
FUNCTION Check-CreateNameMapping{
  Param([String]$DIRECTION,[String]$POSITION,[String]$PATTERN,[String]$REPLACEMENT,[String]$SVMNAME,[String]$LOGFILE)
  [Boolean]$Check = Create-NameMapping "$DIRECTION" "$POSITION" "$PATTERN" "$REPLACEMENT" "$SVMNAME"
  IF($Check){ Wr ("Created Name Map $DIRECTION : $PATTERN -> $REPLACEMENT") GREEN; Wr }
  ELSE{ Wr ("FAILED to create Name Map $DIRECTION : $PATTERN -> $REPLACEMENT") RED -LogFile $LOGFILE; Wr }
}

FUNCTION Wr {
  Param([String]$ToDisplay,[String]$Color = "WHITE",[String]$LogFile)
  If($ToDisplay){ Write-Host $ToDisplay -ForegroundColor $Color -NoNewLine } else { Write-Host }
  If($LogFile){ $ToDisplay >> $LogFile }
}


Comments