Solved

SCCM 2012 Users to Computer collection import vbs

Posted on 2014-01-19
2
1,075 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
Comment Utility
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
Comment Utility
Rob, thank you very much for the info, my foolish mistake, forgot to add "Site_" in line #16
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Companies that have implemented Microsoft’s Active Directory need to ensure that the Active Directory is configured and operating properly. If there are issues found and not resolved, it eventually leads the components to fail or stop working and fi…
Find out how to use Active Directory data for email signature management in Microsoft Exchange and Office 365.
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…
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…

743 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

15 Experts available now in Live!

Get 1:1 Help Now