Microsoft.KnowledgeServices.SharePoint.Foundation.2010.SPFarm_Discovery

Microsoft.KnowledgeServices.SharePoint.Foundation.2010.SPFarm_Discovery (Discovery)

Discovery module for class Microsoft.KnowledgeServices.SharePoint.Foundation.2010.SPFarm

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.SPFarm_Discovery DataSource Microsoft.Windows.TimedPowerShell.DiscoveryProvider Microsoft.SharePoint.Foundation.2010.AdminAccount

Source Code:

<Discovery ID="Microsoft.KnowledgeServices.SharePoint.Foundation.2010.SPFarm_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.SPFarm">
<Property TypeID="MicrosoftKnowledgeServicesSharePointFoundation2010Library!Microsoft.KnowledgeServices.SharePoint.Foundation.2010.SPFarm" PropertyID="FarmID"/>
<Property TypeID="MicrosoftKnowledgeServicesSharePointFoundation2010Library!Microsoft.KnowledgeServices.SharePoint.Foundation.2010.SPFarm" PropertyID="FarmName"/>
<Property TypeID="MicrosoftKnowledgeServicesSharePointFoundation2010Library!Microsoft.KnowledgeServices.SharePoint.Foundation.2010.SPFarm" PropertyID="isSPDiagInstalled"/>
<Property TypeID="MicrosoftKnowledgeServicesSharePointFoundation2010Library!Microsoft.KnowledgeServices.SharePoint.Foundation.2010.SPFarm" PropertyID="SPVersion"/>
<Property TypeID="MicrosoftKnowledgeServicesSharePointFoundation2010Library!Microsoft.KnowledgeServices.SharePoint.Foundation.2010.SPFarm" PropertyID="SPMajorVersion"/>
</DiscoveryClass>
</DiscoveryTypes>
<DataSource ID="Microsoft.KnowledgeServices.SharePoint.Foundation.2010.SPFarm_Discovery" RunAs="SharePoint!Microsoft.SharePoint.Foundation.2010.AdminAccount" TypeID="Windows!Microsoft.Windows.TimedPowerShell.DiscoveryProvider">
<IntervalSeconds>14401</IntervalSeconds>
<SyncTime/>
<ScriptName>SPFarm.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.SPFarm] ""$text"" $sev $eventId"
}
else
{
$this._scomAPI.LogScriptEvent("Advisor Script: Microsoft.KnowledgeServices.SharePoint.Foundation.2010.SPFarm", $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.SPFarm`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.SPFarm"
}

$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.SPFarm"
{
$instance = $this._discoveryData.CreateClassInstance("$MPElement[Name='MicrosoftKnowledgeServicesSharePointFoundation2010Library!Microsoft.KnowledgeServices.SharePoint.Foundation.2010.SPFarm']$")

_AdvisorAddProperty $instance "$MPElement[Name='MicrosoftKnowledgeServicesSharePointFoundation2010Library!Microsoft.KnowledgeServices.SharePoint.Foundation.2010.SPFarm']/FarmID$" $o.FarmID $true
_AdvisorAddProperty $instance "$MPElement[Name='MicrosoftKnowledgeServicesSharePointFoundation2010Library!Microsoft.KnowledgeServices.SharePoint.Foundation.2010.SPFarm']/FarmName$" $o.FarmName $true
_AdvisorAddProperty $instance "$MPElement[Name='MicrosoftKnowledgeServicesSharePointFoundation2010Library!Microsoft.KnowledgeServices.SharePoint.Foundation.2010.SPFarm']/isSPDiagInstalled$" $o.isSPDiagInstalled $false
_AdvisorAddProperty $instance "$MPElement[Name='MicrosoftKnowledgeServicesSharePointFoundation2010Library!Microsoft.KnowledgeServices.SharePoint.Foundation.2010.SPFarm']/SPVersion$" $o.SPVersion $false
_AdvisorAddProperty $instance "$MPElement[Name='MicrosoftKnowledgeServicesSharePointFoundation2010Library!Microsoft.KnowledgeServices.SharePoint.Foundation.2010.SPFarm']/SPMajorVersion$" $o.SPMajorVersion $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.SPFarm"
{
$o = New-Object PSObject
$o | Add-Member -MemberType NoteProperty -Name ClassName -Value $className
$o | Add-Member -MemberType NoteProperty -Name DisplayName -Value "SharePoint Farm"

$o | Add-Member -MemberType NoteProperty -Name "FarmID" -Value $null -Force
$o | Add-Member -MemberType NoteProperty -Name "FarmName" -Value $null -Force
$o | Add-Member -MemberType NoteProperty -Name "isSPDiagInstalled" -Value $null -Force
$o | Add-Member -MemberType NoteProperty -Name "SPVersion" -Value $null -Force
$o | Add-Member -MemberType NoteProperty -Name "SPMajorVersion" -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 = {





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 IsSharePointExtensionInstalled
{
$isInstalled = $true


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

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 Get-CentralAdminServers
{
PARAM($FarmServers)

$serversRunningCentralAdmin = @()


foreach($server in $FarmServers)
{

[bool]$containsCAServiceInstance = ($server.ServiceInstances |
Where-Object {$_.Typename -eq "Central Administration"} |
Where-Object {$_.Status -eq "Online"}) -ne $null
if($containsCAServiceInstance)
{
if (($server | Get-Member -Name "TimeZone") -eq $null)
{
try{

$tz = Get-WmiObject -Class Win32_TimeZone -ComputerName $server.Address -ErrorAction SilentlyContinue
$TimeZone = $tz.Caption.Replace("GMT","UTC")
$server | Add-Member -MemberType NoteProperty -Name "TimeZone" -Value $TimeZone
}catch{
$server | Add-Member -MemberType NoteProperty -Name "TimeZone" -Value ""
}
$Error.Clear()
}

$serversRunningCentralAdmin += $server
}
}

return $serversRunningCentralAdmin
}



function FarmDiscovery()
{
$global:advisorDiscoveredObjects = New-Object psobject
$global:advisorDiscoveredObjects | Add-Member -MemberType NoteProperty -Name "FarmID" -Value ""
$global:advisorDiscoveredObjects | Add-Member -MemberType NoteProperty -Name "FarmName" -Value ""
$global:advisorDiscoveredObjects | Add-Member -MemberType NoteProperty -Name "isSpDiagInstalled" -Value $false
$global:advisorDiscoveredObjects | Add-Member -MemberType NoteProperty -Name "DisplayName" -Value ""
$global:advisorDiscoveredObjects | Add-Member -MemberType NoteProperty -Name "SPVersion" -Value ""
$global:advisorDiscoveredObjects | Add-Member -MemberType NoteProperty -Name "SPMajorVersion" -Value 0

Try
{

if ((IsSharePointExtensionInstalled) -eq $true)
{
$SPFarm = [Microsoft.SharePoint.Administration.SPFarm]::Local
}

if ($SPFarm -ne $null)
{
$ServersInFarm = $SPFarm.Servers
$SolutionsInFarm = $SPFarm.Solutions

$CentralAdminServers = @(Get-CentralAdminServers -FarmServers $ServersInFarm)


$caServers = @()
foreach ($caServer in $CentralAdminServers)
{

$caServers += $caServer.Name
}

$farmMaster = $caServers | Sort-Object | Select-Object -First 1



if ($farmMaster -eq [Environment]::MachineName)
{

$global:advisorDiscoveredObjects.isSPDiagInstalled = $false
foreach ($solution in $SolutionsInFarm)
{
if ($solution.Name -eq "extendeddiagnosticproviders.wsp")
{
$global:advisorDiscoveredObjects.isSPDiagInstalled = $true
}
}

$global:advisorDiscoveredObjects.FarmID = [string]$SPFarm.Id
$global:advisorDiscoveredObjects.FarmName = $SPFarm.Name
$global:advisorDiscoveredObjects.DisplayName = "SharePoint Farm - [" + $SPFarm.Name + "]"
$global:advisorDiscoveredObjects.SPVersion = $SPFarm.BuildVersion.ToString()
$global:advisorDiscoveredObjects.SPMajorVersion = $SPFarm.BuildVersion.Major
}
}
}
Catch
{
Handle-Errors "SPFarm"
}
}

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

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
$global:advisorDiscoveredObjects = Invoke-Command -Session $session -ScriptBlock {$global:advisorDiscoveredObjects}
Remove-PSSession -Session $session
}
}
else
{
&amp; $mainScriptBlock
}

Function AddDiscoveryInstance($discoveredObjects)
{
foreach($item in $discoveredObjects)
{
$farm = $discovery.CreateClassInstance()
$farm.FarmID = $item.FarmID
$farm.FarmName = $item.FarmName
$farm.isSpDiagInstalled = $item.isSpDiagInstalled
$farm.DisplayName = $item.DisplayName
$farm.SPVersion = $item.SPVersion
$farm.SPMajorVersion = $item.SPMajorVersion
$discovery.AddInstance( $farm )
}
}

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>