IndwesAdmin
asked on
Another broken Powershell script
Since the last broken script issue was resolved so quickly I am going to throw this one out there.
I have another script that used to work as a scheduled task but now only works when I run it from the ISE console.
Below is the code for creating the file to be saved and also emailed to me as an attachment.
-----
$html_file = New-Item -ItemType File -Path "$html_file_dir\SharePoint .html" -Force
# Create the file
$html_file
-----
The following is the code for sending the attachment.
-----
# Email the file
$mail = New-Object System.Net.Mail.MailMessag e
$att = new-object Net.Mail.Attachment($html_ file)
$mail.From = $from_address
$mail.To.Add($to_address)
$mail.Subject = "Server Diskspace $date"
$mail.Body = "The diskspace report file is attached."
$mail.Attachments.Add($att )
$smtp = New-Object System.Net.Mail.SmtpClient ($email_ga teway)
#
$smtp.Send($mail)
-----
As stated ... when I run the script from the ISE console it works flawlessly and both created the file in the directory specified and sends it to me as an Email attachment. However when I run it via powershell directly it does not create the file and sends me an Email message that states the file is attached but in fact it is not.
This is another of my scripts that worked as a scheduled task for a long time and then stopped working unless I run it from ISE.
Thanks in advance for any insights.
I have another script that used to work as a scheduled task but now only works when I run it from the ISE console.
Below is the code for creating the file to be saved and also emailed to me as an attachment.
-----
$html_file = New-Item -ItemType File -Path "$html_file_dir\SharePoint
# Create the file
$html_file
-----
The following is the code for sending the attachment.
-----
# Email the file
$mail = New-Object System.Net.Mail.MailMessag
$att = new-object Net.Mail.Attachment($html_
$mail.From = $from_address
$mail.To.Add($to_address)
$mail.Subject = "Server Diskspace $date"
$mail.Body = "The diskspace report file is attached."
$mail.Attachments.Add($att
$smtp = New-Object System.Net.Mail.SmtpClient
#
$smtp.Send($mail)
-----
As stated ... when I run the script from the ISE console it works flawlessly and both created the file in the directory specified and sends it to me as an Email attachment. However when I run it via powershell directly it does not create the file and sends me an Email message that states the file is attached but in fact it is not.
This is another of my scripts that worked as a scheduled task for a long time and then stopped working unless I run it from ISE.
Thanks in advance for any insights.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I can't see $html_file_dir being defined anywhere?!
And you are filling the html file with some content with code not shown here, I assume?
And you are filling the html file with some content with code not shown here, I assume?
ASKER
Qlemo: Yes, I am defining the $html_file_dir variable and filling the file with content. The peculiar thing is that it works fine when run from ISE, writes the file to the defined $html_file_dir directory as well as attaches the file to an Email, but when run using PowerShell directly it doesn't write or attach the file.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Name Value
The file and mail variables are shown below. I checked and the opening and closing brackets match at 24 each.
---- -----
att System.Net.Mail.Attachment
email_gateway smtp.indwes.edu
Error {System.Management.Automat ion.Incomp leteParseE xception: Missing closing '}' in statem...
ExecutionContext System.Management.Automati on.EngineI ntrinsics
Host System.Management.Automati on.Interna l.Host.Int ernalHost
html_file R:\Pages\ServerDiskSpace\S harePoint\ SharePoint .html
html_file_dir R:\Pages\ServerDiskSpace\S harePoint
mail System.Net.Mail.MailMessag e
smtp System.Net.Mail.SmtpClient
The file and mail variables are shown below. I checked and the opening and closing brackets match at 24 each.
---- -----
att System.Net.Mail.Attachment
email_gateway smtp.indwes.edu
Error {System.Management.Automat
ExecutionContext System.Management.Automati
Host System.Management.Automati
html_file R:\Pages\ServerDiskSpace\S
html_file_dir R:\Pages\ServerDiskSpace\S
mail System.Net.Mail.MailMessag
smtp System.Net.Mail.SmtpClient
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I have the script working as a scheduled task now. After incorporating many of the improvements suggested I was able to create a .cmd file that worked using powershell directly and run that successfully as a scheduled task. Thank you very much for all the input.
ASKER
I think I actually learned several things that will come in very useful for future scripting ... thanks again.
ASKER
Removed the line and while it apparently was unneeded the script still runs fine in ISE but does not attach or save the file to the designated directory when run via powershell as a scheduled task. On the bright side, I got rid of an unnecessary piece of code.