Link to home
Start Free TrialLog in
Avatar of bndit
bndit

asked on

Windows 7 (32bit) Task Scheduler and Powershell script

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

Avatar of prashanthd
prashanthd
Flag of India image

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

Avatar of xylog
xylog

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.
ASKER CERTIFIED SOLUTION
Avatar of bndit
bndit

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bndit

ASKER

here are the screenshots of the task.
correct.png
problem.png
Avatar of bndit

ASKER

Found the solution on my own.