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.
#>
param($target, $element, $computerName)
# this constant is used for logger initialization
$kScriptName = "SharePointECommerceDiscovery.ps1"
#Logging.psm1#
$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)
## Functions
### Function tests if logging is enabled
function Test-LoggingEnabled
{
(Get-ItemProperty -Path $LoggingAbilityItemPath -ErrorAction SilentlyContinue).EnableMonitoringScriptLogging -eq "Enabled"
}
# 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
## 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
}
}
# 1. Test if we are on a SharePoint machine with a farm
if((Test-Path $farmSharePointRegKeyPath) -and ((Get-Item $farmSharePointRegKeyPath).GetValue("Id") -ne $null))
{
$isSPFarm = $true
$logger.LogInfo($kSharePointECommerceDiscoveryFarmFound, "Found server to be part of a SharePoint farm.")
}
else
{
$logger.LogInfo($kSharePointECommerceDiscoveryFarmNotFound, "Did not find a SharePoint farm. Discovery will end on this server.")
}
# 2. Test if our reg keys are there
if($isSPFarm -eq $true)
{
if((Test-Path $farmECommerceRegKeyPath) -and ((Get-Item $farmECommerceRegKeyPath).GetValue("Id") -ne $null))
{
$farmIdString = (Get-Item $farmECommerceRegKeyPath).GetValue("Id")
$isECommerceSPFarm = $true
$farmIdGuid = [GUID]$farmIdString
$logger.LogInfo($kSharePointECommerceDiscoveryECommerceDeploymentFound, ("Found server to be part of a Dynamics ECommerce deployment. Farm ID is {0}." -f $farmIdString))
}
else
{
$logger.LogInfo($kSharePointECommerceDiscoveryECommerceDeploymentNotFound, "Found server not to be part of a Dynamics ECommerce deployment. Discovery will end on this server.")
}
$serverInstance = $discoveryData.CreateClassInstance("$MPElement[Name='Microsoft.Dynamics.Retail.AX2012.R2.FP.Base.Class.SharePointServer']$")
$serverInstance.AddProperty("$MPElement[Name='System!System.Entity']/DisplayName$", [string]($computerName))
# ID must be unique, so the rule does not get cooked down
$uniqueId = ("{0} - {1}" -f [string]($computerName), $webAppPublisherName)
$serverInstance.AddProperty("$MPElement[Name='Microsoft.Dynamics.Retail.AX2012.R2.FP.Base.Class.SharePointServer']/ID$", [string]($uniqueId))
$serverInstance.AddProperty("$MPElement[Name='Windows!Microsoft.Windows.Computer']/PrincipalName$", [string]$computerName)
$serverInstance.AddProperty("$MPElement[Name='Microsoft.Dynamics.Retail.AX2012.R2.FP.Base.Class.SharePointServer']/FarmID$", [string]($farmIdGuid.ToString()))
$serverInstance.AddProperty("$MPElement[Name='Microsoft.Dynamics.Retail.AX2012.R2.FP.Base.Class.SharePointServer']/PublisherName$", [string]($webAppPublisherName))
$serverInstance.AddProperty("$MPElement[Name='Microsoft.Dynamics.Retail.AX2012.R2.FP.Base.Class.SharePointServer']/PublisherNameMonitoring$", [string]($webAppPublisherNameMonitoring))
$discoveryData.AddInstance($serverInstance)
$appInstance.AddProperty("$MPElement[Name='System!System.Entity']/DisplayName$", [string]($webAppName))
# ID must be unique in order for it to be aggreated. If we monitor 2 farms with the same objects, we would roll them up into one SCOM instance, hence we use farm id here as well
$uniqueId = ("{0} - {1}" -f $farmIdGuid.ToString(), $webAppName)
$appInstance.AddProperty("$MPElement[Name='Microsoft.Dynamics.Retail.AX2012.R2.FP.Base.Class.SharePointWebApplication']/ID$", [string]($uniqueId))
$appInstance.AddProperty("$MPElement[Name='Microsoft.Dynamics.Retail.AX2012.R2.FP.Base.Class.SharePointWebApplication']/Name$", [string]($webAppName))
$appInstance.AddProperty("$MPElement[Name='Microsoft.Dynamics.Retail.AX2012.R2.FP.Base.Class.SharePointWebApplication']/FarmID$", [string]($farmIdGuid.ToString()))
$appInstance.AddProperty("$MPElement[Name='Microsoft.Dynamics.Retail.AX2012.R2.FP.Base.Class.SharePointWebApplication']/PublisherName$", [string]($webAppPublisherName))
$appInstance.AddProperty("$MPElement[Name='Microsoft.Dynamics.Retail.AX2012.R2.FP.Base.Class.SharePointWebApplication']/PublisherNameMonitoring$", [string]($webAppPublisherNameMonitoring))
$appInstance.AddProperty("$MPElement[Name='Microsoft.Dynamics.Retail.AX2012.R2.FP.Base.Class.SharePointWebApplication']/Url$", [string]($webAppUrl))
$serverInstance = $discoveryData.CreateClassInstance("$MPElement[Name='Microsoft.Dynamics.Retail.AX2012.R2.FP.Base.Class.SharePointServer']$")
$serverInstance.AddProperty("$MPElement[Name='System!System.Entity']/DisplayName$", [string]($computerName))
# ID must be unique, so the rule does not get cooked down
$uniqueId = ("{0} - {1}" -f [string]($computerName), $jobPublisherName)
$serverInstance.AddProperty("$MPElement[Name='Microsoft.Dynamics.Retail.AX2012.R2.FP.Base.Class.SharePointServer']/ID$", [string]($uniqueId))
$serverInstance.AddProperty("$MPElement[Name='Windows!Microsoft.Windows.Computer']/PrincipalName$", [string]$computerName)
$serverInstance.AddProperty("$MPElement[Name='Microsoft.Dynamics.Retail.AX2012.R2.FP.Base.Class.SharePointServer']/FarmID$", [string]($farmIdGuid.ToString()))
$serverInstance.AddProperty("$MPElement[Name='Microsoft.Dynamics.Retail.AX2012.R2.FP.Base.Class.SharePointServer']/PublisherName$", [string]($jobPublisherName))
$serverInstance.AddProperty("$MPElement[Name='Microsoft.Dynamics.Retail.AX2012.R2.FP.Base.Class.SharePointServer']/PublisherNameMonitoring$", [string]($jobPublisherNameMonitoring))
$discoveryData.AddInstance($serverInstance)
$jobInstance.AddProperty("$MPElement[Name='System!System.Entity']/DisplayName$", [string]($jobName))
# ID must be unique in order for it to be aggreated. If we monitor 2 farms with the same objects, we would roll them up into one SCOM instance, hence we use farm id here as well
$uniqueId = ("{0} - {1}" -f $farmIdGuid.ToString(), $jobName)
$jobInstance.AddProperty("$MPElement[Name='Microsoft.Dynamics.Retail.AX2012.R2.FP.Base.Class.SharePointTimerJob']/ID$", [string]$uniqueId)
$jobInstance.AddProperty("$MPElement[Name='Microsoft.Dynamics.Retail.AX2012.R2.FP.Base.Class.SharePointTimerJob']/Name$", [string]($jobName))
$jobInstance.AddProperty("$MPElement[Name='Microsoft.Dynamics.Retail.AX2012.R2.FP.Base.Class.SharePointTimerJob']/FarmID$", [string]($farmIdGuid.ToString()))
$jobInstance.AddProperty("$MPElement[Name='Microsoft.Dynamics.Retail.AX2012.R2.FP.Base.Class.SharePointTimerJob']/PublisherName$", [string]($jobPublisherName))
$jobInstance.AddProperty("$MPElement[Name='Microsoft.Dynamics.Retail.AX2012.R2.FP.Base.Class.SharePointTimerJob']/PublisherNameMonitoring$", [string]($jobPublisherNameMonitoring))
$jobInstance.AddProperty("$MPElement[Name='Microsoft.Dynamics.Retail.AX2012.R2.FP.Base.Class.SharePointTimerJob']/Url$", [string]($jobProductCatalogUrl))