nlks
nlks

Reputation: 69

Weird behavior on my powershell script and schedule task

Have a question on some weird behavior running powershell on my AD server.
I created a powershell script to retrieve inactive users info from AD, not sure whether it will be related but I include it below:

$searchRoot = @("mydomain.com/TEst/test1","mydomain.com/TEst/test2")
$searchRoot += @("mydomain.com/TEst/test3")
Add-PSSnapin Quest.ActiveRoles.ADManagement<br>
$inactiveUsers = @()
Foreach ($ou in $searchRoot) {
$inactiveUsers += @(Get-QADUser -SearchRoot $ou -Enabled | Select-Object Name,SamAccountName,LastLogonTimeStamp,Description,passwordneverexpires,canonicalName)}

$body = $inactiveUsers
$body | out-file -filepath C:\DisableInactive\disabletest.txt -append

I scheduled this script to run with another ID (not the 1 I logged in), the script should take less than 5 seconds to run, but the schedule task took more than a minute to complete (as below):

"DisableInactiveUsers.job" (powershell.exe)
Started 7/7/2014 2:26:00 PM
"DisableInactiveUsers.job" (powershell.exe)
Finished 7/7/2014 2:27:39 PM
Result: The task completed with an exit code of (0).

However when I logoff and login again with that schedule ID, the schedule completed in 4 secs.

"DisableInactiveUsers.job" (powershell.exe)
Started 7/7/2014 7:02:41 PM
"DisableInactiveUsers.job" (powershell.exe)
Finished 7/7/2014 7:02:45 PM
Result: The task completed with an exit code of (0).

I then login with my own ID and right click to run the schedule again, it completed within 5 secs.
Suspecting something to do with the user profile, I then delete the profile from the System Properties and run the job again, guess what, the scheduled job used more than 1 minutes to complete again.
The script and scheduled task have not been changed during all these logoff and in

I am not sure whether it is related to my script but I really appreciate if someone can point out what's wrong with my script/why the scheduled task behave like this.
Thank you.

Upvotes: 1

Views: 620

Answers (1)

FrozenSoul
FrozenSoul

Reputation: 131

try this, delete the profile. Let the task run multiple times automatically. If the first time takes much longer, and subsequent runs are quick. Then its probably due to having to create the profile on the local machine (& group policy processing, etc.) on the initial logon.

sorry for giving an answer when it should probably be a comment, I don't have enough rep to comment yet.

Upvotes: 2

Related Questions