Start IntelliTrace write action

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

Start IntelliTrace profiling 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
CollectionPlanPathstring$Config/CollectionPlanPath$Collection PlanIntelliTrace profiling collection plan
CollectionDurationMinint$Config/CollectionDurationMin$Collection Duration (min)Collect a snapshot in a specified number of minutes.
NeedStopIntelliTracebool$Config/NeedStopIntelliTrace$Turn off IntelliTrace profiling after collecting snapshotTurn off IntelliTrace profiling after collecting a snapshot.
AlertIDstring$Config/AlertID$Alert IDOperations Manager alert to associate a snapshot with
TimeoutSecondsint$Config/TimeoutSeconds$Timeout in secondsTimeout in seconds

Source Code:

<WriteActionModuleType ID="Microsoft.SystemCenter.IntelliTraceProfiling.StartIntelliTrace.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="CollectionPlanPath" type="xsd:string"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="CollectionDurationMin" type="xsd:integer"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="NeedStopIntelliTrace" type="xsd:boolean"/>
<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="CollectionPlanPath" Selector="$Config/CollectionPlanPath$" ParameterType="string"/>
<OverrideableParameter ID="CollectionDurationMin" Selector="$Config/CollectionDurationMin$" ParameterType="int"/>
<OverrideableParameter ID="NeedStopIntelliTrace" Selector="$Config/NeedStopIntelliTrace$" ParameterType="bool"/>
<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>StartIntelliTrace.ps1</ScriptName>
<ScriptBody><Script>

param($intelliTraceInstallPath, $collectionPlanPath, $applicationPoolName, $applicationPoolIdentity, $collectionDurationMin, $needStop, $alertID)

$ErrorActionPreference = "Stop"

$collectionDuration = [System.Int32]::Parse($collectionDurationMin)

if($collectionDuration -gt 24*60)
{
throw "Collection duration cannot be greater than 24 hours."
exit 1
}

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

if(![System.IO.Path]::IsPathRooted($collectionPlanPath))
{
$collectionPlanPath = [System.IO.Path]::Combine($intelliTraceInstallPath, $collectionPlanPath)
}

$tempFolder = [System.IO.Path]::Combine($intelliTraceInstallPath, "traces")
$traceFolder = [System.IO.Path]::Combine($tempFolder, "IntelliTraceSnapshot-" + [System.Guid]::NewGuid().ToString())
[void] (New-Item $traceFolder -type directory)

try
{
$originalLocation = Get-Location

Set-Location $intelliTraceInstallPath
[void] (icacls "." /grant &#x201C;$($applicationPoolIdentity):`(RX`)" 2&gt;&amp;1)
Set-Location $traceFolder
[void] (icacls "." /grant &#x201C;$($applicationPoolIdentity):`(F`)" 2&gt;&amp;1)
Set-Location $originalLocation
Start-IntelliTraceCollection $applicationPoolName $collectionPlanPath $traceFolder -Confirm:$false
}
catch
{
try
{
Remove-Item $traceFolder -Force -Recurse
}
catch {}

throw
}

$bag = $null

if($collectionDuration -gt 0)
{
Start-Sleep -Seconds (60 * $collectionDuration)

$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("AttachmentName", [System.String]::Format("{0}_{1}_{2}", $applicationPoolName, $machineName, [System.DateTime]::Now.ToString("yyyy-MM-dd HH.mm.ss")))
$bag.AddValue("AlertID", $alertID)
$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])
}
}
}
finally
{
$lock.Close()
}
}

if(($collectionDuration -gt 0) -and [System.Convert]::ToBoolean($needStop))
{
try
{
Stop-IntelliTraceCollection $applicationPoolName -Confirm:$false
Start-Sleep -Seconds 5
}
finally
{
try
{
Remove-Item $traceFolder\*.* -Force
}
catch {}
}
}

if($bag -ne $null)
{
$bag
}

try
{
$emptyDirectoryTime = [System.DateTime]::UtcNow.AddHours(-24)
Get-ChildItem $tempFolder "IntelliTraceSnapshot-*" | Where-Object {$_.CreationTimeUtc -lt $emptyDirectoryTime -and $_.GetFiles("*.*", [System.IO.SearchOption]::AllDirectories).Count -eq 0} | Remove-Item -Force -Recurse
}
catch
{
#Ignore clean-up errors
}

</Script></ScriptBody>
<Parameters>
<Parameter>
<Name>IntelliTraceInstallPath</Name>
<Value>$Data/Property[@Name='IntelliTraceInstallPath']$</Value>
</Parameter>
<Parameter>
<Name>CollectionPlanPath</Name>
<Value>$Config/CollectionPlanPath$</Value>
</Parameter>
<Parameter>
<Name>ApplicationPoolName</Name>
<Value>$Data/Property[@Name='ApplicationPoolName']$</Value>
</Parameter>
<Parameter>
<Name>ApplicationPoolIdentity</Name>
<Value>$Data/Property[@Name='ApplicationPoolIdentity']$</Value>
</Parameter>
<Parameter>
<Name>CollectionDurationMin</Name>
<Value>$Config/CollectionDurationMin$</Value>
</Parameter>
<Parameter>
<Name>NeedStop</Name>
<Value>$Config/NeedStopIntelliTrace$</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>