if ($Args.Count -ne $EXPECTED_ARGUMENT_COUNT)
{
$api.LogScriptEvent(
$EVENT_SOURCE,
$UNEXPECTED_ARGUMENT_COUNT_ERROR_ID,
$EVENT_TYPE_ERROR,
"Expected " + $EXPECTED_ARGUMENT_COUNT + " arguments. But there were " + $Args.Count + " arguments. Exiting script.`n Arguments: " + $arguments);
exit -1;
}
# Assign global variables to parameters passed in from the command line.
#
$sourceType = $Args[0];
$sourceId = $Args[1];
$managedEntityId = $Args[2];
$computerPrincipalName = $Args[3];
$computerNetbiosName = $Args[4];
$computerActiveDirectorySite = $Args[5];
$verboseLoggingString = $Args[6];
# Log to EventLog if verboseLogging is set
function LogEvent($eventId, $eventType, $message)
{
if ($verboseLoggingString -eq "true")
{
$api.LogScriptEvent($EVENT_SOURCE, $eventId, $eventType, $message);
}
}
# Create the discovery data object for adding discovered instances.
$discoveryData = $api.CreateDiscoveryData($sourceType, $sourceId, $managedEntityId);
$commonRoleName = $computerNetbiosName + " (Common) - " + $computerActiveDirectorySite;
# ----------------------------------------------------------------------
# First get the essential objects related to this server.
# We will use these for creating classes in the next section.
# ----------------------------------------------------------------------
#
# This script discovers only the Domain Controllers that are insite for current machine.
# Out of site DCs are not used in regular operation. So those are ignored. (see E15 43434)
# Get the FQDNs in an array of strings. All have to be lowercased.
$ADSite = [System.DirectoryServices.ActiveDirectory.ActiveDirectorySite]::GetComputerSite();
# DomainController.Name is fqdn
$dcNames = $ADSite.Servers | % { $_.Name.tolower() } | sort;
# ----------------------------------------------------------------------
# Create the CommonActiveDirectoryAccess instance (key Property=ComponentName)
# ADH_3
# one per Exchange Server
# ----------------------------------------------------------------------
$oCommonADAccess = $discoveryData.CreateClassInstance("$MPElement[Name='Microsoft.Exchange.2010.CommonActiveDirectoryAccess']$");
$oCommonADAccess.AddProperty("$MPElement[Name='Windows!Microsoft.Windows.Computer']/PrincipalName$", $computerPrincipalName);
$oCommonADAccess.AddProperty("$MPElement[Name='Microsoft.Exchange.2010.Component']/ComponentName$", "AD Access - " + $commonRoleName);
$oCommonADAccess.AddProperty("$MPElement[Name='Microsoft.Exchange.2010.ServerRole']/RoleName$", $commonRoleName);
$oCommonADAccess.AddProperty("$MPElement[Name='Microsoft.Exchange.2010.Component']/InstanceName$", "AD Access - " + $computerNetbiosName);
$discoveryData.AddInstance($oCommonADAccess);
# ServiceName is a key property.
# ADH_4
# one class per site (service level class). i.e. there will be exactly one service level instance of this class.
$oCommonADAccessService = $discoveryData.CreateClassInstance("$MPElement[Name='Microsoft.Exchange.2010.CommonActiveDirectoryAccessService']$");
$oCommonADAccessService.AddProperty("$MPElement[Name='Microsoft.Exchange.2010.Service']/ServiceName$", "AD Access Service - " + $computerActiveDirectorySite);
$oCommonADAccessService.AddProperty("$MPElement[Name='Microsoft.Exchange.2010.Service']/InstanceName$", "AD Access Service - " + $computerActiveDirectorySite);
$oCommonADAccessService.AddProperty("$MPElement[Name='Microsoft.Exchange.2010.Service']/ActiveDirectorySite$", $computerActiveDirectorySite);
$discoveryData.AddInstance($oCommonADAccessService);
# ----------------------------------------------------------------------
# Create the DomainController classes.
# ----------------------------------------------------------------------
$dcNames | foreach {
# Per E14 bug 161851, make sure the instance name is not empty.
# We have seen cases where this is an empty string and causing discovery to fail.
if (![string]::IsNullOrEmpty($_))
{
# ServiceName is a key property, we need to make this entity specific for this AD site by uniquely identify it with the AD site in its name.
# ADH_1
# one class per DC & per site (service level class). i.e. there will be a service level instance of this class for each DC.
$dcServiceName = $discoveryData.CreateClassInstance("$MPElement[Name='Microsoft.Exchange.2010.CommonADAccessDomainControllerService']$");
$dcServiceName.AddProperty("$MPElement[Name='Microsoft.Exchange.2010.Service']/ServiceName$", "Domain Controller Service (" + $_ + ") - " + $computerActiveDirectorySite);
$dcServiceName.AddProperty("$MPElement[Name='Microsoft.Exchange.2010.Service']/InstanceName$", $_);
$dcServiceName.AddProperty("$MPElement[Name='Microsoft.Exchange.2010.Service']/ActiveDirectorySite$", $computerActiveDirectorySite);
$discoveryData.AddInstance($dcServiceName);
# ADH_2
# one class per DC & per Exchange Server
$discoverDcName = $discoveryData.CreateClassInstance("$MPElement[Name='Microsoft.Exchange.2010.CommonDomainController']$");
$discoverDcName.AddProperty("$MPElement[Name='Windows!Microsoft.Windows.Computer']/PrincipalName$", $computerPrincipalName);
$discoverDcName.AddProperty("$MPElement[Name='Microsoft.Exchange.2010.Component']/ComponentName$", "Domain Controller (" + $_ + ") - " + $commonRoleName);
$discoverDcName.AddProperty("$MPElement[Name='Microsoft.Exchange.2010.ServerRole']/RoleName$", $commonRoleName);
$discoverDcName.AddProperty("$MPElement[Name='Microsoft.Exchange.2010.Component']/InstanceName$", $_);
$discoveryData.AddInstance($discoverDcName);