1

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.

3
  • try to play with settings in taskmanager, like "run with highest priviledges". Also try to run it under your user. Commented Jul 7, 2014 at 13:31
  • I tried run it using my domain and it behave the same. <5secs with profile and >1min without. Sorry forgot to mention my AD run on Win 2003 so "highest priviledge" is not an option. Commented Jul 8, 2014 at 9:35
  • I tried comment my script and left the add-pssnapin and it still took 30secs to complete so I suspect this and the Get-QADuser is the script that slow down the task (although it use only <5secs in powershell ISE). Tried googled & found this mentioned something related to the profile but not quite sure it is related.. Commented Jul 8, 2014 at 9:50

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Tried this but the subsequent runs completed with same amount of time.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.