|
jscott607 -> Help with ignoring query rule systems (6/4/2008 3:22:43 PM)
|
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
|
|
|
|