Solved

Windows 7 (32bit) Task Scheduler and Powershell script

Posted on 2011-03-17
5
1,129 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
Comment Utility
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
Comment Utility
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
Comment Utility
@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
Comment Utility
here are the screenshots of the task.
correct.png
problem.png
0
 
LVL 2

Author Closing Comment

by:bndit
Comment Utility
Found the solution on my own.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

In this previous article (https://oddytee.wordpress.com/2016/05/05/provision-new-office-365-user-and-mailbox-from-exchange-hybrid-via-powershell/), we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
Today, still in the boom of Apple, PC's and products, nearly 50% of the computer users use Windows as graphical operating systems. If you are among those users who love windows, but are grappling to keep the system's hard drive optimized, then you s…
In this video, we discuss why the need for additional vertical screen space has become more important in recent years, namely, due to the transition in the marketplace of 4x3 computer screens to 16x9 and 16x10 screens (so-called widescreen format). …
This Micro Tutorial will give you basic overview of the control panel section on Windows 7. It will depth in Network and Internet, Hardware and Sound, etc. This will be demonstrated using Windows 7 operating system.

771 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now