Hi All !
There are millions of exampled out there to PowerShell your Service Manager and i dont want to replicate them. So in this Blog Series i will focus on real world examples people have with Service Manager and Provance (2014 Standard or Enterprise).
The first one is about automatic notification of to-be-expired maintenance agreements.
Lets assume you have Provance 2014 and Hardware Maintenance Agreements with a Start and End Date. Those agreements have related Assets linked to it and contracts managers defined.
You want to get notified periodically about contract expiration and want to know which assets are affected. The timespan you want to look into the future is configurable and you have the choice to get a HTML Report as a file or an HTML E-Mail send to the agreement manager.
Below is a picture of the idea which agreements are selected.
The script will collect those agreement find the linked computer assets and generate a report similar to the one below.
Ok, now for the magic behind all this.
1.) The scheduler
As this is programmed in Powershell you can use SMA, SCSM Powershell Workflow or Orchestrator to fire it, i decided to use task scheduler on the workflow server. The reasons for this decision are that task scheduler is available on every Windows Server, its free and simple to configure. If you dont wat to use it, simply use yur favorite powershell integration tool.
2.) The script
As announced here it uses native scsm commandlets and the SCSMPX extension from Kirk Munroe. Use it, extend it as you need it and let me know if you find it useful. I think i have put enough comments into the code so you can understand what each part does. If you have questions let me know.
[codesyntax lang=“powershell“]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
<#.. This script reads Provance Hardware Maintenance agreements, checks if they will expire within the defined duedays timeframe and writes a HTML report file to disk, containing Agreements and their Assets. Optionally each agreement Manager will get notified of "his/her" Agreements by E.Mail ..#> #Parameter Section Param( [int32]$DueDays=350, [string]$ReportFolder="C:\Reports\" ) # Import-Module $scsmlets Import-Module scsmpx #region Definitions # Define Classes (Object and Relationships) $C_HWAggr = Get-SCClass -Name "Provance.Class.HardwareMaintenanceAgreement" $C_HWAsst = Get-SCClass -Name "Provance.Class.ComputerAsset" $C_User = Get-SCCLass -Name "System.User" $R_AgHasUser = Get-SCRelationship -Name "Provance.Relationship.AgreementHasUser" $R_HasMnt = Get-SCRelationship -Name "Provance.Relationship.HardwareAssetStandardHasHardwareMaintenanceAgreement" # Define List Values and Filters $Approved = Get-ScsmPxListItem -ListName Provance.Enumeration.AgreementLifeCycle -Name Approved # Define Agreement Variables $DueDate = (Get-Date).AddDays($DueDays) $DueDateString = $DueDate.ToString("dd.MM.yyyy") $ReportDate = (Get-Date).ToString("dd.MM.yyyy") $ReportFileName = "ProvanceMainReportDue_$ReportDate.html" # Set Mail Variables $SMTPServer = 'mail.yourdomaindomain.local' $FromAdress = 'ProvanceNotifications@yourdomain.local' # Stylesheet definition $Style = @' <style> body {Background-color:#EEEEEE; } body,table,td,th,h1 {font-family:Calibri; color:DarkBlue; Font-Size:14pt; Text-Align:left } th {font-weight:bold; background-color:#AAAAAA; } td {background-color:white; } h1 {font-size:20pt; text-align:left ; color:darkRed} h2 {font-size;14pt; text-align:left ; color:grey} </style> '@ # Functions Function Get-SCSMUserEMailAdress ([System.Object]$SCSMUser) { $C_userpreference = Get-SCSMRelationship -Id 649e37ab-bf89-8617-94f6-d4d041a05171 $Notif = Get-SCSMRelationshipInstance -SourceInstance $SCSMUser | where {$_.RelationshipId -eq $C_userpreference.Id} $EmailObject = $Notif.TargetObject.Values | ? {$_.Type -match 'TargetAddress'} $Email = $EmailObject.Value $Email } <#.. For future Use $Loc = $CC = $Org = $User = ..#> #endregion Definitons #region data processing # Get the Agreements and the Assets in Variables $DueAgreements = Get-SCClassInstance -Class $C_HWAggr -Filter "LifeCycle -eq $($Approved.Id)"|?{$_.ExpiryDate -le $duedate} #Create Data Arrays $EMailAdresses = @() $HTMLBody = @() # Create Base HTML Output File and Header Line ConvertTo-HTML DisplayName -Head $Style -Precontent "<H1>Hardware Maintenance Expiry report for $DueDateString </H1>"|Out-File "$ReportFolder$ReportFileName" Foreach ($Agreement in $DueAgreements) { # Get the Assets Related to that HWMaint Agreement $DueAssets = Get-ScsmPxRelatedObject -Target $Agreement -RelationshipClassName $R_HasMnt # Get The Agreement Manager(s) $AgreementManagers = Get-ScsmPxRelatedObject -Source $Agreement -RelationshipClassName $R_AgHasUser # Write Agreement and Related Assets to the Output File $FileAgreementBody = $Agreement|select-object @{expression={$_.DisplayName};Label="Agreement Name"},ExpiryDate|ConvertTo-Html -Fragment $FileDueAssetsBody = $DueAssets|select-object @{expression={$_.DisplayName};Label="Asset Name"},AssetTag,Type,SerialNumber,Lifecycle,ReceivedDate|ConvertTo-Html -Fragment $HTMLBody = $HTMLBody+"<br>"+$FileAgreementBody+"<br>"+$FileDueAssetsBody+"<br><br>" # Find E-Mail Adresses of Managers in the current agreement foreach ($AgreementManager in $AgreementManagers) { Get-SCSMUserEMailAdress $AgreementManager # Fill up E-Mail Addresses Array $EMailAdresses = $EMailAdresses + $EMail # Send E-Mail to each Agreement Manager foreach ($EMailAdress in $EMailAdresses) { #Create Body Text for One Agreement $MailBodyStyleHeader = ConvertTo-HTML -Head $Style $MailBodyTitle = ConvertTo-HTML -Title "Maintenance Due Report for $DueDate" $AgreementBody = $Agreement | ConvertTo-HTML DisplayName,ExpiryDate -Head $Style $DueAssetsBody = $DueAssets | ConvertTo-HTML DisplayName,AssetTag,Type,SerialNumber,LifeCycle,ReceivedDate -Fragment # Summarize Agreement and Asset Data $MailBody = @($AgreementBody + $DueAssetsBody) # Send The Mail Message to the AgreementManager # Send-MailMessage -To $EMailAdress -Subject "Hardware Maintenance Expiry Report for $DuedateString" -SmtpServer $SMTPServer -Encoding UTF8 -From $FromAdress -Body $Mailbody -BodyasHTML } } } #endregion # Write Results and Line into the HTML File $HTMLBody|Out-File "$ReportFolder$ReportFileName" -Append ConvertTo-HTML $NULL -PostContent "<H2> Report created $ReportDate</H2>" -Fragment |Out-File "$ReportFolder$ReportFileName" -Append remove-module scsmpx |
[/codesyntax]
With best regards /Roman