Friday, May 14, 2010

Discovering the presence of a database in a System Center Powershell script

I’m pretty sure there’s a better way to do this using the native SQL Server management pack but this worked for me (peppered with debugging statements). It looks for a database called staging and if it finds it, it returns the discovery information back.

      <Discovery ID="B.Staging.DiscoverProcessingComponent" Enabled="true" Target="B.Staging.ComputerRole" ConfirmDelivery="true" Remotable="true" Priority="Normal">
<
Category>Discovery</Category>
<
DiscoveryTypes>
<
DiscoveryClass TypeID="B.Staging.ProcessingComponent" />
</
DiscoveryTypes>
<
DataSource ID="PSScript" TypeID="Windows!Microsoft.Windows.TimedPowerShell.DiscoveryProvider">
<
IntervalSeconds>180</IntervalSeconds>
<
SyncTime />
<
ScriptName>DiscoverBStagingProcessingComponent.ps1</ScriptName>
<
ScriptBody><![CDATA[
param($sourceId, $managedEntityId, $computerName)

$api = New-Object -comObject 'MOM.ScriptAPI'
$api.LogScriptEvent("Processing discovery",101,2,"Created MOM.ScriptAPI with param sourceId = $sourceId , managedEntityId = $managedEntityId , computerName = $computerName ")

$discoveryData = $api.CreateDiscoveryData(0, $sourceId, $managedEntityId)
$api.LogScriptEvent("Processing discovery",101,2,"Executed CreateDiscoveryData")

[system.reflection.assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO') | out-null

$s=new-object('Microsoft.SqlServer.Management.Smo.Server') $computerName
$dbs=$s.databases
$dbs | get-member -membertype property
$staging = ($dbs | select name | where {$_.name -eq 'staging'})."Name"
$api.LogScriptEvent("Processing discovery",101,2,"Found anything: $staging")

$instance = $discoveryData.CreateClassInstance("$MPElement[Name='B.Staging.ProcessingComponent']$")
$api.LogScriptEvent("Processing discovery",101,2,"Executed CreateClassInstance")

if ($staging -eq 'staging') {
$instance.AddProperty("$MPElement[Name='Windows!Microsoft.Windows.Computer']/PrincipalName$", $computerName)
$instance.AddProperty("$MPElement[Name='B.Staging.ProcessingComponent']/B.Staging.ProcessingComponentKey$", "staging")

$api.LogScriptEvent("Processing discovery",101,2,"In the staging branch")

} else {
$instance.AddProperty("$MPElement[Name='Windows!Microsoft.Windows.Computer']/PrincipalName$", "")
$instance.AddProperty("$MPElement[Name='B.Staging.ProcessingComponent']/B.Staging.ProcessingComponentKey$", "")

$api.LogScriptEvent("Processing discovery",101,2,"In the not-staging branch")

}

$api.LogScriptEvent("Processing discovery",101,2,"About to AddInstance")

$discoveryData.AddInstance($instance)

$api.LogScriptEvent("Processing discovery",101,2,"About to return discoveryData")

$discoveryData

]]></ScriptBody>
<
Parameters>
<
Parameter>
<
Name>sourceID</Name>
<
Value>$MPElement$</Value>
</
Parameter>
<
Parameter>
<
Name>managedEntityID</Name>
<
Value>$Target/Id$</Value>
</
Parameter>
<
Parameter>
<
Name>computerName</Name>
<
Value>$Target/Host/Property[Type="Windows!Microsoft.Windows.Computer"]/PrincipalName$</Value>
</
Parameter>
</
Parameters>
<
TimeoutSeconds>300</TimeoutSeconds>
</
DataSource>
</
Discovery>

No comments: