Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Script incorrectly detects that a Task Sequence is running #74

Open
d3-X-t3r opened this issue Feb 19, 2022 · 1 comment
Open

Script incorrectly detects that a Task Sequence is running #74

d3-X-t3r opened this issue Feb 19, 2022 · 1 comment

Comments

@d3-X-t3r
Copy link

d3-X-t3r commented Feb 19, 2022

We've come across an issue in our environment where several Windows 10 devices were failing to run the CMCH script. Upon investigating, it was found that the script (executed daily via a Scheduled Task) would run, but would abort with the error:

Configuration Manager Task Sequence detected on computer. Exiting script

If we manually run the code $tsenv = New-Object -COMObject Microsoft.SMS.TSEnvironment we can verify that the $tsenv variable indeed exists, however, none of these machines have a Task Sequence running, nor have one deployed to it.

The issue appears to be caused due to leftover or orphaned TSEnvironment COM registrations (which we suspect stemmed from the OS Upgrade we did last year), as a result, the COM object is always available to be instantiated even when there's no TS running.

Note that uninstalling and reinstalling the client does NOT get rid of the orphaned COM registrations, the only way to remove the leftovers is by deleting the relevant CLSID regkeys.Regasm.exe /u might also help, but I haven't tested this yet.

Whilst it would possible to manually de-register the COM object by deleting certain CLSID registry entries as part of the script, a simple workaround would be to ignore the COM object and simply check whether any of the known Task Sequence processes are active.

Therefore, I propose that we replace this piece of code:

    try { $tsenv = New-Object -COMObject Microsoft.SMS.TSEnvironment }
    catch { $tsenv = $null }

    if ($tsenv) {

With this:

    if (Get-Process -Name TSMBootStrap,TSManager,TsProgressUI,TSInstallSWUpdate -ErrorAction SilentlyContinue) {

Furthermore, I reckon we should add more executables to this list to detect scenarios where it would be undesirable to have CMCH running in the background, for example: Setup, SetupHost,mighost,TiWorker, DISM,DismHost, WindowsUpdateBox,WUSA, ccmsetup, ccmrepair etc. This will prevent CMCH from running in case there are any Windows / ConfigMgr upgrades etc taking place.

@vonpelz
Copy link

vonpelz commented Mar 13, 2024

https://www.autoitconsulting.com/site/deployment/script-detect-running-sccm-task-sequence-avoid-false-positives/

Using this blog post as source I modified the function:

Function Test-InTaskSequence {
      try { $tsenv = New-Object -COMObject Microsoft.SMS.TSEnvironment }
      catch { $tsenv = $null }
  
      if ($null -ne $tsenv) {
  
          if ($tsenv.Value("_SMSTSType") -eq "") {
  
              Write-Host "Orphaned TS COM object found, repairing."
              Start-Process -FilePath 'regsvr32.exe' -ArgumentList '/s /u C:\Windows\CCM\TSCore.dll' -Wait
              Start-Process -FilePath 'C:\Windows\CCM\TsProgressUI.exe' -ArgumentList '/Unregister' -Wait
  
          } else {
  
              Write-Host "Configuration Manager Task Sequence detected on computer. Exiting script"
              Exit 2
          }
      }            
 }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants