title: Query SCCM from Task Sequence - is it possible?
body: <p>I have a VBscript which works abolultuly fine on my workstation.  It queries SCCM to find out what domain a computer is in.  When I run it as part of the task sequence, it fails.  Note that this is almost the last step in the task sequence, under Windows 7, not Windows PE.</p>
  
  <pre><code>Option Explicit
  
  Const wbemFlagReturnImmediately = &amp;H10
  Const wbemFlagForwardOnly = &amp;H20
  
  Dim computerName, userName, userPassword, server
  Dim swbemLocator, swbemServices, providerLoc
  Dim location, connection
  Dim query, found, resource, resources
  
  '--- Settings ---
  userName = "domain\username"
  userPassword = "password"
  server = "domain.com"  
  
  '--- Get computer name ---
  computerName = CreateObject("WScript.Network").ComputerName
  WScript.Echo"Computer name: " &amp; computerName
  
  '--- Connect ----
  Set swbemLocator = CreateObject("WbemScripting.SWbemLocator")
  swbemLocator.Security_.AuthenticationLevel = 6
  Set swbemServices = swbemLocator.ConnectServer(server, "root\sms",userName,userPassword)
  Set providerLoc = swbemServices.InstancesOf("SMS_ProviderLocation")
  For Each location In providerLoc
      If location.ProviderForLocalSite = True Then
          WScript.Echo "SiteCode: " &amp; location.SiteCode       
          Set connection = swbemLocator.ConnectServer(server, "root\sms\site_" + location.SiteCode)
      Else
          WScript.Echo "Not provider for local site."
      End If
  Next
  
  '--- Query &amp; output ---
  query = "SELECT * FROM SMS_FullCollectionMembership WHERE name = '" &amp; computerName &amp; "'"
  Set resources = connection.ExecQuery(query, , wbemFlagForwardOnly Or wbemFlagReturnImmediately)
  found = False
  For Each resource In resources
      WScript.Echo "Domain: " &amp; resource.Domain &amp; " (" &amp; resource.CollectionID &amp; ")"
      found = True
      Exit For
  Next
  If Not found Then WScript.Echo "Computer not found!"
  </code></pre>
  
  <p>If I open a command prompt ruing the task sequence (using F8 under Windows 7, not Windows PE) and run the script, I get:</p>
  
  <blockquote>
    <p>SWbemLocator: Access is denied</p>
  </blockquote>
  
  <p>on the line:</p>
  
  <pre><code>Set connection = swbemLocator.ConnectServer(server, "root\sms\site_" + location.SiteCode)
  </code></pre>
  
  <p>Any thoughts?  Alternatively, any other suggestions on how to find out what domain a computer was in last time it was built?</p>