Microsoft.KnowledgeServices.SharePoint.Foundation.2010.SPServiceInstance.IncomingMail_Discovery

Microsoft.KnowledgeServices.SharePoint.Foundation.2010.SPServiceInstance.IncomingMail_Discovery (Discovery)

Discovery module for class Microsoft.KnowledgeServices.SharePoint.Foundation.2010.SPServiceInstance.IncomingMail

Element properties:

TargetMicrosoft.Windows.Computer
EnabledTrue
Frequency14401
RemotableFalse

Object Discovery Details:

Discovered Classes and their attribuets:

Member Modules:

ID Module Type TypeId RunAs 
Microsoft.KnowledgeServices.SharePoint.Foundation.2010.SPServiceInstance.IncomingMail_Discovery DataSource Microsoft.Windows.TimedPowerShell.DiscoveryProvider Microsoft.SharePoint.Foundation.2010.AdminAccount

Source Code:

<Discovery ID="Microsoft.KnowledgeServices.SharePoint.Foundation.2010.SPServiceInstance.IncomingMail_Discovery" Enabled="true" Target="Windows!Microsoft.Windows.Computer" ConfirmDelivery="true" Remotable="true" Priority="Normal">
<Category>Discovery</Category>
<DiscoveryTypes>
<DiscoveryClass TypeID="MicrosoftKnowledgeServicesSharePointFoundation2010Library!Microsoft.KnowledgeServices.SharePoint.Foundation.2010.SPServiceInstance.IncomingMail">
<Property TypeID="MicrosoftKnowledgeServicesSharePointFoundation2010Library!Microsoft.KnowledgeServices.SharePoint.Foundation.2010.SPServiceInstance.IncomingMail" PropertyID="Status"/>
<Property TypeID="MicrosoftKnowledgeServicesSharePointFoundation2010Library!Microsoft.KnowledgeServices.SharePoint.Foundation.2010.SPServiceInstance.IncomingMail" PropertyID="NeedsUpgrade"/>
<Property TypeID="MicrosoftKnowledgeServicesSharePointFoundation2010Library!Microsoft.KnowledgeServices.SharePoint.Foundation.2010.SPServiceInstance.IncomingMail" PropertyID="GUID"/>
<Property TypeID="MicrosoftKnowledgeServicesSharePointFoundation2010Library!Microsoft.KnowledgeServices.SharePoint.Foundation.2010.SPServiceInstance.IncomingMail" PropertyID="ServiceStatus"/>
<Property TypeID="MicrosoftKnowledgeServicesSharePointFoundation2010Library!Microsoft.KnowledgeServices.SharePoint.Foundation.2010.SPServiceInstance.IncomingMail" PropertyID="SPVersion"/>
<Property TypeID="MicrosoftKnowledgeServicesSharePointFoundation2010Library!Microsoft.KnowledgeServices.SharePoint.Foundation.2010.SPServiceInstance.IncomingMail" PropertyID="SPMajorVersion"/>
<Property TypeID="MicrosoftKnowledgeServicesSharePointFoundation2010Library!Microsoft.KnowledgeServices.SharePoint.Foundation.2010.SPServiceInstance.IncomingMail" PropertyID="FarmID"/>
<Property TypeID="MicrosoftKnowledgeServicesSharePointFoundation2010Library!Microsoft.KnowledgeServices.SharePoint.Foundation.2010.SPServiceInstance.IncomingMail" PropertyID="HostServer"/>
<Property TypeID="MicrosoftKnowledgeServicesSharePointFoundation2010Library!Microsoft.KnowledgeServices.SharePoint.Foundation.2010.SPServiceInstance.IncomingMail" PropertyID="TypeName"/>
<Property TypeID="MicrosoftKnowledgeServicesSharePointFoundation2010Library!Microsoft.KnowledgeServices.SharePoint.Foundation.2010.SPServiceInstance.IncomingMail" PropertyID="PrincipalName"/>
</DiscoveryClass>
</DiscoveryTypes>
<DataSource ID="Microsoft.KnowledgeServices.SharePoint.Foundation.2010.SPServiceInstance.IncomingMail_Discovery" RunAs="SharePoint!Microsoft.SharePoint.Foundation.2010.AdminAccount" TypeID="Windows!Microsoft.Windows.TimedPowerShell.DiscoveryProvider">
<IntervalSeconds>14401</IntervalSeconds>
<SyncTime/>
<ScriptName>SPServiceInstanceIncomingMailExtension.ps1</ScriptName>
<ScriptBody><Script>

param($SourceId,$ManagedEntityId)

$ErrorActionPreference = "Stop"


function _AdvisorGetTypedValue()
{
param([string] $type, [string] $v)
if (($type -eq "String") -or ($v -eq $null) )
{
return $v
}
if ($v.Trim().Length -gt 0)
{
switch ($type)
{
"Boolean" { if ($v -eq "true") {$true} else {$false} }
"DateTime" { [System.DateTime]::Parse($v) }
"Guid" { New-Object System.Guid($v) }
"Integer" { [int]$v }
"Float" { [float]$v }
}
}
else
{
$null
}
}
function _AdvisorAddProperty()
{
param([object]$o, [string]$p, [object]$v, [bool]$key)
if (($key -eq $true) -and ($v -eq $null))
{
Throw "Key property [$p] cannot be null."
}
if ($v -ne $null)
{
$o.AddProperty($p, $v)
}
}
function _AdvisorCreateBaseObject([string] $className, [string] $displayName)
{
$o = new-object PSObject
$o | Add-Member -MemberType NoteProperty -Name "ClassName" -Value $className
$o | Add-Member -MemberType NoteProperty -Name "DisplayName" -Value $displayName
$o | Add-Member -MemberType NoteProperty -Name "ParentClass" -Value $null
return $o
}


$scriptargs = new-object psobject
$scriptargs | add-member NoteProperty "SourceId" $SourceId
$scriptargs | add-member NoteProperty "ManagedEntityId" $ManagedEntityId


if (!$global:AdvisorEmulateRuntime)
{
$oAPI = new-object -comObject "MOM.ScriptAPI"
}


$discovery = New-Object PSObject
$discovery | Add-Member -MemberType NoteProperty -Name "_logFile" -Value $null
$discovery | Add-Member -MemberType NoteProperty -Name "_scomAPI" -Value $oAPI
$discovery | Add-Member -MemberType NoteProperty -Name "_discoveryData" -Value $null
$discovery | Add-Member -MemberType NoteProperty -Name "ParentClass" -Value $null
$discovery | Add-Member -MemberType NoteProperty -Name "Classes" -Value @()
$discovery | Add-Member -MemberType ScriptMethod -Name "AddInstance" -Value {
$o = $args[0]
$this.WriteDiscoveryDebugLog( "-Instance Added-", $o )
$this.Classes += $o
}
$discovery | Add-Member -MemberType ScriptMethod -Name "WriteEventLog" -Value {
$text = $args[0]
$eventId = $args[1]
$sev = $args[2]
if (($sev -eq $null) -or ($sev -gt 4)) { $sev = 0 }
if (($eventId -eq $null) -or ($eventId -lt 0) -or ($eventId -gt 20000)) { $eventId = 19000 }
if ($global:AdvisorEmulateRuntime)
{
Write-Debug "[Advisor Script(EventLog): Microsoft.KnowledgeServices.SharePoint.Foundation.2010.SPServiceInstance.IncomingMail] ""$text"" $sev $eventId"
}
else
{
$this._scomAPI.LogScriptEvent("Advisor Script: Microsoft.KnowledgeServices.SharePoint.Foundation.2010.SPServiceInstance.IncomingMail", $eventId, $sev, $text)
}
}
$discovery | Add-Member -MemberType ScriptMethod -Name "WriteDiscoveryDebugLog" -Value {
$u = [Environment]::UserDomainName + "\" + [Environment]::UserName
$date = Get-Date
$header = "$date`tMicrosoft.KnowledgeServices.SharePoint.Foundation.2010.SPServiceInstance.IncomingMail`t$u"
if (($args -ne $null) -and ($args.Count -gt 0) -and ($args[0] -is [string]))
{
$header += "`t" + $args[0]
$args = @($args | Select-Object -Last ($args.Count-1))
}
$output = @($header, $args)
$output | Out-String -Width 256 | Write-Debug
if ($this._logFile -ne $null)
{
$output | Out-String | Add-Content -Path $this._logFile
}
}
$discovery | Add-Member -MemberType ScriptMethod -Name "CreateClassInstance" -Value {
return AdvisorCreateClassInstance "Microsoft.KnowledgeServices.SharePoint.Foundation.2010.SPServiceInstance.IncomingMail"
}

$discoveryDebugFilePath = Join-Path $Env:ProgramFiles "\System Center Advisor\discovery.txt"
if (Test-Path $discoveryDebugFilePath)
{
$discovery._logFile = $discoveryDebugFilePath
}
del variable:\discoveryDebugFilePath -force &#x2013;ErrorAction SilentlyContinue

$discovery | Add-Member -MemberType ScriptMethod -Name "CreateDiscoveryData" -Value {
if ($global:AdvisorEmulateRuntime)
{
$i = 0
foreach ($o in $this.Classes)
{
$i = $i + 1
$this.WriteDiscoveryDebugLog("Discovered Class $i", $o )
}
}
else
{
$this._discoveryData = $this._scomAPI.CreateDiscoveryData(0, $scriptargs.SourceId, $scriptargs.ManagedEntityId)
foreach ($o in $this.Classes)
{
$instance = $null
switch ($o.ClassName)
{
"Microsoft.KnowledgeServices.SharePoint.Foundation.2010.SPServiceInstance.IncomingMail"
{
$instance = $this._discoveryData.CreateClassInstance("$MPElement[Name='MicrosoftKnowledgeServicesSharePointFoundation2010Library!Microsoft.KnowledgeServices.SharePoint.Foundation.2010.SPServiceInstance.IncomingMail']$")

_AdvisorAddProperty $instance "$MPElement[Name='MicrosoftKnowledgeServicesSharePointFoundation2010Library!Microsoft.KnowledgeServices.SharePoint.Foundation.2010.SPServiceInstance.IncomingMail']/Status$" $o.Status $false
_AdvisorAddProperty $instance "$MPElement[Name='MicrosoftKnowledgeServicesSharePointFoundation2010Library!Microsoft.KnowledgeServices.SharePoint.Foundation.2010.SPServiceInstance.IncomingMail']/NeedsUpgrade$" $o.NeedsUpgrade $false
_AdvisorAddProperty $instance "$MPElement[Name='MicrosoftKnowledgeServicesSharePointFoundation2010Library!Microsoft.KnowledgeServices.SharePoint.Foundation.2010.SPServiceInstance.IncomingMail']/GUID$" $o.GUID $false
_AdvisorAddProperty $instance "$MPElement[Name='MicrosoftKnowledgeServicesSharePointFoundation2010Library!Microsoft.KnowledgeServices.SharePoint.Foundation.2010.SPServiceInstance.IncomingMail']/ServiceStatus$" $o.ServiceStatus $false
_AdvisorAddProperty $instance "$MPElement[Name='MicrosoftKnowledgeServicesSharePointFoundation2010Library!Microsoft.KnowledgeServices.SharePoint.Foundation.2010.SPServiceInstance.IncomingMail']/SPVersion$" $o.SPVersion $false
_AdvisorAddProperty $instance "$MPElement[Name='MicrosoftKnowledgeServicesSharePointFoundation2010Library!Microsoft.KnowledgeServices.SharePoint.Foundation.2010.SPServiceInstance.IncomingMail']/SPMajorVersion$" $o.SPMajorVersion $false
_AdvisorAddProperty $instance "$MPElement[Name='MicrosoftKnowledgeServicesSharePointFoundation2010Library!Microsoft.KnowledgeServices.SharePoint.Foundation.2010.SPServiceInstance.IncomingMail']/FarmID$" $o.FarmID $false
_AdvisorAddProperty $instance "$MPElement[Name='MicrosoftKnowledgeServicesSharePointFoundation2010Library!Microsoft.KnowledgeServices.SharePoint.Foundation.2010.SPServiceInstance.IncomingMail']/HostServer$" $o.HostServer $false
_AdvisorAddProperty $instance "$MPElement[Name='MicrosoftKnowledgeServicesSharePointFoundation2010Library!Microsoft.KnowledgeServices.SharePoint.Foundation.2010.SPServiceInstance.IncomingMail']/TypeName$" $o.TypeName $false
_AdvisorAddProperty $instance "$MPElement[Name='MicrosoftKnowledgeServicesSharePointFoundation2010Library!Microsoft.KnowledgeServices.SharePoint.Foundation.2010.SPServiceInstance.IncomingMail']/PrincipalName$" $o.PrincipalName $false
_AdvisorAddProperty $instance "$MPElement[Name='System!System.Entity']/DisplayName$" $o.DisplayName $false
_AdvisorAddProperty $instance "$MPElement[Name='Windows!Microsoft.Windows.Computer']/PrincipalName$" $this.ParentClass.PrincipalName $true


}
default {Throw "Unsupported ClassName: " + $o.ClassName}
}
$this._discoveryData.AddInstance($instance)
}
return $this._discoveryData
}
}

function AdvisorCreateClassInstance([string] $className)
{
$o = $null
switch ($className)
{
"Microsoft.KnowledgeServices.SharePoint.Foundation.2010.SPServiceInstance.IncomingMail"
{
$o = New-Object PSObject
$o | Add-Member -MemberType NoteProperty -Name ClassName -Value $className
$o | Add-Member -MemberType NoteProperty -Name DisplayName -Value "IncomingMail"

$o | Add-Member -MemberType NoteProperty -Name "Status" -Value $null -Force
$o | Add-Member -MemberType NoteProperty -Name "NeedsUpgrade" -Value $null -Force
$o | Add-Member -MemberType NoteProperty -Name "GUID" -Value $null -Force
$o | Add-Member -MemberType NoteProperty -Name "ServiceStatus" -Value $null -Force
$o | Add-Member -MemberType NoteProperty -Name "SPVersion" -Value $null -Force
$o | Add-Member -MemberType NoteProperty -Name "SPMajorVersion" -Value $null -Force
$o | Add-Member -MemberType NoteProperty -Name "FarmID" -Value $null -Force
$o | Add-Member -MemberType NoteProperty -Name "HostServer" -Value $null -Force
$o | Add-Member -MemberType NoteProperty -Name "TypeName" -Value $null -Force
$o | Add-Member -MemberType NoteProperty -Name "PrincipalName" -Value $null -Force


}
default {Throw "Unsupported ClassName: " + $className}
}
return $o
}



$current = _AdvisorCreateBaseObject "Microsoft.Windows.Computer" ""
$current | Add-Member -MemberType NoteProperty -Name "PrincipalName" -Value "$Target/Property[Type="Windows!Microsoft.Windows.Computer"]/PrincipalName$" -Force
$current | Add-Member -MemberType NoteProperty -Name "DNSName" -Value "$Target/Property[Type="Windows!Microsoft.Windows.Computer"]/DNSName$" -Force
$current | Add-Member -MemberType NoteProperty -Name "NetbiosComputerName" -Value "$Target/Property[Type="Windows!Microsoft.Windows.Computer"]/NetbiosComputerName$" -Force
$current | Add-Member -MemberType NoteProperty -Name "NetbiosDomainName" -Value "$Target/Property[Type="Windows!Microsoft.Windows.Computer"]/NetbiosDomainName$" -Force
$current | Add-Member -MemberType NoteProperty -Name "IPAddress" -Value "$Target/Property[Type="Windows!Microsoft.Windows.Computer"]/IPAddress$" -Force
$current | Add-Member -MemberType NoteProperty -Name "NetworkName" -Value "$Target/Property[Type="Windows!Microsoft.Windows.Computer"]/NetworkName$" -Force
$current | Add-Member -MemberType NoteProperty -Name "ActiveDirectoryObjectSid" -Value "$Target/Property[Type="Windows!Microsoft.Windows.Computer"]/ActiveDirectoryObjectSid$" -Force
$current | Add-Member -MemberType NoteProperty -Name "IsVirtualMachine" -Value $null -Force;$current.IsVirtualMachine = _AdvisorGetTypedValue "Boolean" "$Target/Property[Type="Windows!Microsoft.Windows.Computer"]/IsVirtualMachine$"
$current | Add-Member -MemberType NoteProperty -Name "DomainDnsName" -Value "$Target/Property[Type="Windows!Microsoft.Windows.Computer"]/DomainDnsName$" -Force
$current | Add-Member -MemberType NoteProperty -Name "OrganizationalUnit" -Value "$Target/Property[Type="Windows!Microsoft.Windows.Computer"]/OrganizationalUnit$" -Force
$current | Add-Member -MemberType NoteProperty -Name "ForestDnsName" -Value "$Target/Property[Type="Windows!Microsoft.Windows.Computer"]/ForestDnsName$" -Force
$current | Add-Member -MemberType NoteProperty -Name "ActiveDirectorySite" -Value "$Target/Property[Type="Windows!Microsoft.Windows.Computer"]/ActiveDirectorySite$" -Force
$current | Add-Member -MemberType NoteProperty -Name "LogicalProcessors" -Value $null -Force;$current.LogicalProcessors = _AdvisorGetTypedValue "Integer" "$Target/Property[Type="Windows!Microsoft.Windows.Computer"]/LogicalProcessors$"
$current | Add-Member -MemberType NoteProperty -Name "DisplayName" -Value "$Target/Property[Type="System!System.Entity"]/DisplayName$" -Force

$discovery.ParentClass = $current


[ScriptBlock]$mainScriptBlock = {
PARAM($InvokeParameter)




function Handle-Errors([string]$ErrMsg)
{
if (! $error)
{
return;
}
$ErrorString = $ErrMsg;

$EventLog_Format = "`nCurrent PID: {0}`nInvocationInfo.MyCommand: {1}`nInvocationInfo.ScriptLineNumber: {2}`nInvocationInfo.OffsetInLine: {3}`nInvocationInfo.ScriptName: {4}`nInvocationInfo.Line: {5}`nInvocationInfo.PositionMessage: {6}`nInvocationInfo.InvocationName: {7}`nInvocationInfo.PipelineLength: {8}`nInvocationInfo.PipelinePosition: {9}`n";

$EventLog_Template = "[{0} in Advisor script] {1} {2}";
$Exception = $error[$error.Count - 1];
if ($Exception.InvocationInfo -ne $null)
{
$ErrorString += ($EventLog_Format -f $pid, $Exception.InvocationInfo.MyCommand,
$Exception.InvocationInfo.ScriptLineNumber, $Exception.InvocationInfo.OffsetInLine, $Exception.InvocationInfo.ScriptName,
$Exception.InvocationInfo.Line, $Exception.InvocationInfo.PositionMessage, $Exception.InvocationInfo.InvocationName,
$Exception.InvocationInfo.PipelineLength, $Exception.InvocationInfo.PipelinePosition);
}

if ($null -ne $Exception.Exception)
{
$Exception = $Exception.Exception;
}
$InnerException = $Exception;
if ($null -ne $Exception.InnerException)
{
$InnerException = $Exception.InnerException;
}

$EventObject = New-Object Diagnostics.EventLog;
$EventObject.Source = "Operations Manager"
$EventObject.WriteEntry(($EventLog_Template -f $InnerException.GetType().FullName, $ErrorString, $Exception.Message), [System.Diagnostics.EventLogEntryType]::Error, 8000);
$error.Clear();
}

Function Get-SharepointInstallation
{
$registryKey2010 = "HKLM:\SOFTWARE\Microsoft\Shared Tools\Web Server Extensions\14.0"
$registryKey2013 = "HKLM:\SOFTWARE\Microsoft\Shared Tools\Web Server Extensions\15.0"
if ((Test-Path -Path $registryKey2010) -or (Test-Path -Path $registryKey2013))
{
if ((Get-ItemProperty -Path $registryKey2010 -ErrorAction SilentlyContinue).Sharepoint -eq "Installed" -or `
(Get-ItemProperty -Path $registryKey2013 -ErrorAction SilentlyContinue).Sharepoint -eq "Installed")
{
return $true
}
}
return $false
}

Function IsSharePointEnvironmentPrepared
{
$isPrepared = $true


try
{
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
}
catch
{
$isPrepared = $false
}

if ($null -eq [Microsoft.SharePoint.Administration.SPServer]::Local)
{
$isPrepared = $false
}
$error.Clear()
return $isPrepared
}

function Get-ServiceStatus([string]$ServiceName){
$status = ""
try{
$service = Get-Service $ServiceName -ErrorAction SilentlyContinue

if ( ! $service )
{
$status = "Missing"
}
else
{
$status = $service.Status.ToString()
}
}catch{
$Error.Clear()
}
return $status
}





function SPServiceInstanceIncomingMailDiscovery()
{
$global:advisorDiscoveredObjects = New-Object psobject
$global:advisorDiscoveredObjects | Add-Member -MemberType NoteProperty -Name "Status" -Value ""
$global:advisorDiscoveredObjects | Add-Member -MemberType NoteProperty -Name "NeedsUpgrade" -Value $false
$global:advisorDiscoveredObjects | Add-Member -MemberType NoteProperty -Name "GUID" -Value ""
$global:advisorDiscoveredObjects | Add-Member -MemberType NoteProperty -Name "ServiceStatus" -Value ""
$global:advisorDiscoveredObjects | Add-Member -MemberType NoteProperty -Name "SPVersion" -Value ""
$global:advisorDiscoveredObjects | Add-Member -MemberType NoteProperty -Name "SPMajorVersion" -Value ""
$global:advisorDiscoveredObjects | Add-Member -MemberType NoteProperty -Name "FarmID" -Value ""
$global:advisorDiscoveredObjects | Add-Member -MemberType NoteProperty -Name "HostServer" -Value ""
$global:advisorDiscoveredObjects | Add-Member -MemberType NoteProperty -Name "TypeName" -Value ""
$global:advisorDiscoveredObjects | Add-Member -MemberType NoteProperty -Name "PrincipalName" -Value ""

Try
{
if ((IsSharePointEnvironmentPrepared) -eq $true)
{
$SPFarm = [Microsoft.SharePoint.Administration.SPFarm]::Local
$global:advisorDiscoveredObjects.SPVersion = $SPFarm.BuildVersion.ToString()
$global:advisorDiscoveredObjects.SPMajorVersion = $SPFarm.BuildVersion.Major

$LocalServer = [Microsoft.SharePoint.Administration.SPServer]::Local;
$IncomingMailService = $LocalServer.ServiceInstances | Where-Object -FilterScript {$_.TypeName -eq "Microsoft SharePoint Foundation Incoming E-Mail"}
$global:advisorDiscoveredObjects.Status = $IncomingMailService.Status.ToString()
$global:advisorDiscoveredObjects.NeedsUpgrade = $IncomingMailService.NeedsUpgrade
$global:advisorDiscoveredObjects.GUID = $IncomingMailService.ID.ToString()
$global:advisorDiscoveredObjects.ServiceStatus = Get-ServiceStatus "SMTPSVC"

$global:advisorDiscoveredObjects.FarmID = $LocalServer.Farm.Id.ToString()
$global:advisorDiscoveredObjects.HostServer = $LocalServer.Name
$global:advisorDiscoveredObjects.TypeName = $IncomingMailService.GetType().FullName;
$global:advisorDiscoveredObjects.PrincipalName = "$InvokeParameter"
}
}

Catch
{
Handle-Errors "SPServiceInstanceIncomingMailExtention"
}

}
if (Get-SharePointInstallation -eq $true)
{
SPServiceInstanceIncomingMailDiscovery
}

}

Function Test-NeedPSRemoting
{
$script:SharePointPSVer = 0
$registryKey2010 = "HKLM:\SOFTWARE\Microsoft\Shared Tools\Web Server Extensions\14.0"
$registryKey2013 = "HKLM:\SOFTWARE\Microsoft\Shared Tools\Web Server Extensions\15.0"

if (Test-Path -Path $registryKey2010)
{
if ((Get-ItemProperty -Path $registryKey2010 -ErrorAction SilentlyContinue).Sharepoint -eq "Installed")
{
$script:SharePointPSVer = 2010
}
}
if (Test-Path -Path $registryKey2013)
{
if ((Get-ItemProperty -Path $registryKey2013 -ErrorAction SilentlyContinue).Sharepoint -eq "Installed")
{
$script:SharePointPSVer = 2013
}
}

$PSCLRVersion = [System.String]::Format("{0}.{1}", $PSVersionTable.CLRVersion.Major, $PSVersionTable.CLRVersion.Minor)
if (($PSCLRVersion -ieq "2.0") -and ($script:SharePointPSVer -ieq 2013))
{
return $true
}
return $false
}

if ((Test-NeedPSRemoting))
{
[bool]$script:psRemotingEnabled = $false

$winRMService = (Get-Service WinRM -ErrorAction SilentlyContinue)
if ($winRMService -ne $null)
{
if ($winRMService.Status -ieq [System.ServiceProcess.ServiceControllerStatus]::Stopped)
{
$winRMService.Start()
$winRMService.WaitForStatus([System.ServiceProcess.ServiceControllerStatus]::Running, [System.TimeSpan]::FromSeconds(15))
}
$psSessionConfiguration = (Get-PSSessionConfiguration -Name "Microsoft.PowerShell" -ErrorAction SilentlyContinue)
if ($psSessionConfiguration.Permission.Contains("BUILTIN\Administrators AccessAllowed"))
{
winrm quickconfig -quiet
$script:psRemotingEnabled = $true
}
else
{
Enable-PSRemoting -Force
winrm quickconfig -quiet
$script:psRemotingEnabled = $true
Set-Item WSMan:\localhost\Shell\MaxShellsPerUser 50
}
}


if ($script:psRemotingEnabled -eq $true)
{
$session = New-PSSession
Invoke-Command -Session $session -ScriptBlock $mainScriptBlock -ArgumentList @($discovery.ParentClass.PrincipalName)
$global:advisorDiscoveredObjects = Invoke-Command -Session $session -ScriptBlock {$global:advisorDiscoveredObjects}
Remove-PSSession -Session $session
}
}
else
{
&amp; $mainScriptBlock $discovery.ParentClass.PrincipalName
}

Function AddDiscoveryInstance($discoveredObjects)
{
foreach($item in $discoveredObjects)
{
$SPServiceInstanceIncomingMail = $discovery.CreateClassInstance()
$SPServiceInstanceIncomingMail.Status = $item.Status
$SPServiceInstanceIncomingMail.NeedsUpgrade = $item.NeedsUpgrade
$SPServiceInstanceIncomingMail.GUID = $item.GUID
$SPServiceInstanceIncomingMail.ServiceStatus = $item.ServiceStatus
$SPServiceInstanceIncomingMail.SPVersion = $item.SPVersion
$SPServiceInstanceIncomingMail.SPMajorVersion = $item.SPMajorVersion
$SPServiceInstanceIncomingMail.FarmID = $item.FarmID
$SPServiceInstanceIncomingMail.HostServer = $item.HostServer
$SPServiceInstanceIncomingMail.TypeName = $item.TypeName
$SPServiceInstanceIncomingMail.PrincipalName = $item.PrincipalName
$discovery.AddInstance( $SPServiceInstanceIncomingMail )
}
}

if ($global:advisorDiscoveredObjects -ne $null)
{
AddDiscoveryInstance $global:advisorDiscoveredObjects
}

$discoverydata = $discovery.CreateDiscoveryData()
$discoverydata

</Script></ScriptBody>
<Parameters>
<Parameter>
<Name>SourceId</Name>
<Value>$MPElement$</Value>
</Parameter>
<Parameter>
<Name>ManagedEntityId</Name>
<Value>$Target/Id$</Value>
</Parameter>
</Parameters>
<TimeoutSeconds>300</TimeoutSeconds>
<StrictErrorHandling>false</StrictErrorHandling>
</DataSource>
</Discovery>