Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

SCCM 2012 Users to Computer collection import vbs

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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

A hard and fast method for reducing Active Directory Administrators members.
Compliance and data security require steps be taken to prevent unauthorized users from copying data.  Here's one method to prevent data theft via USB drives (and writable optical media).
This tutorial will walk an individual through the process of configuring their Windows Server 2012 domain controller to synchronize its time with a trusted, external resource. Use Google, Bing, or other preferred search engine to locate trusted NTP …
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…
Suggested Courses

618 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