myITforum.com Community Forum myITforum.com Community Forum

Home  Forums  Blogs  Live Support chat  Search Articles  Wiki  FAQ  Email Lists  Register  Login  My Profile  Inbox  Address Book  My Subscription  My Forums 

Photo Gallery  Member List  Search  Calendars  FAQ  Ticket List  Log Out

All Forums RSS Feed Subscription:


  


Help with ignoring query rule systems

 
View related threads: (in this forum | in all forums)

Logged in as: Guest
  Printable Version
All Forums >> [Scripting Technologies] >> VB Script >> Help with ignoring query rule systems Page: [1]
Login
Message << Older Topic   Newer Topic >>
Help with ignoring query rule systems - 6/4/2008 3:22:43 PM   
jscott607

 

Posts: 208
Score: 0
Joined: 9/20/2005
Status: offline
Hello all. I have another question regarding removing direct memberships from a collection. I am using Jason's script and it works perfectly if there is no query rule on the collection. If I put a query rule on the collection, an error occurs.

I originally was going to just remove all query rules. Unfortunately, that is not an option. Is it possible to only remove systems that have a last successful advert status and are direct members? I have looked at doing If status.laststate = successStateID and objResource.ResourceID = IsDirect. Unfortunately, I am still learning and cannot get it to work.

Any ideas?

Part that I believe I need to modify

' Check if status is successful
If status.laststate = successStateID Then
' Remove resource from collection
Set instDirectRule = objSMS.Get("SMS_CollectionRuleDirect").SpawnInstance_
instDirectRule.ResourceID = objResource.ResourceID
objColl.DeleteMembershipRule instDirectRule



Full Script

 Declare SMS constants
Const SMSServer = "SMSSERVER"
Const SMSSiteCode = "KCI"
Const AdvertID = "KCI00012"
Const rootCollID = "KCI00031"
Const LogPath = "Cleanup.log"
Const successStateID = 13
Const ProcessSubCollections = 0 ' (0/1)
Const EmailResults = 0 ' (0/1)
Const LogResults = 1 ' (0/1)

' Standard File System Object
Set WshFileSys = WScript.CreateObject("Scripting.FileSystemObject")

' Create array objects for email details
Set machineSuccessList = WScript.CreateObject("Scripting.Dictionary")
Set machineNonSuccessList = WScript.CreateObject("Scripting.Dictionary")

' Setup connection to sms server
Set loc = CreateObject("WbemScripting.SWbemLocator")
Set objSMS = loc.ConnectServer(SMSServer, "root\SMS\Site_" & _
SMSSiteCode)

' Creates collection object based on ProcessSubCollection switch above
If ProcessSubCollections = 1 Then
' Get a collection of all subcollections defined from the rootCollID above
Set collList = objSMS.ExecQuery("select * from SMS_CollectToSubCollect WHERE parentCollectionID = '" & rootCollID & "'")
Else
Set collList = objSMS.ExecQuery("select * from SMS_Collection where CollectionID = '" & rootCollID & "'")
End If

' Loop thru all sub collections
For each subColl in collList

If ProcessSubCollections = 1 Then
' Get both resourceID and Advertisements associated with current sub collection
Set objResources = objSMS.ExecQuery("SELECT ResourceID FROM SMS_CollectionMember_a WHERE CollectionID = '" & subColl.SubCollectionID & "'")
Set objAdvertisements = objSMS.ExecQuery("Associators Of {SMS_Collection.CollectionID='" & SubColl.SubCollectionID & "'} Where ResultClass = SMS_Advertisement")
Set objColl = objSMS.Get("SMS_Collection.CollectionID='" & subColl.SubCollectionID & "'")
Else
' Get both resourceID and Advertisements associated with current sub collection
Set objResources = objSMS.ExecQuery("SELECT ResourceID FROM SMS_CollectionMember_a WHERE CollectionID = '" & subColl.CollectionID & "'")
Set objAdvertisements = objSMS.ExecQuery("Associators Of {SMS_Collection.CollectionID='" & SubColl.CollectionID & "'} Where ResultClass = SMS_Advertisement")
Set objColl = subColl
End If


' Loop thru all advertisements found on current collection
For each objAdvert in objAdvertisements

' Loop thru all resources found in current sub collection
For each objResource in objResources

' Get the last status of the current advertisement and for this resource
Set objStatus = objSMS.ExecQuery("Select * from SMS_ClientAdvertisementStatus where ResourceID = '" & _
objResource.ResourceID & "' AND AdvertisementID = '" & objAdvert.AdvertisementID & "'")

' Retrieve the system name for this resource
Set objSystemResource = objSMS.ExecQuery("Select name from SMS_R_System where resourceID = '" & objResource.ResourceID & "'")

' Loop thru system name object
For each objSystemName in objSystemResource

' Loop thru status object
For each status in objStatus

'Wscript.echo "Computer " & objSystemName.Name & " has a status of (" & status.laststate & " " & status.laststatename & ") for advertisement " & objAdvert.AdvertisementName & " in collection " & objColl.Name

' Declare machine details into dictionary array
Set machineStatus = Wscript.CreateObject("Scripting.Dictionary")

machineStatus.Add "Machine", objSystemName.Name
machineStatus.Add "Status", status.laststatename
machineStatus.Add "Advertisement", objAdvert.AdvertisementName

' Check if status is successful
If status.laststate = successStateID Then
' Remove resource from collection
Set instDirectRule = objSMS.Get("SMS_CollectionRuleDirect").SpawnInstance_
instDirectRule.ResourceID = objResource.ResourceID
objColl.DeleteMembershipRule instDirectRule

' Add machine, status, advertisement to Success status array for email
machineSuccessList.add machineSuccessList.count, machineStatus

'Wscript.echo "Removed computer " & objSystemName.Name & " from " & objColl.Name
Else
'Wscript.echo "Did not remove computer " & objSystemName.Name & " from " & objColl.Name

' Add machine, status, advertisement to NonSuccess status array for email
machineNonSuccessList.add machineNonSuccesslist.count, machineStatus

End If

' Clear array memory
Set machineStatus = Nothing
Next

Next

Next

Next

Next

' Send Email if specified in Constants
If EmailResults = 1 Then
' Send status email of both successful and nonsuccessful machines
Call SendMail(FormatResults(machineSuccessList, machineNonSuccessList))
End If

' Write log if specified in Constants
If LogResults = 1 Then
Call WriteLog(FormatResults(machineSuccessList, machineNonSuccessList), logPath)
End If


Wscript.echo "Done"
Wscript.Quit

' Function to create and send email with results
Function SendMail(strBody)
Const strFrom = "Collection.Cleanup.Script@noreply.com" ' From Address
Const strTo = "myemail@email.com" ' To Address
Const strSubject = "Status of Collection Cleanup" ' Subject

Set objSendMail = CreateObject("CDO.Message")

objSendMail.Sender = strFrom

objSendMail.To = strTo

objSendMail.Subject = strSubject

objSendMail.TextBody = strBody

objSendMail.Send

Set objSendMail = Nothing
End Function

' Appends to log file specified in constants
Function WriteLog(Results, logPath)

Set logFile = WshFileSys.OpenTextFile(logPath, 8, true)
logFile.WriteLine ""
logFile.WriteLine "New Cleanup entry started:"
logFile.WriteLine "Date = " & Now()
logFile.WriteLine ""
logFile.WriteLine Results
logFile.Close

End Function

' Function used to format the results for email/log viewing
Function FormatResults(machineSuccessList, machineNonSuccessList)
Dim SuccessList
Dim NonSuccessList
Dim tempString

SuccessList = "The following machines were successful and removed from SMS WebPush Collections:" & vbcrlf
tempString = ""
For each machineStatus in machineSuccessList.Items
tempString = tempString & "Computer Name: " & machineStatus("Machine") & vbTab & " Status: " & machineStatus("Status") & vbTab & " Advertisement: " & machineStatus("Advertisement") & vbcrlf
Next
SuccessList = SuccessList & tempString

NonSuccessList = "The following machines were NOT successful and NOT removed from SMS WebPush Collections:" & vbcrlf
tempString = ""
For each machineStatus in machineNonSuccessList.Items
tempString = tempString & "Computer Name: " & machineStatus("Machine") & vbTab & " Status: " & machineStatus("Status") & vbTab & " Advertisement: " & machineStatus("Advertisement") & vbcrlf
Next
NonSuccessList = NonSuccessList & tempString

FormatResults = SuccessList & vbcrlf & vbcrlf & NonSuccessList

End Function

Post #: 1
RE: Help with ignoring query rule systems - 6/4/2008 5:50:57 PM   
jscheffelmaer

 

Posts: 187
Score: 3
Joined: 5/6/2002
Status: offline
Actually you'll want to change that above in the intial query that collects the objResources (I should've done this from the start)

So
  Set objResources = objSMS.ExecQuery("SELECT ResourceID FROM SMS_CollectionMember_a WHERE CollectionID = '" & subColl.SubCollectionID & "'")
should look like this:
Set objResources = objSMS.ExecQuery("SELECT ResourceID FROM SMS_CollectionMember_a WHERE CollectionID = '" & subColl.SubCollectionID & "' AND IsDirect='True'")



_____________________________

Jason Scheffelmaer
Systems Integration Specialist


(in reply to jscott607)
Post #: 2
RE: Help with ignoring query rule systems - 6/4/2008 8:09:59 PM   
jscott607

 

Posts: 208
Score: 0
Joined: 9/20/2005
Status: offline
Thanks a lot, Jason. I will make the changes tomorrow and test it out.

(in reply to jscheffelmaer)
Post #: 3
RE: Help with ignoring query rule systems - 6/5/2008 8:13:24 AM   
jscott607

 

Posts: 208
Score: 0
Joined: 9/20/2005
Status: offline
I changed it to the below.

If ProcessSubCollections = 1 Then
' Get both resourceID and Advertisements associated with current sub collection
Set objResources = objSMS.ExecQuery("SELECT ResourceID FROM SMS_CollectionMember_a WHERE CollectionID = '" & subColl.SubCollectionID & "' AND IsDirect='True'")
Set objAdvertisements = objSMS.ExecQuery("Associators Of {SMS_Collection.CollectionID='" & SubColl.SubCollectionID & "'} Where ResultClass = SMS_Advertisement")
Set objColl = objSMS.Get("SMS_Collection.CollectionID='" & subColl.SubCollectionID & "'")
Else
' Get both resourceID and Advertisements associated with current sub collection
Set objResources = objSMS.ExecQuery("SELECT ResourceID FROM SMS_CollectionMember_a WHERE CollectionID = '" & subColl.SubCollectionID & "' AND IsDirect='True'")
Set objAdvertisements = objSMS.ExecQuery("Associators Of {SMS_Collection.CollectionID='" & SubColl.CollectionID & "'} Where ResultClass = SMS_Advertisement")
Set objColl = subColl
End If

I am getting a runtime error message.

Object doesn't support this property or method: 'subColl.SubCollectionID'

Any ideas?

(in reply to jscott607)
Post #: 4
RE: Help with ignoring query rule systems - 6/8/2008 1:06:49 PM   
jscott607

 

Posts: 208
Score: 0
Joined: 9/20/2005
Status: offline
I would appreciate any assistance from anyone. After doing further checking , it appears that there is a problem with the script. According to the SDK, SMS_Collection does not have a SubCollectionID property.

I am working on the script and trying to get it to only do one collection/advert at a time. However, I get an error message (null) : 0x8004100C. It is on the line For each objResource in objResources.

Below is the modified script. Any advice would be greatly appreciated.

'Declare SMS constants
Const SMSServer = "SMSCENTRAL"
Const SMSSiteCode = "001"
Const AdvertID = "00120000"
Const rootCollID = "00100010"
Const LogPath = "Cleanup.log"
Const successStateID = 13
Const LogResults = 1 ' (0/1)

' Standard File System Object
Set WshFileSys = WScript.CreateObject("Scripting.FileSystemObject")

' Setup connection to sms server
Set loc = CreateObject("WbemScripting.SWbemLocator")
Set objSMS = loc.ConnectServer(SMSServer, "root\SMS\Site_" & _
SMSSiteCode)

' Creates collection objects
Set coll = objSMS.ExecQuery("select * from SMS_Collection where CollectionID = '" & rootCollID & "'")
' Get both resourceID and Advertisements associated with current sub collection
Set objResources = objSMS.ExecQuery("SELECT ResourceID FROM SMS_CollectionMember_a WHERE CollectionID = '" &  "' And IsDirect= 'True'")
Set objAdvertisements = objSMS.ExecQuery("Associators Of {SMS_Collection.CollectionID='""} Where ResultClass = SMS_Advertisement")

' Loop thru all resources found in current sub collection
For each objResource in objResources

' Get the last status of the current advertisement and for this resource
Set objStatus = objSMS.ExecQuery("Select * from SMS_ClientAdvertisementStatus where ResourceID = '" & _
objResource.ResourceID & "' AND AdvertisementID = '" & objAdvert.AdvertisementID & "'")

' Retrieve the system name for this resource
Set objSystemResource = objSMS.ExecQuery("Select name from SMS_R_System where resourceID = '" & objResource.ResourceID & "'")

' Loop thru system name object
For each objSystemName in objSystemResource

' Loop thru status object
For each status in objStatus

Wscript.echo "Computer " & objSystemName.Name & " has a status of (" & status.laststate & " " & status.laststatename & ") for advertisement " & objAdvert.AdvertisementName & " in collection " & objColl.Name

' Declare machine details into dictionary array
Set machineStatus = Wscript.CreateObject("Scripting.Dictionary")
machineStatus.Add "Machine", objSystemName.Name
machineStatus.Add "Status", status.laststatename
machineStatus.Add "Advertisement", objAdvert.AdvertisementName

' Check if status is successful
If status.laststate = successStateID Then

' Remove resource from collection
Set instDirectRule = objSMS.Get("SMS_CollectionRuleDirect").SpawnInstance_
instDirectRule.ResourceID = objResource.ResourceID
objColl.DeleteMembershipRule instDirectRule

Wscript.echo "Removed computer " & objSystemName.Name & " from " & objColl.Name
Else
Wscript.echo "Did not remove computer " & objSystemName.Name & " from " & objColl.Name

End If
' Clear array memory
Set machineStatus = Nothing
Next
Next
Next
' Write log if specified in Constants
If LogResults = 1 Then
Call WriteLog(FormatResults(machineSuccessList, machineNonSuccessList), logPath)
End If

Wscript.echo "Done"
Wscript.Quit
' Appends to log file specified in constants
Function WriteLog(Results, logPath)
Set logFile = WshFileSys.OpenTextFile(logPath, 8, true)
logFile.WriteLine ""
logFile.WriteLine "New Cleanup entry started:"
logFile.WriteLine "Date = " & Now()
logFile.WriteLine ""
logFile.WriteLine Results
logFile.Close
End Function
' Function used to format the results for email/log viewing
Function FormatResults(machineSuccessList, machineNonSuccessList)
Dim SuccessList
Dim NonSuccessList
Dim tempString
SuccessList = "The following machines were successful and removed from SMS WebPush Collections:" & vbcrlf
tempString = ""
For each machineStatus in machineSuccessList.Items
tempString = tempString & "Computer Name: " & machineStatus("Machine") & vbTab & " Status: " & machineStatus("Status") & vbTab & " Advertisement: " & machineStatus("Advertisement") & vbcrlf
Next
SuccessList = SuccessList & tempString
NonSuccessList = "The following machines were NOT successful and NOT removed from SMS WebPush Collections:" & vbcrlf
tempString = ""
For each machineStatus in machineNonSuccessList.Items
tempString = tempString & "Computer Name: " & machineStatus("Machine") & vbTab & " Status: " & machineStatus("Status") & vbTab & " Advertisement: " & machineStatus("Advertisement") & vbcrlf
Next
NonSuccessList = NonSuccessList & tempString
FormatResults = SuccessList & vbcrlf & vbcrlf & NonSuccessList
End Function




(in reply to jscott607)
Post #: 5
Page:   [1]
All Forums >> [Scripting Technologies] >> VB Script >> Help with ignoring query rule systems Page: [1]
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts



  
Forum Software © ASPPlayground.NET Advanced Edition 2.4.5 ANSI

0.375