I just want to apologise firstly that I haven’t been updating my blog for a little while and this could be my first post this month, but I’ve been in hospital for a little while and then on medication which has left me a little…incapacitated!
Today’s post brings in a couple of technologies and techniques together to produce an email that contains a simple table showing the Histories created by all Users during the past week. This is actually a very simplified request that we received a little while ago from a client who was going away on holiday for a couple of weeks but still wanted to keep a tab on how their team were performing.
We’re going to use Microsoft’s PowerShell to run a SQL statement against the Act! SQL instance and then use a SMTP connection to the internal SMTP server (Exchange Server in our client’s case) to them email it out on a schedule set by nothing more exciting than the Windows Scheduler!
Now there are add-ons that will do this in a much more WYSIWYG and point-‘n’-click manner, but my view on this is; where’s the fun in that and isn’t it cheaper for the client to use the native skills of their Act! Consultant and existing free technology already present within their architecture?
Below is the script for doing this, I’ve added some comments to explain some of the parts and have tried to segment the scripts into blocks. In the current state, the script would need to be run on the server hosting the Act! dB and the scheduler to be running logged in as either a local server administrator or as a Domain administrator since it uses that to log into the SQL instance.
One word of caution though, if you’re not too comfortable with PowerShell or SQL, I would recommend that you don’t try this on your own and try to liaise with someone who is more comfortable with the technologies before using it on a production dB.
I’ve noticed that the image is a little small so here is the script itself that you could copy and paste if you’re feeling especially lazy!
clear #clears the screen of any other commands etc. #-----------This is the PS connector to the native SQLCMD of SQL Server if((Get-PSSnapin -Name sqlserverprovidersnapin100 -ErrorAction SilentlyContinue) -eq $null){ add-PSSnapin sqlserverprovidersnapin100 add-PSSnapin sqlservercmdletsnapin100 } #-------------------------------------------------------------------------------------------- $dbInstance = ".\act7" #The machine hosting the Act! dB SQL Instance $actDb = "Act2013Demo" #The Act! dB we want to report on #----------------SQL Statement---------------------------------------------------------------- $myExp = invoke-Sqlcmd -ServerInstance $dbInstance -Database $actDb -Query " SELECT Count(TBL_HISTORY.HISTORYID)[Count], TBL_HISTORYTYPE.NAME AS [History Type], TBL_USER.USERLOGIN AS [User] FROM TBL_HISTORY INNER JOIN TBL_HISTORYTYPE ON TBL_HISTORY.HISTORYTYPEID = TBL_HISTORYTYPE.HISTORYTYPEID INNER JOIN TBL_USER ON TBL_HISTORY.CREATEUSERID = TBL_USER.USERID WHERE TBL_HISTORY.CREATEDATE >= DATEADD(WW,DateDiff(WW,0,GetDate())-1,0) AND TBL_HISTORY.CREATEDATE <= DATEADD(WW,DateDiff(WW,0,GetDate()),0) GROUP BY TBL_HISTORYTYPE.NAME, TBL_USER.USERLOGIN " | Select-Object Count, 'History Type', User #----------------------------------------------------------------------------------------------- #---------------Send out Email------------------------------------------------------------------ $emailFrom = 'actAdmin@caldere.com' $emailTo = 'SalesManager@caldere.com' $subject = "History Activity Report for week: {0}" -f((Get-Date -UFormat %V)-1) Send-MailMessage -SmtpServer 'MyMailServer' -From $emailFrom -To $emailTo -Subject $subject -Body $myExp -BodyAsHtml #------------------------------------------------------------------------------------------------
I’d be genuinely interested to read any comments anyone would like to make on this or if they can find ways to improve this. The original script created for the client was a little more complicated to include CSS styling and further SQL queries specific to their request.