Run Profile Script Builder

The Following script can be used to build a basic Run Profile Script around your current environment. Included in run profile script that is built for you is a method for a single run profile script to run Delta Import sync during the week and the first run every Saturday would be a Full Sync Cycle and a delta every sync after.

There are 2 Files required to build your Basic Run Profile Script.

1. The MAScriptCreator.ps1 file which does a WMI call to gather the Management Agents which are defined in the WMI Namespace. The script than builds the script using the MAScript.txt file.

2. The MAScript.txt file is use as a base file for the MAScriptCreator.ps1 file.

Both files can be found in the attached ZIP File

MAScript.ps1

function Get-ScriptDirectory
{
 $Invocation = (Get-Variable MyInvocation -Scope 1).Value
 Split-Path $Invocation.MyCommand.Path
}
$dir = Get-ScriptDirectory
$input = [System.IO.File]::ReadAllText("$dir\MAScript.txt")

##$names = get-wmiObject where -query = "Select  * from MIIS_ManagementAgent" -Namespace "root/microsoftidentityintegrationserver" | select Name
$names =get-wmiObject -query "Select  * from MIIS_ManagementAgent" -Namespace "root/microsoftidentityintegrationserver" | select Name
$sb = New-Object System.Text.StringBuilder
foreach($name in $names)
{
 $sb.Append('"')
 $sb.Append($name.Name)
 $sb.Append('"')
 $sb.Append(', ')
}

$sb.Length = $sb.Length - 2

$input = $input.Replace("[MANAMES]", $sb.ToString())
echo $input > "MyNewMAScript.ps1"

 

MAScript.txt

############
# PARAMETERS
############

$params_ComputerName = "."          # "." is the current computer
$params_delayBetweenExecs = 5       #delay between each execution, in seconds
$params_numOfExecs = 1              #Number of executions 0 for infinite
$params_keepRunHistoryDays = 7   #Number of days to keep Run History
$params_runProfilesOrder =

$MAS = @([MANAMES])
$FullCycleprofilesToRun = @("FI", "FS", "EX", "DI")
$DeltaCycleprofilesToRun = @("DI", "DS", "EX", "DI")

############
# FUNCTIONS
############

$line = "-----------------------------"

function Write-Output-Banner([string]$msg)
{
 Write-Output $line,("- "+$msg),$line
}

function Clear-Run-History
{
 #--------------------------------------------------------------------------------------------------------------------
 Clear-Host
 $DeleteDay = Get-Date
 $DayDiff = New-Object System.TimeSpan $params_keepRunHistoryDays, 0, 0, 0, 0
 $DeleteDay = $DeleteDay.Subtract($DayDiff)
  
 Write-Host ""
 Write-Host "Deleting run history earlier than or equal to:" $DeleteDay.toString('MM/dd/yyyy')
 $lstSrv = @(get-wmiobject -class "MIIS_SERVER" -namespace "root\MicrosoftIdentityIntegrationServer" -computer ".")
 Write-Host "Result: " $lstSrv[0].ClearRuns($DeleteDay.toString('yyyy-MM-dd')).ReturnValue
 Write-Host ""
 #--------------------------------------------------------------------------------------------------------------------
 Trap
 {
   Write-Host "`nError: $($_.Exception.Message)`n" -foregroundcolor white -backgroundcolor darkred
   Exit
 }
 #--------------------------------------------------------------------------------------------------------------------
}

function Get-IsSomebodyRunning
{
    foreach($maName in $MAS)
    {
        $MA = $maObjects | ? {$_.Name -eq $maName}
        $myMas += $MA
        if($MA -ne $null)
        {
            $MARunStatus = $MA.RunStatus()
            If ($MARunStatus.ReturnValue -eq "in-progress")
            {
                Write-Host "$MA.Name is running"
                return $true
            }
         }  
    }
}

############
# DATA
############
$numOfExecDone = 0
############
# PROGRAM
############
$maObjects = get-wmiObject -query "Select  * from MIIS_ManagementAgent" -Namespace "root/microsoftidentityintegrationserver"
$myMas = @()

while(Get-IsSomebodyRunning -eq $true)
{
    Start-Sleep 10
}

Clear-Run-History
do
{
 Write-Output-Banner("Execution #:"+(++$numOfExecDone))
 foreach($MA in $myMas)
 {              
  if($MA -ne $null)
  {
   Write-Output-Banner("MA: "+$maName)
            $date = [DateTime]::Now

            $file = Get-ChildItem "lastRun.txt" -EA SilentlyContinue
            if($file -ne $null)
            {
                $lastRun = [DateTime]::Parse([System.IO.File]::ReadAllText("lastRun.txt"))
            }

            $needsFullImort = $file -eq $null -or $lastRun.Date -lt [DateTime]::Now.AddDays(-6)

            if($date.DayOfWeek -eq "Saturday" -and $needsFullImort)
            {
                $profilesToRun = $FullCycleprofilesToRun
                $dateString = [DateTime]::Now.Date.ToString()
                echo $dateString > "lastRun.txt"
            }
            else
            {
                $profilesToRun = $DeltaCycleprofilesToRun
            }

            $maType = $MA.Type
            #Do something with this

   foreach($profileName in $profilesToRun)
   {
    Write-Output (" "+$profileName),"  -> starting"
    $datetimeBefore = Get-Date;
    $result = $MA.Execute($profileName);
    $datetimeAfter = Get-Date;
    $duration = $datetimeAfter - $datetimeBefore;
    if("success".Equals($result.ReturnValue))
    {
     $msg = "done. Duration: "+$duration.Hours+":"+$duration.Minutes+":"+$duration.Seconds
    } else
    {
     $msg = "Error: "+$result
    }  
    Write-Output ("  -> $msg")
   }
  }
  else
  {
   Write-Output ("Not found MA type :"+$maName);
  }
 }
  
 $continue = ($params_numOfExecs -EQ 0) -OR ($numOfExecDone -lt $params_numOfExecs)

 if($continue)
 {
  Write-Output-Banner("Sleeping "+$params_delayBetweenExecs+" seconds")
  Start-Sleep -s $params_delayBetweenExecs
 }

}
while($continue)

  

## https://blogs.msdn.com/connector_space ##

RunProfiler.zip