Active Roles Scheduled Task Parameters

This is an example  script, which could be turned into a function, that I use to get the task parameters from the ARS scheduled task that calls the script.  When the script is called directly from the scheduled task I can just grab the parameters but when debugging I set the TaskDN to the DN of the scheduled task.

#Region  Retrieve Scheduled Task Parameters
try {
 $Task.DirObj.GetInfo()
$Task.DirObj.GetInfoEx(@("edsaParameters"),0)
$parameters = $Task.DirObj.Get("edsaParameters")
$DN = $Task.DirObj.DN
$DebugPreference = "SilentlyContinue"
}
catch { }
if ( $DN -eq $null ) {
 $Task = Get-QADObject -Identity $TaskDN  -Proxy -IncludedProperties edsaParameters
 if ( $Task -ne $null ) {
  $parameters = $Task.edsaParameters
  $DN = $TaskDN
} # if ( $Task -ne $null ) {
 else { throw "Unable to get TASK DN script cannot continue" }
} # if ( $DN -eq $null ) {
# now get the mandatory parameters
if ( $parameters -ne $null ) {
 foreach ( $parameter in $parameters ) {
  switch ( $($parameter.Substring( 1,$parameter.IndexOf(">")-1)) ) {
   "GroupToManage" {
    $groupToManage = $($parameter.Substring( $parameter.IndexOf(">")+1,$parameter.IndexOf("</")-($parameter.IndexOf(">")+1)))
    break
   } # "GroupToManage" {
   "UserOU" {
    $targetOUs  = $($parameter.Substring( $parameter.IndexOf(">")+1,$parameter.IndexOf("</")-($parameter.IndexOf(">")+1)))
    if ( $targetOUs -ne "" ) {
     $targetOUs = $targetOUs.split(";")
    }
    break
   }
   "emailTo" {
    $emailTo  = $($parameter.Substring( $parameter.IndexOf(">")+1,$parameter.IndexOf("</")-($parameter.IndexOf(">")+1)))
    if ( $emailTo -ne "" ) {
     $emailTo = $emailTo.split(";")
    }
    break
   } #  "emailTo" {
  }  # switch ( $($parameter.Substring( 1,$parameter.IndexOf(">")-1)) ) {
} # foreach ( $parameter in $parameters ) {
}
# now check we got all 3 mandatory parameters
if ( ( $groupToManage -eq "") -or ( $targetOUs.length -eq 0 ) -or ( $emailTo.length -eq 0 ) ) {
 $alertParameters =@{
  "BodyText"    = "Missing one or more of the mandatory parameters, GroupToManage, emailTo or UserOU"
  "subject"     = "Missing one or more of the mandatory parameters, GroupToManage, emailTo or UserOU"
  "exitMessage" = "Missing one or more of the mandatory parameters, GroupToManage, emailTo or UserOU"
}
Send-AlertEmail @alertParameters
} # if ( ( $groupToManage -eq $null) -or ( $targetOUs -eq $null ) ) {

Advertisements