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.
LVL 9
samiam41Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dustin SaundersDirector 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)?
0
PberSolutions 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.
1
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.
0
Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

PberSolutions ArchitectCommented:
Are per Dustin's comment, does your logging within the script shed any light to what is happening?
0
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
0
PberSolutions ArchitectCommented:
what is your command that you running.  it should just be powershell.exe
and

 -file yourscript.ps1 in your arguments
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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?
0
samiam41Author Commented:
Phenomenal work experts!!  It's running like a champ now!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.