Quantcast
Channel: The Official Scripting Guys Forum! forum
Viewing all articles
Browse latest Browse all 15028

Windows Task Scheduler and PowerShell

$
0
0

Hey guys,

I had a question I was hoping someone could help me with.

Overview:

I have two scripts that I use in an automated process of creating AD accounts. The first connects to a SQL HR database and extracts new hire information and the second script provisions the accounts.

The data extraction script initiates the provisioning script after it has created a file to iterate through from the HR database.

My Windows Task Scheduler initiates the data extraction script only since the provisioning script gets initiated as a sub-script.

Problem:

I found with Windows Task Scheduler jobs that if I don't prevent the data extraction script from closing out (after initating the provisioning script) it will stop the job and close out all of my powershell instances. To fix this, I added a powershell -noexit line to keep the data extraction script open while the provisioning script runs. It passes the process id of the data extraction script powershell instance to the provisioning script so when all of the ad accounts are created I run a stop-process on the data extraction instance and it effectively completes the job in the task scheduler.

This all works fine except when I start the sub script for some reason a 'phantom' powershell instance gets opened and when everything is done that phantom instance stays open. For example, when the data extract script initiates the provisioning script 2 PS instances will open. I have tried the following with no luck:

Using - Start-Job, Start-Process (both opens phantom instance). I tried just calling the provisioning script by listing the file path (e.g. $provisioningScriptPath $passedVariables) but in the middle of the provisioning script powershell times out and closes all of my instances. Now if I have a small set of accounts that need to be created this works but in larger jobs it times out.

Any ideas? Do you guys have any suggestions on how to start a sub-script in a Windows Task Scheduled job? The thing that is weird is if I run the scripts manually from the PS shell it works fine - it only creates the phantom instance when being run from the task scheduler.


Viewing all articles
Browse latest Browse all 15028

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>