{"id":1989,"date":"2018-06-20T14:22:32","date_gmt":"2018-06-20T13:22:32","guid":{"rendered":"https:\/\/www.autoitconsulting.com\/site\/?p=1989"},"modified":"2025-07-26T12:51:05","modified_gmt":"2025-07-26T11:51:05","slug":"script-detect-running-sccm-task-sequence-avoid-false-positives","status":"publish","type":"post","link":"https:\/\/www.autoitconsulting.com\/site\/deployment\/script-detect-running-sccm-task-sequence-avoid-false-positives\/","title":{"rendered":"Script to Detect if Running from ConfigMgr Task Sequence and Avoid False Positives"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Overview<\/h2>\n\n\n\n<p>This post shows how a script can detect if it is running from inside a Microsoft Configuration Manager Task Sequence in a way that avoids false positives.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The Problem<\/h2>\n\n\n\n<p>At times it&#8217;s useful for a script to detect if it is running from a ConfigMgr Task Sequence so it can alter its behaviour accordingly. The most commonly used way to do this involves trying to create an instance of the COM object&nbsp;<strong>Microsoft.SMS.TSEnviroment<\/strong>. This COM object is temporarily registered during Task Sequence execution, and then it is unregistered on completion. So in a script if you try to create an instance of the COM object and you succeed then it can be assumed that the script is running inside a Task Sequence.<\/p>\n\n\n\n<p>Or so I thought&#8230;<\/p>\n\n\n\n<p>I was involved in a simple Package and Program script deployment recently and I noticed that in around 1% of cases my script was failing. I checked the script logs on the affected machines and found that they were crashing because they were detecting that they were in a ConfigMgr Task Sequence and invoking specific logic. But because they actually weren&#8217;t in a Task Sequence the script was crashing. I checked the script on the affected machines and saw that the script was successfully creating the&nbsp;<strong>Microsoft.SMS.TSEnvironment<\/strong> COM object and therefore assuming a Task Sequence.&nbsp;On further investigation of the machines I found that they all had a left over&nbsp;<strong>C:\\_SMSTaskSequence<\/strong> folder on the C: drive and that during an OSD build they not finished cleanly (unrelated issue).<\/p>\n\n\n\n<p>During a successful Task Sequence deployment a clean up task is run on completion. The&nbsp;<strong>smsts.log<\/strong> file shows certain folders and COM objects being unregistered, the log entries are as follows:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:10px 0px 0 16px;font-size:0.8em;width:100%;text-align:left;background-color:#1E1E1E;font-style:italic;color:#D4D4D4\"><span style=\"border-bottom:1px solid rgba(234, 191, 191, 0.2)\">Plaintext<\/span><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>Successfully unregistered Task Sequencing Environment COM Interface.\tTSManager\t19\/06\/2018 12:24:52\t2428 (0x097C)\nExecuting command line: \"C:\\WINDOWS\\CCM\\TsProgressUI.exe\" \/Unregister\tTSManager\t19\/06\/2018 12:24:52\t2428 (0x097C)\n==========&#91; TsProgressUI started in process 1044 &#93;==========\tTsProgressUI\t19\/06\/2018 12:24:52\t1040 (0x0410)\nCommand line: \"C:\\WINDOWS\\CCM\\TsProgressUI.exe\" \/Unregister\tTsProgressUI\t19\/06\/2018 12:24:52\t1040 (0x0410)\nUnregistering COM classes\tTsProgressUI\t19\/06\/2018 12:24:52\t1040 (0x0410)\nUnregistering class objects\tTsProgressUI\t19\/06\/2018 12:24:52\t1040 (0x0410)\nShutdown complete.\tTsProgressUI\t19\/06\/2018 12:24:52\t1040 (0x0410)\nProcess completed with exit code 0\tTSManager\t19\/06\/2018 12:24:52\t2428 (0x097C)\nSuccessfully unregistered TS Progress UI.\tTSManager\t19\/06\/2018 12:24:52\t2428 (0x097C)<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #D4D4D4\">Successfully unregistered Task Sequencing Environment COM Interface.\tTSManager\t19\/06\/2018 12:24:52\t2428 (0x097C)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">Executing command line: &quot;C:\\WINDOWS\\CCM\\TsProgressUI.exe&quot; \/Unregister\tTSManager\t19\/06\/2018 12:24:52\t2428 (0x097C)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">==========&#91; TsProgressUI started in process 1044 &#93;==========\tTsProgressUI\t19\/06\/2018 12:24:52\t1040 (0x0410)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">Command line: &quot;C:\\WINDOWS\\CCM\\TsProgressUI.exe&quot; \/Unregister\tTsProgressUI\t19\/06\/2018 12:24:52\t1040 (0x0410)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">Unregistering COM classes\tTsProgressUI\t19\/06\/2018 12:24:52\t1040 (0x0410)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">Unregistering class objects\tTsProgressUI\t19\/06\/2018 12:24:52\t1040 (0x0410)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">Shutdown complete.\tTsProgressUI\t19\/06\/2018 12:24:52\t1040 (0x0410)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">Process completed with exit code 0\tTSManager\t19\/06\/2018 12:24:52\t2428 (0x097C)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">Successfully unregistered TS Progress UI.\tTSManager\t19\/06\/2018 12:24:52\t2428 (0x097C)<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>If a Task Sequence fails to clean up successfully then you can be left with a left over&nbsp;<strong>C:\\_SMSTaskSequence<\/strong> folder and&nbsp;<strong>Microsoft.SMS.TSEnvironment<\/strong> COM object and these can confuse the usual method of detecting a Task Sequence.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The Solution<\/h2>\n\n\n\n<p>Although it would be best to avoid machines getting into this state in the first place it can be worked around. The workaround is as follows:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Attempt to create the COM object&nbsp;<strong>Microsoft.SMS.TSEnvironment<\/strong><\/li>\n\n\n\n<li>If successful, also attempt to read the Task Sequence variable&nbsp;<strong>_SMSTSType<\/strong><\/li>\n\n\n\n<li>If&nbsp;<strong>_SMSTSType<\/strong> is not blank then we are in a Task Sequence<\/li>\n<\/ul>\n\n\n\n<p><strong>_SMSTSType<\/strong> is set to&nbsp;<strong>1<\/strong> or<strong>&nbsp;2<\/strong> depending on if the current Task Sequence is an OSD or Custom type. If the value is blank then you are not in a Task Sequence.<\/p>\n\n\n\n<p>Script examples are shown below.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">VBScript<\/h3>\n\n\n\n<p>The code below shows the solution for VBScript:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:10px 0px 0 16px;font-size:0.8em;width:100%;text-align:left;background-color:#1E1E1E;font-style:italic;color:#D4D4D4\"><span style=\"border-bottom:1px solid rgba(234, 191, 191, 0.2)\">VB<\/span><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>'\n' Function to test if script is running from a Task Sequence. Will return True\/False.\n'\n\nFunction IsInTaskSequence()\n\n\t' False by default\n\tIsInTaskSequence = False\n\n\tOn Error Resume Next\n\tDim oTSEnv : Set oTSEnv = CreateObject(\"Microsoft.SMS.TSEnvironment\")\n\tIf Err.Number = 0 Then \n\t\t' May succeed because of a failed Task Sequence or bad clean-up of COM objects.\n\t\t' Double check by reading the Task Sequence type, if its blank - not in a Task Sequence\n\t\tIf oTSEnv(\"_SMSTSType\") &lt;> \"\" Then \n\t\t\tIsInTaskSequence = True\n\t\tEnd If\n\tEnd If\n\tOn Error Goto 0\n\nEnd Function<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #6A9955\">&#39;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #6A9955\">&#39; Function to test if script is running from a Task Sequence. Will return True\/False.<\/span><\/span>\n<span class=\"line\"><span style=\"color: #6A9955\">&#39;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #569CD6\">Function<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">IsInTaskSequence<\/span><span style=\"color: #D4D4D4\">()<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">\t<\/span><span style=\"color: #6A9955\">&#39; False by default<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">\t<\/span><span style=\"color: #9CDCFE\">IsInTaskSequence<\/span><span style=\"color: #D4D4D4\"> =<\/span><span style=\"color: #569CD6\"> False<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">\t<\/span><span style=\"color: #569CD6\">On Error Resume Next<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">\t<\/span><span style=\"color: #569CD6\">Dim<\/span><span style=\"color: #9CDCFE\"> oTSEnv <\/span><span style=\"color: #D4D4D4\">:<\/span><span style=\"color: #569CD6\"> Set <\/span><span style=\"color: #9CDCFE\">oTSEnv<\/span><span style=\"color: #D4D4D4\"> = <\/span><span style=\"color: #DCDCAA\">CreateObject<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #CE9178\">&quot;Microsoft.SMS.TSEnvironment&quot;<\/span><span style=\"color: #D4D4D4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">\t<\/span><span style=\"color: #C586C0\">If<\/span><span style=\"color: #D4D4D4\"> Err.<\/span><span style=\"color: #9CDCFE\">Number<\/span><span style=\"color: #D4D4D4\"> = <\/span><span style=\"color: #B5CEA8\">0<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #C586C0\">Then<\/span><span style=\"color: #D4D4D4\"> <\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">\t\t<\/span><span style=\"color: #6A9955\">&#39; May succeed because of a failed Task Sequence or bad clean-up of COM objects.<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">\t\t<\/span><span style=\"color: #6A9955\">&#39; Double check by reading the Task Sequence type, if its blank - not in a Task Sequence<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">\t\t<\/span><span style=\"color: #C586C0\">If<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">oTSEnv<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #CE9178\">&quot;_SMSTSType&quot;<\/span><span style=\"color: #D4D4D4\">) &lt;&gt; <\/span><span style=\"color: #CE9178\">&quot;&quot;<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #C586C0\">Then<\/span><span style=\"color: #D4D4D4\"> <\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">\t\t\t<\/span><span style=\"color: #9CDCFE\">IsInTaskSequence<\/span><span style=\"color: #D4D4D4\"> =<\/span><span style=\"color: #569CD6\"> True<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">\t\t<\/span><span style=\"color: #C586C0\">End If<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">\t<\/span><span style=\"color: #C586C0\">End If<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">\t<\/span><span style=\"color: #569CD6\">On Error Goto <\/span><span style=\"color: #B5CEA8\">0<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #569CD6\">End Function<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Cleaning Up Task Sequence Left Overs<\/h2>\n\n\n\n<p>If you want to clean-up a machine that has left over Task Sequence COM objects then one method is to advertise a new Task Sequence to it that does nothing except run a dummy command. It should clean itself up on completion.<\/p>\n\n\n\n<p>Alternatively if you want to manually fix up a machine in this state you can unregister the COM components as follows:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:10px 0px 0 16px;font-size:0.8em;width:100%;text-align:left;background-color:#1E1E1E;font-style:italic;color:#D4D4D4\"><span style=\"border-bottom:1px solid rgba(234, 191, 191, 0.2)\">BAT (Batchfile)<\/span><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>regsvr32.exe \/s \/u C:\\Windows\\CCM\\TSCore.dll\nC:\\Windows\\CCM\\TsProgressUI.exe \/Unregister<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #D4D4D4\">regsvr32.exe \/s \/u C:\\Windows\\CCM\\TSCore.dll<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">C:\\Windows\\CCM\\TsProgressUI.exe \/Unregister<\/span><\/span><\/code><\/pre><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Overview This post shows how a script can detect if it is running from inside a Microsoft Configuration Manager Task Sequence in a way that avoids false positives. The Problem At times it&#8217;s useful for a script to detect if it is running from a ConfigMgr Task Sequence so it can alter its behaviour accordingly. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":100234,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20],"tags":[59,62,91,68,65,55],"class_list":["post-1989","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-deployment","tag-configmgr","tag-deployment","tag-mecm","tag-sccm","tag-scripting","tag-vbscript"],"_links":{"self":[{"href":"https:\/\/www.autoitconsulting.com\/site\/wp-json\/wp\/v2\/posts\/1989","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.autoitconsulting.com\/site\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.autoitconsulting.com\/site\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.autoitconsulting.com\/site\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.autoitconsulting.com\/site\/wp-json\/wp\/v2\/comments?post=1989"}],"version-history":[{"count":14,"href":"https:\/\/www.autoitconsulting.com\/site\/wp-json\/wp\/v2\/posts\/1989\/revisions"}],"predecessor-version":[{"id":100242,"href":"https:\/\/www.autoitconsulting.com\/site\/wp-json\/wp\/v2\/posts\/1989\/revisions\/100242"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.autoitconsulting.com\/site\/wp-json\/wp\/v2\/media\/100234"}],"wp:attachment":[{"href":"https:\/\/www.autoitconsulting.com\/site\/wp-json\/wp\/v2\/media?parent=1989"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.autoitconsulting.com\/site\/wp-json\/wp\/v2\/categories?post=1989"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.autoitconsulting.com\/site\/wp-json\/wp\/v2\/tags?post=1989"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}