What's the syntax for loading a PowerShell Library script from a Scheduled Task script?

Hi,

I'm running Active Roles 7.4, the latest version.  I've created a PowerShell Library script which contains a logging function I'd like to share with all the other Scheduled Task scripts I've written.  Currently, the function is copy and pasted into each Scheduled Task Script, which isn't programming best practise.

However, I can't work out how to tell the Scheduled Task scripts how to use the Library Script.  The error which the Scheduled Task gives is:

The term 'myCoolLoggingFunction' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

My Library Script looks like this (I've trimmed it for brevity):

# This function writes a log entry to a SQL table
function myCoolLoggingFunction([string]$source, [int]$severity, [string]$string, [string]$integer, [string]$dt, [string]$detail)
{
    # No single quotes
    $string = $string -replace '''', ''
    # etc....
}

It's more clear that to get this to work from a Policy Script, I'd call UseLibraryScript in the onInit function ... but I can't work out how to tell a Scheduled Task script to include the Library Script which Active Roles has let me create.  Could you post a snippet of your code if you've got this to work?

Thanks,

Tom

Parents
  • Hi all,

    In my testing, I was able to get the library script to work in a scheduled task script. Annoyingly, I did it by accident testing something else. I was testing a library script that included another library script. Turns out that adding the onInit function to the library script allows it to be used in a scheduled task script module.

    There's a catch though. When either that library script is saved or when the scheduled task script is saved, the scheduled task script will execute. That could be dangerous in some situations depending on what your script does. If that's not an issue, then it's a non-issue. There is logged defect for this already, execute on save issue (90727)

    .

    Below is my test library script (Configuration/Script Modules/Libraries/Temp):

    function onInit($Request){}
    
    function testProfile($testWhat){
        $testWhat | Out-File -FilePath C:\Temp\TestProfile.txt
    }

    Below is my scheduled task script:

    function onInit($context)
    {
        $context.UseLibraryScript("Libraries/Temp")
    }
    
    testProfile "This is a test from a scheduled task in Active Roles"
    $EventLog.ReportEvent($Constants.EDS_EVENTLOG_WARNING_TYPE, "Scheduled Task Script Run - Test 2")

  • Hey there,

    Thanks so much for responding.  I copied and pasted from your post into two scripts, putting the Library Script into the same place as yours, and it still didn't work.

    ...until I enabled script debugging.  Opening the Properties of the Scheduled Task Script, ticking the "Enable debugging" check, and hitting Apply in that properties dialog causes the script to execute, and the TestProfile.txt file is written from the Library Script.  Clearing the tick and hitting Apply doesn't cause it to execute, and nor does running it from a Scheduled Task.

    What with the catch you mention, and that I can't get the Library Script referenced correctly from the Scheduled Task, I don't think this this approach is right for me.  The cmdlet approach is almost as visible as a Library Script, and is working nicely, so I'm sticking with that for the time being.

    Tom

Reply
  • Hey there,

    Thanks so much for responding.  I copied and pasted from your post into two scripts, putting the Library Script into the same place as yours, and it still didn't work.

    ...until I enabled script debugging.  Opening the Properties of the Scheduled Task Script, ticking the "Enable debugging" check, and hitting Apply in that properties dialog causes the script to execute, and the TestProfile.txt file is written from the Library Script.  Clearing the tick and hitting Apply doesn't cause it to execute, and nor does running it from a Scheduled Task.

    What with the catch you mention, and that I can't get the Library Script referenced correctly from the Scheduled Task, I don't think this this approach is right for me.  The cmdlet approach is almost as visible as a Library Script, and is working nicely, so I'm sticking with that for the time being.

    Tom

Children
  • # Using edsaScriptText you can invoke-expression with your library functions to load Parameters/functions into the environment like so.
    $DNofYourLibrary = 'CN=TEST Library,CN=Builtin,CN=Script Modules,CN=Configuration'
    iex $(([adsi]"EDMS://$DNofYourLibrary").edsaScriptText[0])

    # Guid Also Works also and persists through Script movement changing the DN Path.
    $guidOfYourLibrary = '<GUID=501BD585457F6F4DB0714845E28ADC59>'
    iex $(([adsi]"EDMS://$guidOfYourLibrary").edsaScriptText[0])

    # Loading Libraries in Oninit function can be "Sticky" as the library location isn't always updated along with its contents. You can test this with any Oninit library execution, simply move the libary script to a new location and the script will continue to function.. Its not until the edsaScriptText is updated that the script will fail as it reloads the content of the script. I'm not sure how the backend is coded, but I've encountered this numerous times as anything in the OnInit Section of the script seems to be "Sticky" including Parameters.