Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Windows 7 (32bit) Task Scheduler and Powershell script

Posted on 2011-03-17
5
Medium Priority
?
1,161 Views
Last Modified: 2012-05-11
Ok, so I'm stumped. I'm having trouble getting a .ps1 script to run correctly from within a Task Scheduler Task in Windows 7 Pro (32). I have two .ps1; one script uses WMI to query for disk space and outputs the results to an HTML file, and the second basically creates, attaches the HTML, and sends the email. Simple, right? The part that gets me is that I'm not using cmdlets that require specific modules or snapins such as doing an Exchange 2007 function where you'd have to add the Exchange 2007 snapin to the script. I can run the each .ps1 file individually without issues; I can run the first .ps1 file which then calls the second .ps1 without a problem, and I can even create a batch and run it from CMD without issues. The result is the same; the script runs, queries, saves the data in a HTML file, and then I get an email WITH the attachment. On the task scheduler side I've tried setting different accounts with various levels to rule out a permission problem (even though the script uses the correct account to do the query), but still no luck. Here's what happens when I run the task; the task completes SUCCESSFULLY and I get the email...BUT WITHOUT the attachment.

What am I missing? Again, everything works fine if I run the scripts from outside task scheduler...when the task runs, it works..but the attachment is MISSING...what gives?!
# Create, add attachment, and send email

$strToday = Get-Date -format "yyyyMMdd"
$strLog = "C:\"

# Email components
$strFromAddress = "** ROUTINE CHECK ** <routinecheck@donotreply.local>"
$strToAddress = "administrator@domain.local"
$strMessageSubject = "SOME REPORT"
$strMessageBody = "SOMETHING DONE ON SERVER1 AND SERVER2"
$strSendingServer = "myrelayhostname.domain.local"

# Email objects
$objSMTPMessage = New-Object System.Net.Mail.MailMessage $strFromAddress, $strToAddress, $strMessageSubject, $strMessageBody
$objAttachment = New-Object Net.Mail.Attachment($strLog + $strToday + "-report.html")
$objSMTPMessage.Attachments.Add($objAttachment)
$objSMTPClient = New-Object System.Net.Mail.SMTPClient $strSendingServer
$objSMTPClient.Send($objSMTPMessage)

Open in new window

# Generate HTML file and save it on the computer

$servers = 'SERVER1','SERVER2'
$password = Get-Content C:\password.txt | ConvertTo-SecureString
$credential = New-Object System.Management.Automation.PSCredential "domain\account",$password
$strToday = Get-Date -format "yyyyMMdd" 
$strLog = "C:\Output\"+$strToday+"-report.html"

# WMI query 
Get-WMIObject Win32_LogicalDisk -Filter "DriveType=3" -Credential $credential -ComputerName $servers | `
Select-Object `
	SystemName,`
	DeviceID,`
	VolumeName,`
	@{Name="Size(GB)";Expression={[decimal]("{0:N1}" -f($_.size/1gb))}},`
	@{Name="Free Space(GB)";Expression={[decimal]("{0:N1}" -f($_.freespace/1gb))}},`
	@{Name="Free Space(%)";Expression={"{0:P2}" -f(($_.freespace/1gb) / ($_.size/1gb))}} | `
ConvertTo-Html -Title "Free Space Report - Exchange Servers" | `
Out-File -FilePath $strLog

# Call "send_email.ps1"
.{.\send_email.ps1}

Open in new window

0
Comment
Question by:bndit
  • 3
5 Comments
 
LVL 12

Expert Comment

by:prashanthd
ID: 35163181
Combined both scripts,please try the following

regards
Prashanth
$servers = 'server1','server2'
$strToday = Get-Date -format "yyyyMMdd" 
$strLog = "C:\output\"+$strToday+"-report.html"

# WMI query 
Get-WMIObject Win32_LogicalDisk -Filter "DriveType=3" -ComputerName $servers | `
Select-Object `
	SystemName,`
	DeviceID,`
	VolumeName,`
	@{Name="Size(GB)";Expression={[decimal]("{0:N1}" -f($_.size/1gb))}},`
	@{Name="Free Space(GB)";Expression={[decimal]("{0:N1}" -f($_.freespace/1gb))}},`
	@{Name="Free Space(%)";Expression={"{0:P2}" -f(($_.freespace/1gb) / ($_.size/1gb))}} | `
ConvertTo-Html -Title "Free Space Report - Exchange Servers" | `
Out-File -FilePath $strLog

# Email components
$strFromAddress = "** ROUTINE CHECK ** <routinecheck@donotreply.local>"
$strToAddress = "administrator@domain.local"
$strMessageSubject = "SOME REPORT"
$strMessageBody = "SOMETHING DONE ON SERVER1 AND SERVER2"
$strSendingServer = "myrelayhostname.domain.local"

# Email objects
$objSMTPMessage = New-Object System.Net.Mail.MailMessage $strFromAddress, $strToAddress, $strMessageSubject, $strMessageBody
$objAttachment = New-Object Net.Mail.Attachment($strLog)
$objSMTPMessage.Attachments.Add($objAttachment)
$objSMTPClient = New-Object System.Net.Mail.SMTPClient $strSendingServer
$objSMTPClient.Send($objSMTPMessage)

Open in new window

0
 
LVL 5

Expert Comment

by:xylog
ID: 35183726
I see a logic error that could explain this. In the first script:

$strLog = "C:\"
$objAttachment = New-Object Net.Mail.Attachment($strLog + $strToday + "-report.html")

In the second script:

$strLog = "C:\Output\"+$strToday+"-report.html"

So you output the file to "C:\"+ $strToday + "-report.html" but you look for the attachment  "C:\Output\"+$strToday+"-report.html". Not gunna work.
0
 
LVL 2

Accepted Solution

by:
bndit earned 0 total points
ID: 35203419
@prashanthd & xylog

Thanks so much for your replies to my query. Like I'd mentioned in my question, both of the scripts worked individually and outside task scheduler without problems and without making changes to the code. Thus, no need to combine and the typo xylog caught was due to my housekeeping when I posted the code on this question; which was irrelevant if both scripts were working fine. The answer to my problem was in task scheduler all along (kinda had the feeling since that's when the problem first appeared). In the task, I had checked the "Do not store password. The task will only have access to local computer resources." option. I unchecked this option and ran the task; I got the email WITH the attachment.
0
 
LVL 2

Author Comment

by:bndit
ID: 35203426
here are the screenshots of the task.
correct.png
problem.png
0
 
LVL 2

Author Closing Comment

by:bndit
ID: 35230147
Found the solution on my own.
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
This Micro Tutorial will give you a basic overview of Windows Live Photo Gallery and show you various editing filters and touches to photos you can apply. This will be demonstrated using Windows Live Photo Gallery on Windows 7 operating system.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

783 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question