View cancelled batch tasks

Microsoft.Dynamics.AX6.ManagementPack.AOS.Monitoring.BatchFramework.ViewCancelledTasks (Task)

View a list of cancelled batch tasks.

Element properties:

TargetMicrosoft.Dynamics.AX6.ManagementPack.ClassLibrary.BatchFramework
AccessibilityPublic
CategoryOperations
EnabledTrue
RemotableFalse
Timeout300

Member Modules:

ID Module Type TypeId RunAs 
Script WriteAction System.CommandExecuter Microsoft.Dynamics.AX6.ManagementPack.ClassLibrary.AxDbReaderAccount

Source Code:

<Task ID="Microsoft.Dynamics.AX6.ManagementPack.AOS.Monitoring.BatchFramework.ViewCancelledTasks" Accessibility="Public" Enabled="true" Target="MicrosoftDynamicsAX6ManagementPackClassLibrary!Microsoft.Dynamics.AX6.ManagementPack.ClassLibrary.BatchFramework" Timeout="300" Remotable="true">
<Category>Operations</Category>
<WriteAction ID="Script" RunAs="MicrosoftDynamicsAX6ManagementPackClassLibrary!Microsoft.Dynamics.AX6.ManagementPack.ClassLibrary.AxDbReaderAccount" TypeID="System!System.CommandExecuter">
<ApplicationName>%windir%\system32\windowspowershell\v1.0\powershell.exe</ApplicationName>
<WorkingDirectory/>
<CommandLine>-Command ".\Microsoft.Dynamics.AX6.ManagementPack.AOS.Monitoring.ViewCancelledTasks.ps1 '$Target/Property[Type="MicrosoftDynamicsAX6ManagementPackClassLibrary!Microsoft.Dynamics.AX6.ManagementPack.ClassLibrary.BatchFramework"]/ServerName$' '$Target/Property[Type="MicrosoftDynamicsAX6ManagementPackClassLibrary!Microsoft.Dynamics.AX6.ManagementPack.ClassLibrary.BatchFramework"]/DatabaseName$'"</CommandLine>
<TimeoutSeconds>50</TimeoutSeconds>
<RequireOutput>true</RequireOutput>
<Files>
<File>
<Name>Microsoft.Dynamics.AX6.ManagementPack.AOS.Monitoring.ViewCancelledTasks.ps1</Name>
<Contents><Script>

function GetLibraryPath($key)
{
foreach ($sub in $key)
{
$aosPath = $sub.GetValue("AOSPath")
$tokens = $aosPath.Split('\')
$libPath = ""
foreach ($token in $tokens)
{
if($token -ne "Server")
{
$libPath = $libPath + $token + "\"
}
else
{
$libPath = $libPath + $token + "\Common\"
return $libPath
}
}
break
}
}

function WriteErrorToEventLog($functionName)
{
$errorRecord = [string]::Format("{0}`n{1}`n{2}`n{3}`n",$Error[0].Exception.Message,$Error[0].InvocationInfo.Line,$Error[0].InvocationInfo.PositionMessage,$Error[0].Exception)
if($Error[0].Exception.InnerException -ne $null) {
$errorRecord = $errorRecord + $Error[0].Exception.InnerException }
$scomapi.LogScriptEvent("Exception in " + $functionName, 300, 1, "`n`nError: " +$errorRecord)
$Error.Clear()
}

try
{
$dbServer = $args[0]
$dbName = $args[1]
$scomapi = new-object -comObject "MOM.ScriptAPI"
}
catch
{
WriteErrorToEventLog "Initializing scom API object"
return
}
try
{
$aosRegPath = "HKLM:\SYSTEM\CurrentControlSet\Services\Dynamics Server\6.0"
$key = Get-ChildItem $aosRegPath

$libraryPath = GetLibraryPath $key
$localizedFilePath = $libraryPath
$dllLoadPath = $libraryPath + "Microsoft.Dynamics.Ax.ManagementPackSupport.dll"
$libraryPath = $libraryPath + "Microsoft.Dynamics.Ax.ManagementPackSupport.ps1"

#PSUICulture is in the format &lt;language&gt;-&lt;region&gt;. get language
$language = ($PsUICulture.Split('-'))[0]
if($language.ToUpper() -eq "ZH") { $language = "ZH-HANS" }
if($language.ToUpper() -eq "PT") { $language = "PT-BR" }

.$libraryPath
Import-LocalizedData -BindingVariable localizedMessages -BaseDirectory $localizedFilePath -FileName Microsoft.Dynamics.Ax.ManagementPackSupport.psd1 -UICulture $language

$eventBatchTaskCancelledTasksStarted = "PowerShell script for View Cancelled Tasks task executed"
$scomapi.LogScriptEvent("Microsoft.Dynamics.AX6.ManagementPack.AOS.Task.BatchFramework.CancelledTasks", 100, 4, $eventBatchTaskCancelledTasksStarted)

$tasks = @{}
try
{
GetCancelledTasks $dbServer $dbName $tasks
if($tasks.Count -gt 0)
{
$tasks.Keys | foreach {
$task = @{}
$task = $tasks.Item($_)

$taskObject = New-Object PSObject
$taskObject | Add-Member Noteproperty $localizedMessages.taskColumnTaskId $task[$TaskRecIdAttrib]
$taskObject | Add-Member Noteproperty $localizedMessages.taskColumnJobId $task[$TaskBatchJobIdAttrib]
$taskObject | Add-Member Noteproperty $localizedMessages.taskColumnStartTime $task[$TaskStartDateTimeAttrib]
$taskObject | Add-Member Noteproperty $localizedMessages.taskColumnEndTime $task[$TaskEndDateTimeAttrib]
$taskObject | Add-Member Noteproperty $localizedMessages.taskColumnCompany $task[$TaskCompanyAttrib]
$taskObject | Add-Member Noteproperty $localizedMessages.taskColumnBatchGroup $task[$TaskGroupIdAttrib]
$taskObject | Add-Member Noteproperty $localizedMessages.taskColumnDescription $task[$TaskCaptionAttrib]

Write $taskObject
} | Format-Table -AutoSize
}
else
{
Write-Host $localizedMessages.noTasks
}
}
catch
{
WriteErrorToEventLog "GetCancelledTasks"
Write-Host $localizedMessages.taskCompletionError
}
}
catch
{
WriteErrorToEventLog "GetLibraryPath"
Write-Host $localizedMessages.taskCompletionError
return
}
</Script></Contents>
<Unicode>true</Unicode>
</File>
</Files>
</WriteAction>
</Task>