We use cookies to improve your experience.
By your continued use of this site you accept such use.
For more details please see our privacy policy and cookies policy.

Script Repository

Export all scripts from business rules, custom commands and scheduled tasks

February 24, 2021 Views: 1662

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 6
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.

avatar
Napoleon Jun 02, 2020
HI,
if I run this Script I got this Message:
Illegales Zeichen im Pfad. Stapelüberwachung: bei <ScriptBlock>, <Keine Datei>: Zeile 60
Have you any Idea?
avatar
Support Jun 02, 2020

Hello,

Please, make sure that the path specified in the $folderPath variable is valid and corresponds to an existing folder on the computer where Adaxes service is installed. Additionally, make sure that the path does not contain illegal and non-printable characters.

avatar
Douglas May 20, 2021
When I run this as using Adaxes cutom command, i do not get the output of the business rules; I only get custom commands. How do I fix this?
avatar
Support May 21, 2021
Hello Douglas,

As per our check, the script works exactly as intended. Could you, please, send us (support@adaxes.com) the exact script you are using with all the modifications you made? If you face any error messages, please, send us screenshots.
Leave a comment
Loading...

Got questions?

Support Questions & Answers