开发者

Scheduled powershell script not running dot sourced scripts

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


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
}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜