5

I have a main script that is running other scripts and loading the variables from those scripts using dot sourcing. The script runs fine interactively but when I schedule it to run it does not run the scripts I am dot sourcing. Any ideas?

3 Answers 3

7

I had a similar problem: My dot sourced scripts didn't execute when I ran the main script with run as administrator.

It turned out, that the base dir was different, when I ran the script as administrator. Try using the absolute path in your dot sourcing. If it works then, you can work out a better solution, like this:

$subScriptName = "MySubscript.ps1"
$subScriptPath = Join-Path -Path $callingDir -ChildPath $subScriptName

if (Test-Path $subScriptPath)
{
    # use file from local folder
    . $subScriptPath
}
else
{
    # use central file (via PATH-Variable)
    . $subScriptName
}
Sign up to request clarification or add additional context in comments.

Comments

0

Use absolute paths.

Dot sourcing refers to the current directory. This is typically the same directory as your script when you run interactively, but may be another directory when you schedule your script.

Comments

0

One way this can go wrong without it even being particularly obvious is if you think you're getting drive resources in one of your subqueries from a mapped drive but it turns out that it's a UNC instead. The inserted sub functions weren't shelling out errors and it turned out that my own script was failing to pull a resource because of that (with the same strange caveat that it would work just fine if I tried running the .ps1 script with the drive letter assigned to the UNC from command prompt but it would fail without error if I tried running it from Task Scheduler).

This might be a rare case but I'd still rather save who I can the search that I just had to do.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.