Solved

SCCM 2012 Users to Computer collection import vbs

Posted on 2014-01-19
2
1,097 Views
Last Modified: 2014-02-05
Hi,
i have used the attached VBS to do a very simple task, i created an empty computer collection in SCCM, added a text file of users from active directory, ran the script and it would check the last computer login of each user in the last and add it to the collection.
This script ran well in a completely different domain environment and with SCCM 2007, now i have a new domain with SCCM2012, and when i ran it i always get "User not found".
anyone have any idea on what i need to do to modify the script\my environment so it will work?

on error resume next
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Const TristateUseDefault = -2

Set fso = CreateObject("Scripting.FileSystemObject")
Set file = fso.OpenTextFile("Test.txt", ForReading, True, TristateUseDefault) 

Do While file.AtEndOfStream <> True

 newname= "Test" 
 comname = file.readline

    Dim lLocator 
    Set lLocator = CreateObject("WbemScripting.SWbemLocator") 
    Dim gService 
    Set gService = lLocator.ConnectServer(, "root\sms\SERVER_CODE")

  		ResID = 0
        Set Computers = gService.ExecQuery("Select * From SMS_R_system WHERE LastLogonUserName LIKE ""%" + comname + "%""") 
        For Each Comp In Computers
            If UCase(comp.LastLogonUserName) = UCase(comname) Then ResID = comp.ResourceID 
        Next 
        If ResID = 0 Then WScript.echo "not found:" + comname
        Dim CollectionRule 
        Set CollectionRule = gService.Get("SMS_CollectionRuleDirect").SpawnInstance_() 
        CollectionRule.ResourceClassName = "SMS_R_System" 
        CollectionRule.RuleName = "ResourceID=" & ResID 
        CollectionRule.ResourceID = ResID 


        Dim oCollectionSet 
        Dim oCollection 
        Set oCollectionSet = gService.ExecQuery("Select * From SMS_Collection") 
        For Each oCollection In oCollectionSet 
           ' WScript.Echo oCollection.Name 
            If oCollection.Name = newname Then 
                oCollection.AddMembershipRule CollectionRule 
                If Err.Number = 0 Then 
                    Wscript.Echo comname + " were added to the " + oCollection.Name + " collection!" 
                End If 
            End If 
        Next


Loop

file.close

Open in new window


Thanks in advance.
0
Comment
Question by:johnnyjonathan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 65

Accepted Solution

by:
RobSampson earned 500 total points
ID: 39795418
Hi, firstly, comment out On Error Resume Next, and run the script manually to see what error you get.

If that shows no error, run this to compare the results to what you are looking for.

Set lLocator = CreateObject("WbemScripting.SWbemLocator") 
Set gService = lLocator.ConnectServer(, "root\sms\SERVER_CODE")
Set Computers = gService.ExecQuery("Select * From SMS_R_system") 
For Each comp In Computers
	WScript.Echo "LastLogonUserName: " & comp.LastLogonUserName & vbTab & "| ResourceID: " & comp.ResourceID 
Next

Open in new window


Regards,

Rob.
0
 

Author Comment

by:johnnyjonathan
ID: 39835420
Rob, thank you very much for the info, my foolish mistake, forgot to add "Site_" in line #16
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article runs through the process of deploying a single EXE application selectively to a group of user.
This article outlines the process to identify and resolve account lockout in an Active Directory environment.
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles from a Windows Server 2008 domain controller to a Windows Server 2012 domain controlle…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question