0
votes

I have a basic PowerShell (v4) script running on Server 2012. I decided to have Task Scheduler task it. Task scheduler instance is running as domain and machine administrator, Runs whether the user is logged on or not and runs with Highest Privileges. It's configured for Windows Server 2012 R2.

It starts a program of "Powershell" and this is my argument: "-ExecutionPolicy Bypass -file F:\AdMgmt\scripts\newcheckandcopy.ps1"

The problem: It executes some of the script just fine - however, I have an "If/Else" statement that it ignores. I've tried this with two scripts now and the Task scheduler always executes fine, but doesn't handle the If/Else stuff - it just skips over it and runs everything else.

This is the text of the script (it runs perfectly from the powershell console when logged in as the same account that runs the task):

$path = 'Q:\'  
$stats = 0  
$msg = ''  
$days = 1  
$hours = 0  
$mins = 0
$logtime = Get-Date -uFormat "%y%m%d%H%M" 


$files = @(Get-ChildItem -Recurse -Path $path -Include '*.*' | ?{ $_.LastWriteTime -lt (Get-Date).AddDays(-$days).AddHours(-$hours).AddMinutes(-$minutes) -and $_.psIsContainer -eq $false})  


if ($files -ne $null) {  
  $f_names = [System.String]::Join('|',$files)  
  $msg = 'Message: ' + $f_names
  $stats = $files.Count
  Send-MailMessage -to "[email protected]" -from "[email protected]" -subject "FileMaker Files older than 1 Day" -body $msg -smtpserver "smtp-relay.test.com"
} else {  
  $msg = 'Message: 0 files exceed defined age'
  Send-MailMessage -to "[email protected]" -from "[email protected]" -subject "FileMaker Files OK" -body $msg -smtpserver "smtp-relay.test.com"
}  

Copy-Item Q:\* F:\admgmt\ -recurse
Add-Content f:\admgmt\logs\checkandcopy.txt $logtime
Write-Host $msg  
Write-Host "Statistic: $stats" 
1
Is Q:\ as physical drive or mapped drive? You would think it would execute one part of your If Else clause. So you get no email then? I'll assume you are not getting an email which is why you think neither clause is executing. Can you put something else in the if and else like "IF" | Add-Content c:\temp\log.log or "Else" | Add-Content c:\temp\log.log - Matt
Q: is a mapped drive - an SMB share on a non-domain computer. I added an Add-content and got a really interesting result - no matter the age of the file, it always defaults to the "Else" - so if I run the script now in console and the file is older than the script wants, then it generates the correct email. From task scheduler, it ALWAYS logs the else condition...weirdest thing. - Jen Davenport
Would q: not be available then since it Runs whether the user is logged on or not and runs with Highest Privileges. I would imagine if you did Test-Path q:\ | Add-Content c:\temp\log.log would guess it would be false from the Schedule Task - Matt
I'm guessing you hit the nail on the head. The minute I took Q: out of the equation, it worked swimmingly. Going to find another way to copy the files over, then do the rest with this script. Thanks for the second eye!! - Jen Davenport

1 Answers

0
votes

I'm guessing your issue is you are running the script outside of user context. the Q:\ drive would not be available to it. PowerShell supports UNC paths so you might be able to substitute the actual path that Q:\ points to.