Job Discovery

VKernel.CapacityAnalyzer.JobDiscovery (Discovery)

Element properties:

TargetMicrosoft.SystemCenter.VirtualMachineManager.2012.VMMManagementServer
EnabledTrue
Frequency14400
RemotableFalse

Object Discovery Details:

Discovered Classes and their attribuets:
Discovered relationships and their attribuets:

Member Modules:

ID Module Type TypeId RunAs 
VKernel.CapacityAnalyzer.JobDiscoveryModule DataSource Microsoft.Windows.TimedPowerShell.DiscoveryProvider Default

Source Code:

<Discovery ID="VKernel.CapacityAnalyzer.JobDiscovery" Enabled="true" Target="SCVMM2012DiscoveryMP!Microsoft.SystemCenter.VirtualMachineManager.2012.VMMManagementServer" ConfirmDelivery="true" Remotable="false" Priority="Normal">
<Category>Discovery</Category>
<DiscoveryTypes>
<DiscoveryClass TypeID="VKernel.CapacityAnalyzer.Job">
<Property TypeID="VKernel.CapacityAnalyzer.Job" PropertyID="ID"/>
<Property TypeID="VKernel.CapacityAnalyzer.Job" PropertyID="Name"/>
<Property TypeID="VKernel.CapacityAnalyzer.Job" PropertyID="TargetObjectID"/>
<Property TypeID="VKernel.CapacityAnalyzer.Job" PropertyID="ResultObjectID"/>
<Property TypeID="VKernel.CapacityAnalyzer.Job" PropertyID="User"/>
<Property TypeID="VKernel.CapacityAnalyzer.Job" PropertyID="StartTime"/>
<Property TypeID="VKernel.CapacityAnalyzer.Job" PropertyID="EndTime"/>
<Property TypeID="VKernel.CapacityAnalyzer.Job" PropertyID="Source"/>
<Property TypeID="VKernel.CapacityAnalyzer.Job" PropertyID="Target"/>
</DiscoveryClass>
<DiscoveryClass TypeID="VKernel.CapacityAnalyzer.PropertyChange">
<Property TypeID="VKernel.CapacityAnalyzer.PropertyChange" PropertyID="ID"/>
<Property TypeID="VKernel.CapacityAnalyzer.PropertyChange" PropertyID="PropertyName"/>
<Property TypeID="VKernel.CapacityAnalyzer.PropertyChange" PropertyID="OldValue"/>
<Property TypeID="VKernel.CapacityAnalyzer.PropertyChange" PropertyID="NewValue"/>
</DiscoveryClass>
<DiscoveryRelationship TypeID="VKernel.CapacityAnalyzer.JobHostsPropertyChange"/>
</DiscoveryTypes>
<DataSource ID="VKernel.CapacityAnalyzer.JobDiscoveryModule" TypeID="Windows!Microsoft.Windows.TimedPowerShell.DiscoveryProvider">
<IntervalSeconds>14400</IntervalSeconds>
<SyncTime/>
<ScriptName>JobDiscovery.ps1</ScriptName>
<ScriptBody><Script>
$oAPI = new-object -comObject "MOM.ScriptAPI"
$SourceId = "$MPElement$"
$ManagedEntityId = "$Target/Id$"
$ComputerName = "$Target/Host/Property[Type="Windows!Microsoft.Windows.Computer"]/PrincipalName$"
$oDiscoveryData = $oAPI.CreateDiscoveryData(0, $SourceId, $ManagedEntityId)

Import-Module VirtualMachineManager

trap [Exception]
{
$oAPI.LogScriptEvent("JobDiscovery.ps1", 101, 2, $_.Exception.Message)
continue;
}

#Connecting to VMM Server
Get-VMMServer -ComputerName $ComputerName

$jobs = Get-Job -all |
where {"New-SCVirtualMachine", "Remove-SCVirtualMachine", "Set-SCVirtualMachine", "Move-SCVirtualMachine",
"Start-SCVirtualMachine", "Stop-SCVirtualMachine", "Shutdown-VM", "Suspend-SCVirtualMachine",
"Add-SCVMHost", "Remove-SCVMHost", "Add-SCVMHostCluster",
"Remove-SCVMHostCluster", "New-SCVMTemplate" -contains $_.CmdLetName}
if ($jobs -eq $null) {
$oAPI.LogScriptEvent("JobDiscovery.ps1", 101, 2, "Null object was returned by Get-Job cmdlet")
$jobs = @()
}
foreach ($job in $jobs)
{
if ($job.Status -eq "Failed")
{
# Ignore jobs that were not completed successfully
continue
}

$jobID = $job.ID.ToString()

# Add property changes if they exist
$jobDetails = Get-Job -Job $job -Full
$auditRecordsCount = 0
$auditRecordsExist = $jobDetails | select AreAuditRecordsAvailable
if ($auditRecordsExist)
{
foreach ($auditRecord in $jobDetails.AuditRecords)
{
$propName = $auditRecord.Name.ToString()
if ("ProcessorCount", "RAM", "Priority", "State", "Location", "Name" -notcontains $propName)
{
continue
}

if ($propName -eq "Name" -and $auditRecord.ObjectData.ObjectType.ToString() -ne "VirtualDiskDrive")
{
continue
}

$auditRecordsCount++

$oInst = $oDiscoveryData.CreateClassInstance("$MPElement[Name='VKernel.CapacityAnalyzer.PropertyChange']$")

$oInst.AddProperty("$MPElement[Name='Windows!Microsoft.Windows.Computer']/PrincipalName$", $ComputerName)
$oInst.AddProperty("$MPElement[Name='VKernel.CapacityAnalyzer.Job']/ID$", $jobID)

$oInst.AddProperty("$MPElement[Name='VKernel.CapacityAnalyzer.PropertyChange']/ID$", $auditRecord.ID.ToString())
$oInst.AddProperty("$MPElement[Name='VKernel.CapacityAnalyzer.PropertyChange']/PropertyName$", $propName)
if ($auditRecord.Previous.ValueParameter -ne $null)
{
$oInst.AddProperty("$MPElement[Name='VKernel.CapacityAnalyzer.PropertyChange']/OldValue$", $auditRecord.Previous.ValueParameter.ToString())
}
if ($auditRecord.New.ValueParameter -ne $null)
{
$oInst.AddProperty("$MPElement[Name='VKernel.CapacityAnalyzer.PropertyChange']/NewValue$", $auditRecord.New.ValueParameter.ToString())
}

$oInst.AddProperty("$MPElement[Name='System!System.Entity']/DisplayName$", $job.CmdLetName + " - " + $propName);

$oDiscoveryData.AddInstance($oInst)
}
}

# Some jobs are useless for us if they have no audit records. Filter them to decrease the size of discovery data.
if (($auditRecordsCount -gt 0) -or ("Set-SCVirtualMachine" -notcontains $job.CmdLetName))
{
if (($job.TargetObjectId -eq $null) -or ($job.ResultObjectID -eq $null)) {
# Add some logging here
continue
}

$oInst = $oDiscoveryData.CreateClassInstance("$MPElement[Name='VKernel.CapacityAnalyzer.Job']$")

$oInst.AddProperty("$MPElement[Name='Windows!Microsoft.Windows.Computer']/PrincipalName$", $ComputerName)

$oInst.AddProperty("$MPElement[Name='VKernel.CapacityAnalyzer.Job']/ID$", $jobID)
$oInst.AddProperty("$MPElement[Name='VKernel.CapacityAnalyzer.Job']/Name$", $job.CmdLetName)
$oInst.AddProperty("$MPElement[Name='VKernel.CapacityAnalyzer.Job']/TargetObjectID$", $job.TargetObjectID.ToString())
$oInst.AddProperty("$MPElement[Name='VKernel.CapacityAnalyzer.Job']/ResultObjectID$", $job.ResultObjectID.ToString())
$oInst.AddProperty("$MPElement[Name='VKernel.CapacityAnalyzer.Job']/User$", $job.Owner)
$oInst.AddProperty("$MPElement[Name='VKernel.CapacityAnalyzer.Job']/StartTime$", [System.TimeZoneInfo]::ConvertTimeToUtc($job.StartTime))
$oInst.AddProperty("$MPElement[Name='VKernel.CapacityAnalyzer.Job']/EndTime$", [System.TimeZoneInfo]::ConvertTimeToUtc($job.EndTime))
$oInst.AddProperty("$MPElement[Name='VKernel.CapacityAnalyzer.Job']/Source$", $job.Source)
$oInst.AddProperty("$MPElement[Name='VKernel.CapacityAnalyzer.Job']/Target$", $job.Target)

$oInst.AddProperty("$MPElement[Name='System!System.Entity']/DisplayName$", $job.CmdLetName);

$oDiscoveryData.AddInstance($oInst)
}
}

# Return the discovery data by calling the variable
$oDiscoveryData
</Script></ScriptBody>
<TimeoutSeconds>3600</TimeoutSeconds>
</DataSource>
</Discovery>