IIS Application Pool Status Probe

Microsoft.Dynamics.Retail.AX2012.R2.FP.Monitoring.Probe.AppPoolStatus (ProbeActionModuleType)

Gets the status, version and mode of the specified application pool.

Element properties:

TypeProbeActionModuleType
IsolationAny
AccessibilityInternal
RunAsDefault
OutputTypeSystem.PropertyBagData

Member Modules:

ID Module Type TypeId RunAs 
PowerShellPropertyBagTriggerOnlyProbe ProbeAction Microsoft.Windows.PowerShellPropertyBagTriggerOnlyProbe Default

Overrideable Parameters:

IDParameterTypeSelectorDisplay NameDescription
TimeoutSecondsint$Config/TimeoutSeconds$Maximum Processing TimeMaximum time spent processing a monitoring script.

Source Code:

<ProbeActionModuleType ID="Microsoft.Dynamics.Retail.AX2012.R2.FP.Monitoring.Probe.AppPoolStatus" Accessibility="Internal" Batching="false" PassThrough="false">
<Configuration>
<xsd:element minOccurs="1" name="AppPoolName" type="xsd:string"/>
<xsd:element minOccurs="1" name="TimeoutSeconds" type="xsd:integer"/>
</Configuration>
<OverrideableParameters>
<OverrideableParameter ID="TimeoutSeconds" Selector="$Config/TimeoutSeconds$" ParameterType="int"/>
</OverrideableParameters>
<ModuleImplementation Isolation="Any">
<Composite>
<MemberModules>
<ProbeAction ID="PowerShellPropertyBagTriggerOnlyProbe" TypeID="Windows!Microsoft.Windows.PowerShellPropertyBagTriggerOnlyProbe">
<ScriptName>IISAppPoolStatus.ps1</ScriptName>
<ScriptBody><Script>&lt;#
SAMPLE CODE NOTICE

THIS SAMPLE CODE IS MADE AVAILABLE AS IS. MICROSOFT MAKES NO WARRANTIES, WHETHER EXPRESS OR IMPLIED,
OF FITNESS FOR A PARTICULAR PURPOSE, OF ACCURACY OR COMPLETENESS OF RESPONSES, OF RESULTS, OR CONDITIONS OF MERCHANTABILITY.
THE ENTIRE RISK OF THE USE OR THE RESULTS FROM THE USE OF THIS SAMPLE CODE REMAINS WITH THE USER.
NO TECHNICAL SUPPORT IS PROVIDED. YOU MAY NOT DISTRIBUTE THIS CODE UNLESS YOU HAVE A LICENSE AGREEMENT WITH MICROSOFT THAT ALLOWS YOU TO DO SO.
#&gt;

param($appPoolName)
# this constant is used for logger initialization
$kScriptName = "IISAppPoolStatus.ps1"
#Logging.psm1#

## Event codes
### RetailServerInstanceDiscovery
$kRetailServerInstanceDiscoveryStartedToRun = 10000
$kRetailServerInstanceDiscoveryRegistryKeyFound = 10001
$kRetailServerInstanceDiscoveryNewInstanceAddedToDiscoveryData = 10002
$kRetailServerInstanceDiscoveryNoRegistryKeyFound = 10003
$kRetailServerInstanceDiscoveryFinishedSuccessfully = 10004
$kRetailServerInstanceDiscoveryFinishedWithError = 10005

### RealtimeServiceInstanceDiscovery
$kRealtimeServiceInstanceDiscoveryStartedToRun = 10020
$kRealtimeServiceInstanceDiscoveryRegistryKeyFound = 10021
$kRealtimeServiceInstanceDiscoveryAddedToDiscoveryData = 10022
$kRealtimeServiceInstanceDiscoveryNoRegistryKeyFound = 10023
$kRealtimeServiceInstanceDiscoveryFinishedSuccessfully = 10024
$kRealtimeServiceInstanceDiscoveryFinishedWithError = 10025

### AsyncServerReplicationJobDiscovery
$kAsyncServerReplicationJobDiscoveryReplicationJobFound = 10050
$kAsyncServerReplicationJobDiscoveryNoReplicationJobsFound = 10051
$kAsyncServerReplicationJobDiscoveryNoEventWithReplicationJobs = 10052
$kAsyncServerReplicationJobDiscoveryStartedToRun = 10053
$kAsyncServerReplicationJobDiscoveryNoAOSInstancesFound = 10054
$kAsyncServerReplicationJobDiscoveryNoAOSRegistryKeysFound = 10055
$kAsyncServerReplicationJobDiscoveryFinishedSuccessfully = 10056
$kAsyncServerReplicationJobDiscoveryFinishedWithError = 10057
$kAsyncServerReplicationJobDiscoveryReplicationJobsEventId = 10065 # This event is generated by Dynamics Server

### SharePointECommerceDiscovery
$kSharePointECommerceDiscoveryStartedToRun = 10080
$kSharePointECommerceDiscoveryFinishedSuccessfully = 10081
$kSharePointECommerceDiscoveryFinishedWithError = 10082
$kSharePointECommerceDiscoveryFarmFound = 10083
$kSharePointECommerceDiscoveryFarmNotFound = 10084
$kSharePointECommerceDiscoveryECommerceDeploymentFound = 10085
$kSharePointECommerceDiscoveryECommerceDeploymentNotFound = 10086
$kSharePointECommerceDiscoveryECommerceWebAppsFound = 10087
$kSharePointECommerceDiscoveryECommerceWebAppsNotFound = 10088
$kSharePointECommerceDiscoveryECommerceTimerJobsFound = 10089
$kSharePointECommerceDiscoveryECommerceTimerJobsNotFound = 10090
$kSharePointECommerceDiscoveryECommerceWebAppDiscoveryDataAdded = 10091
$kSharePointECommerceDiscoveryECommerceTimerJobDiscoveryDataAdded = 10091

### CdxSynchServiceClientDiscovery
$kCdxSynchServiceClientDiscoveryStartedToRun = 10100
$kCdxSynchServiceClientDiscoveryRootKeyFound = 10101
$kCdxSynchServiceClientDiscoveryFailedToEvaluateErrorCountingPeriod = 10102
$kCdxSynchServiceClientDiscoveryNewInstanceDiscovered = 10103
$kCdxSynchServiceClientDiscoveryNoRootKeyFound = 10104
$kCdxSynchServiceClientDiscoveryFinishedSuccessfully = 10105
$kCdxSynchServiceClientDiscoveryFinishedWithError = 10106

### CdxSynchServiceServerDiscovery
$kCdxSynchServiceServerDiscoveryStartedToRun = 10110
$kCdxSynchServiceServerDiscoveryRootKeyFound = 10111
$kCdxSynchServiceServerDiscoveryNewInstanceDiscovered = 10112
$kCdxSynchServiceServerDiscoveryNoRootKeyFound = 10113
$kCdxSynchServiceServerDiscoveryFinishedSuccessfully = 10114
$kCdxSynchServiceServerDiscoveryFinishedWithError = 10115

### RetailServerChannelDBConnectionDiscovery
$kRetailServerChannelDBConnectionDiscoveryStartedToRun = 10120
$kRetailServerChannelDBConnectionDiscoverySearchingSuccessEvents = 10121
$kRetailServerChannelDBConnectionDiscoverySearchingErrorEvents = 10122
$kRetailServerChannelDBConnectionDiscoveryNewInstanceFound = 10123
$kRetailServerChannelDBConnectionDiscoveryNoInstancesFound = 10124
$kRetailServerChannelDBConnectionDiscoveryFinishedSuccessfully = 10125
$kRetailServerChannelDBConnectionDiscoveryFinishedWithError = 10126

### HardwareStationDiscovery
$kHardwareStationDiscoveryStartedToRun = 10140
$kHardwareStationDiscoveryRegistryKeyFound = 10141
$kHardwareStationDiscoveryNewInstanceAddedToDiscoveryData = 10142
$kHardwareStationDiscoveryNoRegistryKeyFound = 10143
$kHardwareStationDiscoveryFinishedSuccessfully = 10144
$kHardwareStationDiscoveryFinishedWithError = 10145

$kChannelDbConnectivityFailureEventId = 10200 # This event is generated by CRT Sdk (Retail Server, POS, eCommerce)
$kChannelDbConnectivitySuccessEventId = 10201 # This event is generated by CRT Sdk (Retail Server, POS, eCommerce)

### ReplicationJobScheduleState
$kReplicationJobStateStartedToRun = 10230
$kReplicationJobStateNoMonitoringEvents = 10231
$kReplicationJobStateSucceeded = 10232
$kReplicationJobStateFailed = 10233
$kReplicationJobStateFinishedSuccessfully = 10234
$kReplicationJobStateFinishedWithError = 10235

$kReplicationJobStateEventId = 10230 # This event is generated by Dynamics Server

### IISAppPoolStatus
$kIISAppPoolStatusStartedToRun = 10300
$kIISAppPoolStatusInetsrvNotExist = 10301
$kIISAppPoolStatusAppCmdDoesntExist = 10302
$kIISAppPoolStatusNoAppPoolName = 10303
$kIISAppPoolStatusAppcmdExitedWithError = 10304
$kIISAppPoolStatusUnexpectedAppcmdOutput = 10305
$kIISAppPoolStatusLogBagContent = 10306
$kIISAppPoolStatusFinishedSuccessfully = 10307
$kIISAppPoolStatusExitedWithError = 10308

### ConnectionToChannelDbState
$kConnectionToChannelDbStateStartedToRun = 10310
$kConnectionToChannelDbStateSearchingSuccessEvents = 10311
$kConnectionToChannelDbStateSearchingErrorEvents = 10312
$kConnectionToChannelDbStateNoMonitoringEvents = 10313
$kConnectionToChannelDbStateSuccess = 10314
$kConnectionToChannelDbStateFailed = 10315
$kConnectionToChannelDbStateFinishedSuccessfully = 10316
$kConnectionToChannelDbStateFinishedWithError = 10317

### HardwareStationSmokeTest
$kHardwareStationSmokeTestStartedToRun = 10320
$kHardwareStationSmokeTestFinishedSuccessfully = 10321
$kHardwareStationSmokeTestFinishedWithError = 10322

### ChangeTraceLevelInAppConfig
$kChangeTraceLevelInAppConfigStartedToRun = 10400
$kChangeTraceLevelInAppConfigFinishedSuccessfully = 10401
$kChangeTraceLevelInAppConfigFinishedWithError = 10402
$kChangeTraceLevelInAppConfigFileSaved = 10403
$kChangeTraceLevelInAppConfigFileUnchanged = 10404
$kChangeTraceLevelInAppConfigFileParameterCheckError = 10405

### ChangeSharePointTracingLevel
$kChangeSharePointTracingLevelStartedToRun = 10600
$kChangeSharePointTracingLevelFinishedSuccessfully = 10601
$kChangeSharePointTracingLevelFinishedWithError = 10602
$kChangeSharePointTracingLevelParameterCheckError = 10603

### EnterprisePosDiscovery
$kEnterprisePosDiscoveryStartedToRun = 10700
$kEnterprisePosDiscoveryFinishedSuccessfully = 10701
$kEnterprisePosDiscoveryFinishedWithError = 10702
$kNoEnterprisePosDiscoveryDataFound = 10703
$kEnterprisePosDiscoveryEvent = 10704

### EnterprisePosOfflineDiscovery
$kRetailPosOfflineServiceDiscoveryStartedToRun = 10800
$kNoRetailPosOfflineServiceDiscoveryDataFound = 10801
$kRetailPosOfflineServiceDiscoveryFinishedSuccessfully = 10802
$kRetailPosOfflineServiceDiscoveryRootKeyFound = 10803
$kRetailPosOfflineServiceDiscoveryNotUsedByPos = 10804
$kRetailPosOfflineServiceDiscoveryFinishedSuccessfully = 10805
$kNoRetailPosOfflineServiceAdditionalDiscoveryDataFound = 10806
$kRetailPosOfflineServiceDiscoveryFinishedSuccessfully = 10807
$kRetailPosOfflineServiceDiscoveryFinishedWithError = 10808

### StartAppPool
$kStartAppPoolStartedToRun = 10900
$kStartAppPoolFinishedSuccessfully = 10901
$kStartAppPoolFinishedWithError = 10902

### CertificateStatus
$kCertificateStatusStartedToRun = 10910
$kCertificateStatusNonExistingCertificate = 10911
$kCertificateStatusLogBagContent = 10912
$kCertificateStatusFinishedSuccessfully = 10913
$kCertificateStatusExitedWithError = 10914

### SetDiscoveryTimeFrame
$kSetDiscoveryTimeFrameStartedToRun = 10920
$kSetDiscoveryTimeFrameFinishedSuccessfully = 10921
$kSetDiscoveryTimeFrameFinishedWithError = 10922

## Functions
### Function tests if logging is enabled
function Test-LoggingEnabled
{
(Get-ItemProperty -Path $LoggingAbilityItemPath -ErrorAction SilentlyContinue).EnableMonitoringScriptLogging -eq "Enabled"
}

## Constants
$LoggingAbilityItemPath = "HKLM:\SOFTWARE\Microsoft\Dynamics\6.0"
$LoggingAbilityItemPropertyName = "EnableMonitoringScriptLogging"

$kLogWidth = 200 # characters
$kLogName = "Application"
$kLogSourceName = "Microsoft Dynamics AX Retail Monitoring"
$kEnableLogging = Test-LoggingEnabled

# This is needed to mock the logger object
if ($logger -ne $null)
{
# $logger object is initialized with the MS.Dynamics.Test.Retail.Test.Monitoring.Mocks.Logger object
$logger.LogName = $kLogName;
$logger.LogSourceName = $kLogSourceName;
$logger.ScriptName = $kScriptName;
}
else
{
# this means that object $logger is not initialized
## Create logger
$logger = New-Object -TypeName System.Management.Automation.PSObject

### Add logger properties
$logger | Add-Member -MemberType ScriptProperty -Name LogName -Value { $kLogName }
$logger | Add-Member -MemberType ScriptProperty -Name LogSourceName -Value { $kLogSourceName }
$logger | Add-Member -MemberType ScriptProperty -Name ScriptName -Value { $kScriptName }
$logger | Add-Member -MemberType ScriptProperty -Name ShouldWriteToEventLog -Value { $kEnableLogging }

### Add logger methods
$logger | Add-Member -MemberType ScriptMethod -Name LogInfo -Value {
param($eventId, $message)
if ($This.ShouldWriteToEventLog)
{
try
{
Write-EventLog -LogName $This.LogName -Source $This.LogSourceName -EntryType Information -EventId $eventId -Message ($This.ScriptName + " : " + $message) -ErrorAction Stop
}
catch
{
try
{
$scomApi.LogScriptEvent($This.ScriptName, $eventId, 0, "{0}" -f $message)
}
catch
{
}
}
}
}

$logger | Add-Member -MemberType ScriptMethod -Name LogError -Value {
param($eventId, $message)
try
{
Write-EventLog -LogName $This.LogName -Source $This.LogSourceName -EntryType Error -EventId $eventId -Message ($This.ScriptName + " : " + $message) -ErrorAction Stop
}
catch
{
try
{
$scomApi.LogScriptEvent($This.ScriptName, $eventId, 2, "{0}" -f $message)
}
catch
{
}
}
}

$logger | Add-Member -MemberType ScriptMethod -Name LogScriptExecutionSucceeded -Value {
param($eventId)
$This.LogInfo($eventId, "Script execution ended successfully")
}

$logger | Add-Member -MemberType ScriptMethod -Name LogScriptExecFailed -Value {
param($eventId, $error)
$This.LogError($eventId, "Script executed with errors :`n" + ($error | Out-String -Width $kLogWidth))
}

$logger | Add-Member -MemberType ScriptMethod -Name LogScriptExecFailedWithDefaultError -Value {
param($eventId)
$This.LogError($eventId, "Script executed with errors :`n" + ($Global:Error | Out-String -Width $kLogWidth))
}
}

#ScomApi.psm1#

## Create SCOM Api Object
# needed for mocking
if ($scomApi -ne $null)
{
# scomApi is created by wrapper test case
}
else
{
$scomApi = New-Object -ComObject 'MOM.ScriptAPI' -ErrorAction SilentlyContinue
if ($scomApi -eq $null) {
return $null
}
}


Function Test-IfStringIsNullOrWhiteSpace
{
param([string]$String)
if (($String -eq $null) -or ([string]::IsNullOrEmpty($String.Trim())))
{
return $true;
}
return $false;
}

# Run actual monitor code
$logger.LogInfo($kIISAppPoolStatusStartedToRun, ("Checking application pool status for {0}." -f $appPoolName))
try {
# Create a SCOM Property bag to return the values in.
$bag = $scomApi.CreatePropertyBag()

$appCmdFolder = "$env:systemroot\system32\inetsrv"

# If we can't find the appcmd folder, we exit with an error.
if ((Test-Path $appCmdFolder) -eq $false)
{
$logger.LogScriptExecFailed($kIISAppPoolStatusInetsrvNotExist, "$appCmdFolder doesn't exist.")
return $bag
}

# Move into the appcmd folder.
cd $appCmdFolder

# Make sure appcmd exists.
if ((Test-Path ".\appcmd.exe") -eq $false)
{
$logger.LogScriptExecFailed($kIISAppPoolStatusAppCmdDoesntExist, "appcmd.exe does not exist inside $appCmdFolder");
return $bag
}

if (Test-IfStringIsNullOrWhiteSpace -String $appPoolName)
{
$logger.LogScriptExecFailed($kIISAppPoolStatusNoAppPoolName, "App pool name provided to monitoring script is empty.")
return $bag;
}

$global:LASTEXITCODE = 0

# Run appcmd to find the status of the app pool.
$result = (&amp; ".\appcmd.exe" "list" "apppool" "$appPoolName")

if ($Global:LASTEXITCODE -ne 0)
{
$message = ("appcmd.exe finished execution with error for apppool {0}. Exit code : {1}.`nError message : {2}." -f $appPoolName, $Global:LASTEXITCODE, ($result | Out-String))
$logger.LogScriptExecFailed($kIISAppPoolStatusAppcmdExitedWithError, $message)
return $bag
}

# Parse out the status of the app pool.
$tokens = $result.split(":,)", [StringSplitOptions]::RemoveEmptyEntries)

# Make sure we got back a string in the format we expected.
if($tokens -and $tokens.Count -ne 6) {
$logger.LogScriptExecFailed($kIISAppPoolStatusUnexpectedAppcmdOutput, "Unexpected result returned from appcmd list apppool command. ($result)")
return $bag
}

$version = $tokens[1]
$mode = $tokens[3]
$status = $tokens[5]

# log valuable information
$bagContent = ("Version : {0}`nMode : {1}`nStatus : {2}" -f $version, $mode, $status)
$logger.LogInfo($kIISAppPoolStatusLogBagContent, ("Bag content for web application pool {1}: `n{0}" -f $bagContent, $appPoolName))

# Add the status of the app pool to the bag.
$bag.AddValue('Version', $version)
$bag.AddValue('Mode', $mode)
$bag.AddValue('Status', $status)

$logger.LogScriptExecutionSucceeded($kIISAppPoolStatusFinishedSuccessfully)
} catch {
$logger.LogScriptExecFailed($kIISAppPoolStatusExitedWithError, $_)
}
$bag
</Script></ScriptBody>
<Parameters>
<Parameter>
<Name>appPoolName</Name>
<Value>$Config/AppPoolName$</Value>
</Parameter>
</Parameters>
<TimeoutSeconds>$Config/TimeoutSeconds$</TimeoutSeconds>
</ProbeAction>
</MemberModules>
<Composition>
<Node ID="PowerShellPropertyBagTriggerOnlyProbe"/>
</Composition>
</Composite>
</ModuleImplementation>
<OutputType>System!System.PropertyBagData</OutputType>
<TriggerOnly>true</TriggerOnly>
</ProbeActionModuleType>