Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Windows 7 (32bit) Task Scheduler and Powershell script

Posted on 2011-03-17
5
Medium Priority
?
1,153 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

While working, an annoying popup showing below will come and we cannot cancel or close it form the screen. The error message will come again and again.
The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
In this Micro Tutorial viewers will learn how to use Boot Corrector from Paragon Rescue Kit Free to identify and fix the boot problems of Windows 7/8/2012R2 etc. As an example is used Windows 2012R2 which lost its active partition flag (often happen…
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…

730 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