Solved

SCCM 2012 Users to Computer collection import vbs

Posted on 2014-01-19
2
1,082 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
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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

This script will sweep a range of IP addresses (class c only, 255.255.255.0) and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
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 tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles to another domain controller. Log onto the new domain controller with a user account t…

937 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now