Archive

Posts Tagged ‘HTML’

Sending Mail from Powershell 2.0 CTP3 with Attachments

March 14th, 2009
Comments Off

I had seen a number of scripts detailing how to send mail from Powershell v2.0, but the limitation that always seemed to appear was that they used SMTP to do so and didnt allow complex mail formation… So I investigated .Net to see if it was possible, low and behold I came accross the following Class….

System.net.mail Namespace

and then more specifically

System.net.mail.mailmessage Class

It was this mail message class I based the following script on

#Collect Events from That are errors and report
$A = Get-date -hour 0 -minute 0 -Second 0
$logfile = $env:Temp,"applog.html" -join ""
$logfile2 = $env:Temp,"syslog.html" -join ""
$SmtpClient = new-object system.net.mail.smtpClient
$SmtpServer = "smtp.domain.com.au"
$SmtpClient.host = $SmtpServer
$log2 = Get-EventLog -LogName System -After $a -EntryType Error
$log = Get-EventLog -LogName Application -After $a -EntryType Error
If (($log.count + $log2.count) -gt 0)
{
$MailMessage = new-object system.net.mail.Mailmessage -Args "system@domain.com.au","user@domain.com.au"
$MailMessage.From = "System Log <ystem@domain.com.au>"
$MailMessage.Subject = "Syslogs for $a"
If ($log.count -gt 0){
$log | convertto-html -Property index, TimeGenerated, InstanceID, Source, Message   -Title "Application Log for $a" > $logfile
$mailmessage.attachments.add($logfile)
}
if ($log2.count -gt 0){
$log2 | convertto-html -Property index, TimeGenerated, InstanceID, Source, Message   -Title "System Log for $a" > $logfile2
$mailmessage.attachments.add($logfile2)
}
$SmtpClient.send($mailmessage)
$mailmessage.finalize
$mailmessage.dispose()
$SmtpClient.finalize
If (Test-Path $logfile){Remove-Item -Force -Path $logfile}
If (Test-Path $logfile2){Remove-Item -Force -Path $logfile2}
}

So thats the entire script…now onto a breakdown

So we start with this

$logfile = $env:Temp,"applog.html" -join ""
$logfile2 = $env:Temp,"syslog.html" -join ""

Here I declare two objects containing two temporary files I intend to fill with information and then attach to an email. $env:temp simply takes the default temp directory path and appends it two the file name with a join. A new cmdlet is planned called new-path which I think will be a better alternative here.

$SmtpClient = new-object system.net.mail.smtpClient
$SmtpServer = "smtp.domain.com.au"
$SmtpClient.host = $SmtpServer

This section declares a new object as a net smtpclient – this object will send my mail a little later.

$log2 = Get-EventLog -LogName System -After $a -EntryType Error
$log = Get-EventLog -LogName Application -After $a -EntryType Error

Here I am getting some info to send my self  – in this case I am asking for any errors in the application and system logs that have occured today. I define a$ previously as

$A = Get-date -hour 0 -minute 0 -Second 0

Basically a time call.  Okay now I need to process my data a little do some checking to ensure I am not processing nothing and then send the mail.

If (($log.count + $log2.count) -gt 0)
{

This just checks that one of the logs actually returned some entries otherwise we are wasting our time and I dont need empty emails!

$MailMessage = new-object system.net.mail.Mailmessage -Args "system@domain.com.au","user@domain.com.au"
$MailMessage.From = "System Log <ystem@domain.com.au>"
$MailMessage.Subject = "Syslogs for $a"

Now this part is the message.   I created a new object based on the Mailmessage class.  You need to specify the from and to address’s as args as the class property of  “to” is readonly and if you tried to declare it like

$mailmessage.to ="user@domain.com"

You would get an error stating the property was readonly.

Now some more error checking to ensure  which log returned values.  Then only attach that result.

If ($log.count -gt 0){
$log | convertto-html -Property index, TimeGenerated, InstanceID, Source, Message   -Title "Application Log for $a" > $logfile
$mailmessage.attachments.add($logfile)
}

Here we see I parse the result to a html file.  It makes for a nice easy read.  Then I call the Add method of the mailmessage.attachment property and add the log file to the message.

The attachments property is actually a collection and you can use a single command to add more that 1 file as long as you delimit each file with a comma.

I repeated the process for log2

$SmtpClient.send($mailmessage)

And then I send the message

Now I need to clean up. This part is really important particularly if the script is executed in a continually running powershell session rather than a 1 off instance.  For two reasons.  The mail message class once you add a file locks it.  Even once the script ends the file will remain locked.  It will only unlock after the powershell instance ends.  Thats too late as you cant remove your temp files and clean up after yourself which is just rude!

So I need to call two methods, firstly I finalize mailmessage, I am not sure if this was actually neccessary but reading the technet docs it seemed the smart way to go.  Then I dispose of the mailmessage object.  This unhooks it and leaves the files that you added open to removal.  I think after going a bit of reading net objects have a habit of doing this and it is probably good policy to always dispose the objects once you are finished if its possble.

So her goes

$mailmessage.finalize
$mailmessage.dispose()
$SmtpClient.finalize
If (Test-Path $logfile){Remove-Item -Force -Path $logfile}
If (Test-Path $logfile2){Remove-Item -Force -Path $logfile2}
}

And we are done.  One last comment

I hate errors, so I test for the existence of each logfile before removing them …just to avoid errors in case I never created them.

The Script above is based on
Windows Powershell 2.o CTP2
.NET Framework 3.5
It may not work onother .NET versions.
Upgrade to the latest version if possible.
Windows PowerShell 2.0 includes several significant features that extend its use, improve its usability, and allow you to control and manage Windows-based environments more easily and comprehensively. Windows PowerShell 2.0 is backward compatible. Cmdlets, providers, snap-ins, scripts, functions, and profiles that were designed for Windows PowerShell 1.0 work in Windows PowerShell 2.0 without changes.

Share/Save/Bookmark

benjiC Script, System Management , , , , , ,

Fine Tuning my Website’s

January 23rd, 2009
Comments Off

So…I started in website building and marketing about 3 years ago.  I wanted a clean site for my business that gave my customers information that was relevant to them and thier pets.

Initially I wrote all the html myself having grand ideas about what I could make of the site…the result?

Failure!.  Raw HTML design is slow,frustrating and incredibly time consuming DONT DO IT!

My first piece of advice use a GUI interface if you are going to build in HTML.  Some software I trialled but due to the fact I was just starting off I could not afford to use or purchase at the time.

My favourite had to be Dreamweaver from Macromedia .. now owned by Adobe. You can still download a trial of the latest edition from the Adobe website.  Purchasing it can be expensive 399.00 US$ standalone or US$1699.00 in the suite, this product is top end, but I find adobe’s support is good.  If you have loose cash, go get it!

So….once my trial expired I had accomplished 2 things – I had a basic website and now no way of updating it!  I went looking for an open source alternative.  Let me make things clear, I love the open source concept.  I use a large number of open source software solutions published under the GNU licensing standards.

Nothing really worked for me as comprehensively as Dreamweaver had….but I did manage to find some handy programs to help out.  The first was Notepad++.  If you arent using this program already your crazy.  It is an upgrade of the microsoft supplied Notepad, I should say revolution.  Firstly you can download it here from sourceforge.  The makers describe it as

Notepad++ is a free (as in “free speech” and also as in “free beer”) source code editor and Notepad replacement that supports several languages. Running in the MS Windows environment, its use is governed by GPL Licence.

Based on a powerful editing component Scintilla, Notepad++ is written in C++ and uses pure Win32 API and STL which ensures a higher execution speed and smaller program size. By optimizing as many routines as possible without losing user friendlyness, Notepad++ is trying to reduce the world carbon dioxide emissions. When using less CPU power, the PC can throttle down and reduce power consumption, resulting in a greener environment.

Get it and install it – you wont regret it…..for editing text files it is so much better than Microsofts Notepad.  The GUI is friendly and it is regularly updated.

The problem is that Notepad++ can’t intuitively display your HTML as you work, and it was also at this point that I realized designing a new page for each content item I wished to add was slow and I was never going to get a big site.   I needed something new I just didnt know what it was.

Enter Joomla…

Content Managment Systems are one of the best inventions the website maintainers could have asked for.  They ease content management to the point where the end user can maintain and update a site easily.  Secondly they come with Templates… Templates mean you can make your site look however you like and they are completely customizable.  Thirdly many of them are free….completely free…you dont need to register, provide credit card details or any of that rubbish.  There are lots of them too. Heres a list of the ones I have come accross.

These are the two I ended up focusing on however this link provides a list of all of them. Be aware some of these are not free…the ones above definitely are, the link that follows should show the free and the not so free.  But before we go further….if money is burning a hole in your pocket my advice? Start with a open source cms and then pay for support from a reputable support provider (most of the Open Source CMS sites clearly show recommended profressional support providers) and donate to the development.  The money is better spent this way.

OpenSourceCMS – My advice would be to sort them but “Votes Cast – Most to least” to get an idea of the most active projects.  Activity or development of the CMS you choose is the most important thing.  You want new features – if the project is dead you want to have the source and be a great programmer or have people working on the source…I prefer the later…I am lazy.

So I had my CMS system – I had the content…but I didnt have artistic talent.  I felt that it wasnt that important I was delivering good content so as long as the site was clear and concise I could move forward.  To that end I modified some existing templates to colours more suitable for my business and away I went.

Next time  I will discuss SEO – what it “was” , is and will never be….

Share/Save/Bookmark

benjiC Website Design , , ,