?
Solved

SCCM 2012 Users to Computer collection import vbs

Posted on 2014-01-19
2
Medium Priority
?
1,112 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 2000 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

Group policies can be applied selectively to specific devices with the help of groups. Utilising this, it is possible to phase-in group policies, over a period of time, by randomly adding non-members user or computers at a set interval, to a group f…
This process allows computer passwords to be managed and secured without using LAPS. This is an improvement on an existing process, enhanced to store password encrypted, instead of clear-text files within SQL
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…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.
Suggested Courses

762 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