As I’ve mentioned before, at work we’re migrating all our scheduled tasks to JAMS. Now JAMS has a lot of flexibility to notify, sends emails etc but… you have to tell it to 🙂
And you can imagine that having to click-click-type-click in order to change, say, the email address in a few tens of jobs is not the creative work a developer craves for. Writing a powershell script to do that, though, is!
So here’s the script I wrote to change the email address for Warnings and Critical conditions, in bulk. Of course you can easily modify it to do whatever change you want (enable/disable a lot of jobs at once is a good example).
param(
[string]$jamsServer = "myJamsServer",
[string]$jamsPath = "\somePath\someOtherPath"
)
# This script loops through all enabled JAMS jobs under a certain folder
# recursively, and changes the email address except for successes.
Import-Module Jams
$ErrorActionPreference = "Stop"
cls
try
{
if ($null -eq (Get-PSDrive JD))
{
New-PSDrive JD JAMS $jamsServer -scope Local
}
}
catch
{
New-PSDrive JD JAMS $jamsServer -scope Local
}
$folders = New-Object System.Collections.ArrayList
$rootFolder = (Get-Item "JAMS::$($jamsServer)$($jamsPath)").Name
$folders.Add($rootFolder) | Out-Null
$childFolders = Get-ChildItem "JAMS::$($jamsServer)$($jamsPath)\*" -objecttype Folder -IgnorePredefined
$childFolders | foreach { $folders.Add($_.Name) | Out-Null }
$rootJobs = New-Object System.Collections.ArrayList
foreach($f in $folders)
{
Write-Host "Folder: $f"
if ($f -eq $rootFolder)
{
$jobs = Get-ChildItem "JAMS::$($jamsServer)$($jamsPath)\*" -objecttype Job -IgnorePredefined -FullObject
$jobs | foreach { $rootJobs.Add($_.Name) | Out-Null }
}
else
{
$jobs = Get-ChildItem "JAMS::$($jamsServer)$($jamsPath)\$f\*" -objecttype Job -IgnorePredefined -FullObject
}
# for test
#$jobs | Format-Table -AutoSize
foreach($job in $jobs)
{
#Write-Host "$($job.Name) : $($job.Properties["Enabled"])"
#if you need a name filter as well, you can do:
#if (($job.Name -notlike "*SomeString*") -or ($job.Properties["Enabled"].Value -eq $false))
if ($job.Properties["Enabled"].Value -eq $false)
{
continue
}
$jobElements = $job.Elements
$doUpdate = $false
foreach($jobElement in $jobElements)
{
#Write-Host "$($job.Name) / $($jobElement.ElementTypeName) / $($jobElement.Description) / $($jobElement.ToString())"
if (($jobElement.ElementTypeName -eq "SendEMail") -and ($jobElement.EntrySuccess -eq $false))
{
#Write-Host "$($job.Name) / $($jobElement.ElementTypeName) / $($jobElement.Description) / $($jobElement.FromAddress) / $($jobElement.ToAddress)"
if ([string]::IsNullOrWhiteSpace($jobElement.ToAddress))
{
$jobElement.FromAddress = "admin@superduperincrediblesoftware.com"
$jobElement.ToAddress = "someone@superduperincrediblesoftware.com;andhisdog@superduperincrediblesoftware.com"
$jobElement.MessageBody = "Uh, Houston, we've had a problem"
$doUpdate = $true
}
}
}
if ($doUpdate -eq $true)
{
$job.Update()
Write-Host "Job $($job.Name) is updated"
}
}
}
Have fun coding 🙂
Very good post. I will be dealing with many of these issues as well..