TSManager.exe being detected erroneously

Topics: Archive - General
Oct 28, 2014 at 12:59 AM
Edited Oct 28, 2014 at 12:59 AM
For some reason (which we are still investigating) the tsmanager.exe is hanging around while application enforcement starts.

I looked at the code that detects this and noticed it just checks for the exe in the process list. What happens is the interactive install runs silently (so it kills off any apps the user is using without warning and proceeds to do the upgrade).

A better way to check if you are in a task sequence or not would be to try to load the "Microsoft.SMS.TSEnvironment" com object - if it fails to load, then you know your not in a TS.

The way this works is that com object can only be spawned by the same process ID that tsmanager.exe is running as - so even if tsmanager.exe is running for whatever reason - if you can't load it - your not in a TS (it would be safe to assume). Here's a sample:
$smsTSEnv = New-Object -COMObject Microsoft.SMS.TSEnvironment -ErrorAction SilentlyContinue

if ($? -eq $false)
{
    Write-Log "Error: Failed to load SMS TS COM Object.  Are you running this from an SMS Task Sequence?"
    $runningTaskSequence = $false
} 
Oct 28, 2014 at 6:19 PM
Here's what I ended up writing inside toolkitman:
ElseIf ($(New-Object -COMObject Microsoft.SMS.TSEnvironment -ErrorAction SilentlyContinue) -ne $null)
    {
    Write-Log "Running in SCCM Task Sequence."
    $runningTaskSequence = $true
    $sessionZero = $true
    }
Developer
Oct 31, 2014 at 11:26 AM
Thanks for the fix. I found that it would still throw an error on some machines even with ErrorAction set to SilentlyContinue. Therefore, I modified your code a bit to get it working without displaying an error.
Try {
    [__ComObject]$SMSTSEnvironment = New-Object -ComObject Microsoft.SMS.TSEnvironment -ErrorAction 'SilentlyContinue' -ErrorVariable SMSTSEnvironmentErr
}
Catch {
}
If ($SMSTSEnvironmentErr) {
    Write-Log -Message 'Unable to load ComObject [Microsoft.SMS.TSEnvironment]. Therefore, script is not currently running from an SCCM Task Sequence.'
    $runningTaskSequence = $false
}
ElseIf ($null -ne $SMSTSEnvironment) {
    Write-Log -Message 'Successfully loaded ComObject [Microsoft.SMS.TSEnvironment]. Therefore, script is currently running from an SCCM Task Sequence.'
    $runningTaskSequence = $true
}
Oct 31, 2014 at 5:10 PM
Yeah I noticed the same problem last night too - the fix I wrote isn't quite as elegant as yours :). Also - I thought the erroraction had to be set to stop to generate an exception? I'm kind of a powershell noob though.
    Try
        {
        If ($(New-Object -COMObject Microsoft.SMS.TSEnvironment -ErrorAction "Stop") -ne $null)
            {
            Write-Log "Running in SCCM Task Sequence."
            $runningTaskSequence = $true
            $sessionZero = $true
            }
        Else
            {
            Write-Log "Not running in a CM Task Seqeunce."
            }

        }
    Catch [Exception]
        {
        Write-Log "Not running in a CM Task Seqeunce."
        }
Developer
Oct 31, 2014 at 5:22 PM
Yes, it has to be set to Stop to generate a terminating exception. Otherwise, it just generates an exception and continues with code execution. There is a bug there which causes it to not honor the 'SilentlyContinue' because that should not generate any error. If you suppress errors with SilentlyContinue, you can still save the error to a variable using -ErrorVariable parameter so that is what I ended up doing.
Developer
Nov 4, 2014 at 1:08 AM
Thanks for this fix. It has been integrated into the toolkit with the latest 3.5.0 release.
Nov 4, 2014 at 1:13 AM
Cool - I really appreciate your help with this! :)