Wednesday, April 20, 2011

VBScript to get collection schedules

Option Explicit

Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objLogFile : Set objLogFile = objFSO.CreateTextFile("Collections.log")

Dim strSourceServer
strSourceServer   = "XXX"
Dim strSourceSiteCode
strSourceSiteCode = "XXX"

Dim objSourceWMIService
Dim AllCollections
Dim objCollection
Dim strCollectionName
Dim strCollectionComment
Dim schedule
Dim schedtype
Dim bolScheduleAvailable
Dim strRecur
Dim strEvery

Set objSourceWMIService = GetObject("winmgmts://" & strSourceServer & "\root\sms\site_" & strSourceSiteCode)

'objLogFile.WriteLine Now & " start "

'Loop trought all collections exsisting on the source server
Set AllCollections = objSourceWMIService.ExecQuery("SELECT * FROM SMS_Collection WHERE NAME NOT LIKE 'All%' AND NAME NOT LIKE 'Root%'")

For Each objCollection In AllCollections
    strCollectionName    = objCollection.Name
    strCollectionComment = objCollection.Comment

    'objLogFile.WriteLine Now & " - Collection  " & objCollection.Name

    Set objCollection = objSourceWMIService.Get("SMS_Collection='" & objCollection.CollectionID & "'" )

    'Get Collection schedule
    On Error Resume Next
    Set schedule = objCollection.RefreshSchedule(0)

    On Error GoTo 0

    If Err.Number = 424 Then
        objLogFile.WriteLine "Collection has no Schedule  " & objCollection.Name
    ElseIf Err.Number <> 0 Then
        objLogFile.WriteLine "An Error (" & Err.Number & ") occured while getting the Update Schedule from " & objCollection.Name
    Else
        'On Error Resume Next
        schedtype = Schedule.Path_.Class

        Select Case (schedtype)
            Case "SMS_ST_RecurInterval"
                If schedule.MinuteSpan <> 0 Then strRecur = "Minute"
                If schedule.HourSpan <> 0 Then strRecur = "Hour"
                If schedule.DaySpan <> 0 Then strRecur = "Day"

                If schedule.MinuteDuration <> 0 Then strEvery = schedule.MinuteDuration
        If schedule.HourDuration <> 0 Then strEvery = schedule.HourDuration
        If schedule.DayDuration <> 0 Then strEvery = schedule.DayDuration

                    bolScheduleAvailable = True
                    objLogFile.WriteLine objCollection.Name & ",Recurr every " & strEvery & " " &  strRecur & ",Starting " & schedule.StartTime

                Case "SMS_ST_RecurWeekly"

                    If schedule.ForNumberOfWeeks <> 0 Then  strRecur = "Weekly"

                    bolScheduleAvailable = True
                    objLogFile.WriteLine objCollection.Name & ",Recurr every " & strRecur & ",Starting " & schedule.StartTime

                Case "SMS_ST_RecurMonthlyByDate"

            If schedule.ForNumberOfMonths <> 0 Then strRecur = "Monthly"
                    bolScheduleAvailable = True
                    objLogFile.WriteLine objCollection.Name & ",Recurr every " & strRecur & ",Starting " & schedule.StartTime
                Case "SMS_ST_RecurMonthlyByWeekday"

                    Token.StartTime = schedule.StartTime
                    objLogFile.WriteLine Now & " - SMS_ST_RecurMonthlyByWeekday Schedule available " & objCollection.Name

                    bolScheduleAvailable = True

                Case "SMS_ST_NonRecurring"

                    bolScheduleAvailable = True
                    objLogFile.WriteLine Now & " - SMS_ST_NonRecurring Schedule available " & objCollection.Name

                Case Else
                    bolScheduleAvailable = False
                    objLogFile.WriteLine Now & " - Unknown Schedule (skipping) " & objCollection.Name
            End Select

        End If

    Next

    objLogFile.WriteLine Now & " - Finish"
    WScript.Echo "Done"

Wednesday, April 6, 2011

VBScript Script to Delete Specific SCCM Collections and Associated Advertisements

'need to change the site code in line Set WbemServices = loc.ConnectServer( ,"root\SMS\site_XXX")
'script takes input from a text file collectionslist.txe, having collection names.

Option Explicit
On Error Resume Next

Dim loc

Dim Collection
Dim Collections
Dim strCollection
Dim strCollectionID

Dim Advertisement
Dim Advertisements
Dim objFSO
Dim objFile

Dim WbemServices
Const ForReading = 1

Set loc = CreateObject("WbemScripting.SWbemLocator")

Set WbemServices = loc.ConnectServer( ,"root\SMS\site_XXX")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("collectionslist.txt", ForReading)

Do Until objFile.AtEndOfStream
strCollection = objFile.ReadLine

    Set Collections = WbemServices.ExecQuery("select * from SMS_Collection where name like '%" & strCollection & "%'")

    For Each Collection In Collections
        strCollectionID = Collection.CollectionID
        Wscript.Echo "Collection    : " + Collection.Name

        Set Advertisements = WbemServices.ExecQuery("select * from SMS_Advertisement where CollectionID = """ & strCollectionID & """")
       
        For Each Advertisement in Advertisements
            Wscript.Echo "Advertisement : " & Advertisement.AdvertisementName
            Advertisement.Delete_
        Next
    On Error Goto 0
    Collection.Delete_
    Next
Loop
objFile.Close