Scheduled task running PS script continues to run, doesn't stop

Greetings Experts.  The script I am running as a scheduled task is listed below.  After I created the task on a Windows 2012 R2 server (member server) and manually run it to test it, the task keeps running and running when it should take only 30-60 seconds.  The task is set to run under credentials with domain admin rights.  If I run the script from the PS ISE, the script completes without errors.

Import-Module ActiveDirectory

$logPath = "C:\Tools\Scripts\Stale_Accounts\Logs\Stale_PCs_"  #directory to log output
$Date = Get-Date
$TodaysLog = $logPath + $Date.ToString('yyyy_MM-dd_HHmm') + ".log"
$FilterDate = $Date.AddDays(-49)

Function WriteLog($message) {
    $message = (Get-Date).ToShortTimeString() + " - " + $message
    Add-Content -Path $TodaysLog -Value $message

$DisabledOU = "OU=Computers,OU=Inactive,DC=com"

$StaleComputers = Get-ADComputer -SearchBase "OU=Depts,DC=com" -Filter * -Properties DistinguishedName, SAMAccountName, lastLogonTimeStamp |
	Where-Object {[DateTime]::FromFileTime($_.lastLogonTimeStamp) -lt $FilterDate}

If ($StaleComputers) {
	ForEach ($Computer in $StaleComputers) {
		Try {
			Disable-ADAccount -Identity $Computer.DistinguishedName -ErrorAction Stop
			WriteLog -Message "Disabled $($Computer.SAMAccountName)"
		} Catch {
			WriteLog -Message "Failed to disable $($Computer.SAMAccountName): $($_.Exception.Message)"
		Try {
			Move-ADObject -Identity $Computer.DistinguishedName -TargetPath $DisabledOU -ErrorAction Stop
			WriteLog -Message "Moved $($Computer.SAMAccountName) to $($DisabledOU)"
		} Catch {
			WriteLog -Message "Failed to move $($Computer.SAMAccountName) to $($DisabledOU): $($_.Exception.Message)"
} Else {
	WriteLog -Message "No stale computer accounts found."

Open in new window

Any help with this would be greatly appreciated.  Thanks Experts.
Who is Participating?
PberConnect With a Mentor Solutions ArchitectCommented:
what is your command that you running.  it should just be powershell.exe

 -file yourscript.ps1 in your arguments
Dustin SaundersConnect With a Mentor Director of OperationsCommented:
What does the task look like?  The action?

What does the log look like?  Does it go through everything expected logging wise (so it looks like it gets to the end)?
PberConnect With a Mentor Solutions ArchitectCommented:
Not related to your issue, but don't run this script with domain admin rights.  That is just bad practice.  Someone could modify the script to something much more malicious.  You should create a "Service Account" user that has been delegated the rights to move objects within OU's to perform this task.
Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

samiam41Author Commented:
Good point Pber!  Much appreciated.  I wanted to test this with my account before moving to a service account. I should have started with the service account.  My bad.
PberSolutions ArchitectCommented:
Are per Dustin's comment, does your logging within the script shed any light to what is happening?
samiam41Author Commented:
@Dustin, the log file should yield results something like this >>  10:15 AM - No stale computer accounts found.  Except that the log file isn't being created because the script isn't running.

The task is configured like this:
Trigger: 2nd Sunday a month
Action: Run the script
Conditions: Normal settings
samiam41Author Commented:
Task Scheduler launched action "C:\Windows\System32\notepad.exe" in instance "{ed9d2a17-b52b-4ef6-9e72-08b29a508e9d}" of task "\Stale Users".

Looks like task scheduler is trying to run the PS script with something other than PS.  How do I configure the task to use PS?
samiam41Author Commented:
Phenomenal work experts!!  It's running like a champ now!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.