Collect IntelliTrace snapshot write action

Microsoft.SystemCenter.IntelliTraceProfiling.CollectIntelliTraceSnapshot.WriteAction (WriteActionModuleType)

Collect an IntelliTrace profiling snapshot for an IIS application pool

Element properties:

TypeWriteActionModuleType
IsolationAny
AccessibilityInternal
RunAsDefault
InputTypeSystem.BaseData

Member Modules:

ID Module Type TypeId RunAs 
PA ProbeAction Microsoft.SystemCenter.IntelliTraceProfiling.GetApplicationPool.ProbeActionType Default
WA2 WriteAction Microsoft.Windows.PowerShellPropertyBagWriteAction Default
WA3 WriteAction Microsoft.SystemCenter.AlertAttachment.SendAttachment.WriteAction Default

Overrideable Parameters:

IDParameterTypeSelectorDisplay NameDescription
IISApplicationPathstring$Config/IISApplicationPath$IIS Application PathIIS Application Path
AlertIDstring$Config/AlertID$Alert IDAlert ID
TimeoutSecondsint$Config/TimeoutSeconds$Timeout in secondsTimeout in seconds

Source Code:

<WriteActionModuleType ID="Microsoft.SystemCenter.IntelliTraceProfiling.CollectIntelliTraceSnapshot.WriteAction" Accessibility="Internal" Batching="false">
<Configuration>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="IISApplicationPath" type="xsd:string"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="AlertID" type="xsd:string"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="TimeoutSeconds" type="xsd:unsignedInt"/>
</Configuration>
<OverrideableParameters>
<OverrideableParameter ID="IISApplicationPath" Selector="$Config/IISApplicationPath$" ParameterType="string"/>
<OverrideableParameter ID="AlertID" Selector="$Config/AlertID$" ParameterType="string"/>
<OverrideableParameter ID="TimeoutSeconds" Selector="$Config/TimeoutSeconds$" ParameterType="int"/>
</OverrideableParameters>
<ModuleImplementation Isolation="Any">
<Composite>
<MemberModules>
<ProbeAction ID="PA" TypeID="Microsoft.SystemCenter.IntelliTraceProfiling.GetApplicationPool.ProbeActionType">
<IISApplicationPath>$Config/IISApplicationPath$</IISApplicationPath>
</ProbeAction>
<WriteAction ID="WA2" TypeID="Windows!Microsoft.Windows.PowerShellPropertyBagWriteAction">
<ScriptName>CollectIntelliTraceSnapshot.ps1</ScriptName>
<ScriptBody><Script>

param($intelliTraceInstallPath, $applicationPoolName, $alertID)

$ErrorActionPreference = "Stop"

Import-Module "$($intelliTraceInstallPath)\Microsoft.VisualStudio.IntelliTrace.PowerShell.dll"

$traceStatus = Get-IntelliTraceCollectionStatus | Where-Object {$_.OutputPath -ne $null -and $_.CollectionPlanPath -ne $null -and $_.ApplicationPool -eq $applicationPoolName} | Select-Object -First 1

if($traceStatus -eq $null)
{
throw "The IntelliTrace snapshot cannot be found. Make sure that the application pool is started and the task is running under an account that has sufficient privileges to access collected traces."
exit 1
}

$traceDirectory = [System.IO.Path]::GetDirectoryName($traceStatus.OutputPath)

try
{
$lock = [System.IO.File]::Open( [System.IO.Path]::Combine($traceDirectory, ".lock") , [System.IO.FileMode]::OpenOrCreate, [System.IO.FileAccess]::ReadWrite, [System.IO.FileShare]::None)
}
catch [System.IO.IOException]
{
throw "The IntelliTrace snapshot cannot be collected. Another collect snapshot operation is already running for this application pool."
exit 1
}

try
{
Checkpoint-IntelliTraceCollection $applicationPoolName -Confirm:$false

$snapshotDirectory = [System.IO.Path]::Combine($traceDirectory ,[System.Guid]::NewGuid().ToString())
[void] (New-Item $snapshotDirectory -type directory)

$traceFiles = @(Get-Item "$($traceDirectory)\*.iTrace")
$collectedTraces = @()

foreach ($file in $traceFiles)
{
try
{
$destination = [System.IO.Path]::Combine($snapshotDirectory, $file.Name)
[System.IO.File]::Move($file.FullName, $destination)
$collectedTraces = $collectedTraces + $destination
}
catch [System.IO.IOException]
{
# ignore files locked by IntelliTrace
}
}

if($collectedTraces.Count -eq 0)
{
throw "The IntelliTrace snapshot cannot be collected. Make sure that the application pool is running and that the task is running under an account that has sufficient rights to access collected traces."
exit 1
}

$machineName = [System.Environment]::MachineName

$oAPI = new-object -comObject "MOM.ScriptAPI"
$bag = $oAPI.CreatePropertyBag()
$bag.AddValue("AlertID", $alertID)
$bag.AddValue("AttachmentName", [System.String]::Format("{0}_{1}_{2}", $applicationPoolName, $machineName, [System.DateTime]::Now.ToString("yyyy-MM-dd HH.mm.ss")))
$bag.AddValue("AlertDescription", "A new IntelliTrace snapshot for application pool $($applicationPoolName) was received from computer $($machineName).")
$bag.AddValue("AttachmentType", "IntelliTrace")

for($i = 0; $i -lt $collectedTraces.Count; $i++)
{
if($i -eq 0)
{
$bag.AddValue("AttachmentPath", $collectedTraces[$i])
}
else
{
$bag.AddValue("AttachmentPath$($i)", $collectedTraces[$i])
}
}

$bag
}
finally
{
$lock.Close()
}

</Script></ScriptBody>
<Parameters>
<Parameter>
<Name>IntelliTraceInstallPath</Name>
<Value>$Data/Property[@Name='IntelliTraceInstallPath']$</Value>
</Parameter>
<Parameter>
<Name>ApplicationPoolName</Name>
<Value>$Data/Property[@Name='ApplicationPoolName']$</Value>
</Parameter>
<Parameter>
<Name>AlertID</Name>
<Value>$Config/AlertID$</Value>
</Parameter>
</Parameters>
<TimeoutSeconds>$Config/TimeoutSeconds$</TimeoutSeconds>
<StrictErrorHandling>true</StrictErrorHandling>
</WriteAction>
<WriteAction ID="WA3" TypeID="AA!Microsoft.SystemCenter.AlertAttachment.SendAttachment.WriteAction">
<DeleteLocalFile>true</DeleteLocalFile>
</WriteAction>
</MemberModules>
<Composition>
<Node ID="WA3">
<Node ID="WA2">
<Node ID="PA"/>
</Node>
</Node>
</Composition>
</Composite>
</ModuleImplementation>
<InputType>System!System.BaseData</InputType>
</WriteActionModuleType>