• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 968
  • Last Modified:

PowerShell No File Output with Task Scheduler

I have a PowerShell script that checks the disk space on various servers in my network and then emails me a result file.  I have a command (.CMD) file to run the script. It works fine if I execute it at the PowerShell command line.  If I run the same .CMD file from within  a task scheduler task I get a blank e-mail.  So the shell script is executing but with no result.  See the code section below for details.  Notice that I am capturing the console output to a file in the command file.  This works fine when executed at the command line.  But the task scheduler does not create that console output file.  Also - The task scheduler is set up to run with domain admin privileges, the same as my login used at the command line.  This issue is driving me batty.  Any help is appreciated.  
Maypo

Added additional error handling code to better determine what is going on.  See revised PS script below.  The email now contains the single line: "Drive list query returned empty with no errors"
########  The .CMD file:
@echo off
Powershell.exe -command "& 'c:\scripts\DiskSpace.PS1'" > DiskSpaceTranscript.log
 
######## And the PowerShell script:
$logfile = "C:\Scripts\DiskSpace.log"
 
## Setup log file e-mail
$smtpServer = "foo.com"
 
$msg = new-object Net.Mail.MailMessage
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
 
$msg.From = "foo@foo.com"
$msg.To.Add("foo@foo.com")
$msg.Subject = "Disk Space Less Than 30GB"
 
New-Item -ItemType file $logfile -Force
 
## List any server that has a disk which free space is less than 30GB
$driveList = get-wmiobject -computer (get-content servers.txt) -query "Select * from win32_logicaldisk where freespace <= 32212254720 and drivetype=3" -errorvariable myerror -erroraction silentlycontinue
if (-not $driveList)
{
	if ($myerror)
	{
		add-content $logfile "Drive list query returned empty with error:" 
		add-content $logfile $myerror[0].categoryinfo
	}
	else
	{
		add-content $logfile "Drive list query returned empty with no errors"
	}
}
else
{
	format-table -inputobject $driveList -groupby SystemName @{label = 'Server'; expression = {$_.SystemName}; width=12}, 
		@{label='Drive'; expression = {$_.DeviceID}; width=7}, 
		@{label = 'FreeSpace'; expression = {"{0:n0}MB" -f ($_.FreeSpace / 1mb)}; width=10} | out-file $logfile
}
 
$msg.Body = [string]::join([environment]::newline, (get-content $logfile))
$smtp.Send($msg)

Open in new window

0
Maypo
Asked:
Maypo
  • 5
  • 5
1 Solution
 
BSonPoshCommented:
First... so you know you can call Powershell directly from Task Manager. Copy you code to .ps1 file and add the task using: Powershell.exe -command <ScriptFile> -noprofile

Second... I suspect your problem is an authentication issue. When you run a schedule task it often runs as system. The system account does not have access to remote machines.
0
 
MaypoAuthor Commented:
As I stated in the original question, the task is set up to run as domain administrator.  Is there some policy I need to set for logins in Domain Administrators group that allows access to remote machines?
0
 
BSonPoshCommented:
One way to make sure is to add this to the top of the Powershell script.

$NTIdentity = ([Security.Principal.WindowsIdentity]::GetCurrent())
$NTIdentity
0
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.

 
BSonPoshCommented:
btw... I do not see mention of the OS you are running the script from.
0
 
MaypoAuthor Commented:
The zone states Windows Server 2008, which is the server I am running this on.
I will try logging the $NTIdentity as you suggest.  If it is indeed using the domain admin login what else could cause this?  IS there a policy setting?
0
 
MaypoAuthor Commented:
So I added the output of the logged in username to the script like so:
$loggedInUser = ([Security.Principal.WindowsIdentity]::GetCurrent())
out-file -filepath $logfile -append -inputobject $loggedInUser.name
and ran it using the task scheduler.  Sure enough the account is my domain administrator account.
I do not understand why this account would not have the privileges necessary to run this script?
Thanks for the help.
0
 
BSonPoshCommented:
I agree... it is odd. can you write the error stream out?

$error | out-file errorlog.txt -enc ascii
0
 
MaypoAuthor Commented:
Bingo!  That single line of how to write out the current error pointed me in the right direction, a million thanks!
The problem was that the call to "get-wmiobject -computer (get-content servers.txt) " was failing because when executing the ps1 script from Task Scheduler the "current directory" was no longer the scripts directory.  Task Scheduler executes out of c:\windows\system32 so it was expecting the servers.txt file to be in that directory.  Doh.
0
 
MaypoAuthor Commented:
Really appreciate the help and the fact you sticked with me through this.
0
 
BSonPoshCommented:
Glad to help
0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

  • 5
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now