Script Repository


Export all scripts from Business Rules, Custom Commands and Scheduled Tasks

March 25, 2019
352

The script will export all the scripts from Business Rules, Custom Commands and Scheduled Tasks, and save them to TXT files named same as the scripts themselves. the files can be sent to a predefined e-mail address and then removed.

Parameters:

  • $folderPath - specifies the path to the folder where the scripts will be saved;
  • $sendMail - specifies whether to send the scripts via email in attachments;
  • $removeFiles - specifies whether to remove the TXT files;
  • $to - specifies the recipient e-mail address;
  • $from - specifies the e-mail address from which the message will be sent;
  • $smtpServer - specifies the SMTP Server to use for sending the report;
  • $subject - specifies the notification message subject;
  • $messageBody - specifies the text for the notification message body.
Edit Remove
PowerShell
# A hash table with types of configuration objects and their aliases
$configurationObjectInfos = @{
    "BusinessRules" = "adm-BusinessRule";
    "CustomCommands" = "adm-CustomCommand";
    "ScheduledTasks" = "adm-ScheduledTask";
}

# File settings
$folderPath = "C:\Scripts" # TODO: modify me

# E-mail settings
$sendMail = $True # TODO: modify me
$removeFiles = $True # TODO: modify me
$to = "recipient@domain.com" # TODO: modify me
$subject = "Adaxes scripts" # TODO: modify me
$message = "Adaxes scripts" # TODO: modify me
$from = "noreply@domain.com" # TODO: modify me
$smtpServer = "mail.domain.com" # TODO: modify me


foreach ($alias in $configurationObjectInfos.Keys)
{
    # Bind to the configuration container that contains objects of the current type
    $configurationContainerPath = $Context.GetWellKnownContainerPath($alias)
    $configurationContainer = $Context.BindToObject($configurationContainerPath)

    # Find configuration objects of the current type
    $type = $configurationObjectInfos[$alias]
    $configurationContainer.SearchFilter =  "(objectCategory=$type)"
    $configurationContainer.PageSize = 500
    $configurationContainer.SearchScope = "ADS_SCOPE_SUBTREE"
    $configurationContainer.ReferralChasing = "ADS_CHASE_REFERRALS_NEVER"
    
    try
    {
        $searchResultIterator = $configurationContainer.ExecuteSearch()
        $searchResults = $searchResultIterator.FetchAll()
        
        # Search scripts
        foreach ($searchResult in $searchResults)
        {
            # Bind to the Business Rule, Custom Command or Scheduled Task
            $object = $Context.BindToObject($searchResult.AdsPath)
            
            # Search scripts
            foreach ($actionsAndConditionsSet in $object.ConditionedActions)
            {
                foreach ($action in $actionsAndConditionsSet.Actions)
                {
                    if ($action.Class -ne "adm-RunScriptAction")
                    {
                        continue
                    }
                    
                    $objectAction = $action.GetAction()
                    $script = $objectAction.Script
                    
                    # Save the script to a file
                    $fileName = $objectAction.ScriptDescription
                    $script | Out-File -FilePath "$folderPath\$fileName`.txt"
                }
            }
        }
    }
    finally
    {
        # Release resources
        if ($searchResultIterator){ $searchResultIterator.Dispose() }
    }
}

# Send mail
$files = (Get-ChildItem -Path $folderPath) | %%{$_.FullName}
if ($sendMail)
{
    Send-MailMessage -to $to -From $from -Subject $subject -SmtpServer $smtpServer -Body $message -Attachments $files
}

if ($removeFiles)
{
    $files | %%{Remove-Item -Path $_ -Confirm:$False -Force}
}

Comments ( 2 )
avatar
a_br
May 13, 2019

I'm trying to test this script in our environment. If I run it via PowerShell ISE on the Adaxes server I get a lot of errors (starts with line 24/25, can't call method on null-valued expression). Could you let me know the correct way to implement it?

avatar
Support
May 13, 2019

Hello,

 

The script can be executed only in a Business Rule, Custom Command or Scheduled Task, not in Windows PowerShell. For example, you can create a Custom Command configured for Domain-DNS object type and then execute the command on a domain registered in Adaxes. The domain will not specify the scope of objects affected by the script and will only be used to execute it. The export criteria are specified in the script itself.

Leave a comment