Solved

Windows 7 (32bit) Task Scheduler and Powershell script

Posted on 2011-03-17
5
1,136 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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Never expired checked 2 19
Exporting Windows Firewall rules to other clients 4 11
Locking down a pc/laptop 11 29
add image on html mail 6 19
Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
By default the complete memory dump option is disabled in windows . If we want to enable the complete memory dump for a diagnostic purpose, we have a solution for it. here we are using the registry method to enable this.
This Micro Tutorial will give you a basic overview of Windows DVD Burner through its features and interface. This will be demonstrated using Windows 7 operating system.
The viewer will learn how to successfully download and install the SARDU utility on Windows 7, without downloading adware.

777 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