I had an email about trying to use my Morning Report script to connect to machines that required alternate credentials. For example, you might have non-domain systems in a DMZ. Fair enough. Since most of the report script uses WMI, it wasn't too hard to add a Credential parameter and modify the WMI code to use it. I tweaked the code a bit to use hashtables to splat parameters.
ManageEngine ADManager Plus - Download Free Trial
Exclusive offer on ADManager Plus for US and UK regions. Claim now!
#region define a parameter hashtable
$paramhash=@{
Classname="Win32_OperatingSystem"
Computername=$Computername
ErrorAction="Stop"
}
if ($credential) {
$paramhash.Add("Credential",$Credential)
}
If ($OK) {
Try {
#get Operating system information from WMI
$os = Get-WmiObject @paramhash
...
I'm a little mixed on using splatting in the script. On one hand it makes it easier to wrap up parameters but the actual command might be a little confusing. Hopefully the comments make it clear.
So that handled all the WMI parts. The event log section is using Get-Eventlog which doesn't have a -Credential parameter. I could have tried to rewrite the section using WMI, but that seemed like a lot of work. So I made the assumption that the computers you are querying are running PowerShell 2 or later with remoting enabled. That means I can use Invoke-Command to run Get-Eventlog ON the remote computer. As an additional benefit this seems to run a little faster, at least in my testing.
The tricky part was passing all the parameter values to Get-EventLog and Invoke-Command. I ended up with some complicated nesting but it works.
#Event log errors and warnings in the last $Hours hours
$last=(Get-Date).AddHours(-$Hours)
#define a hash table of parameters to splat to Get-Eventlog
$GetEventLogParam = @{
LogName="System"
EntryType="Error","Warning"
After=$last}
#System Log
Write-Host "...System Event Log Error/Warning since $last" -ForegroundColor Cyan
#hashtable of optional parameters for Invoke-Command
$InvokeCommandParam = @{
Computername=$Computername
ArgumentList=$GetEventLogParam
ScriptBlock = {Param ($params) Get-EventLog @params }
}
if ($Credential) { $InvokeCommandParam.Add("Credential",$Credential) }
$syslog = Invoke-Command @InvokeCommandParam
$syslogdata = $syslog | Select TimeGenerated,EventID,Source,Message
#Application Log
Write-Host "...Application Event Log Error/Warning since $last" -ForegroundColor Cyan
#update the hashtable
$GetEventLogParam.LogName="Application"
#update invoke-command parameters
$InvokeCommandParam.ArgumentList = $GetEventLogParam
$applog = Invoke-Command @InvokeCommandParam
$applogdata = $applog | Select TimeGenerated,EventID,Source,Message
Now you can run the script using -Credential, specifying either a saved credential object or the user name which will give you the Get-Credential prompt. I also made some slight tweaks to the embedded style and layout.
If you missed the original and related posts, you might want to read:
https://jdhitsolutions.com/blog/2012/01/the-powershell-morning-report/
https://jdhitsolutions.com/blog/2012/02/morning-report-revised/
https://jdhitsolutions.com/blog/2012/08/event-log-morning-report/
Download the latest version of the MorningReport.
thanks a lot! very interesting script. Could you tell me how to insert the ability to send the output of the script as the body of an email message?
I would like to send the report as the message body in html or text format (via send-mailmessage).
Thanks,
Andrea
Look at help for Send-MailMessage. Here’s one way. First create the HTML report. Then send it as the body. The trick is to get the html report as one long string.
PS C:\> send-mailmessage -to $TO -from $FROM -Subject “Morning Report” -body (get-content c:\work\report.htm | out-string) -BodyAsHtml -SmtpServer $SMTP