How to Delete an Assignment from an SMS 2003 Advertisement
I recently worked on an issue where a customer was trying to programmatically delete a mandatory assignment from an SMS 2003 advertisement. I saw that adding an assignment is a very easy and documented task, however deleting the same assignment is not.
Unfortunately I was only able to work on the issue for a few hours because I was going on vacation but one of my peers, David Stewart, figured it out while I was out. I thought this example would be useful for anyone looking to delete an assignment using a script.
' Parse cmd line args
' ==============================
sSiteCode = WScript.Arguments(0)
sAdvertID = WScript.Arguments(1)
' Connect to local site provider
' ==============================
Set objSWbemLocator = CreateObject("WbemScripting.SWbemLocator")
Set objSWbemServices = objSWbemLocator.ConnectServer(".", "root\sms")
Set ProviderLoc = objSWbemServices.InstancesOf("SMS_ProviderLocation")
For Each Location In ProviderLoc
If Location.ProviderForLocalSite = True Then
Set objSWbemServices = objSWbemLocator.ConnectServer(Location.Machine, "root\sms\site_" + Location.SiteCode)
Set objSWbemLocator = Nothing
Set ProviderLoc = Nothing
End If
Next
' Retrieve the advertisment instance of concern
' =============================================
Set objAdvert = objSWbemServices.Get("SMS_Advertisement.AdvertisementID='" & sAdvertID & "'")
' Display advertisement properties
' ================================
WScript.Echo "ActionInProgress = " & objAdvert.ActionInProgress
WScript.Echo "AdvertFlags = " & objAdvert.AdvertFlags
WScript.Echo "AdvertisementID = " & objAdvert.AdvertisementID
WScript.Echo "AdvertisementName = " & objAdvert.AdvertisementName
WScript.Echo "CollectionID = " & objAdvert.CollectionID
WScript.Echo "Comment = " & objAdvert.Comment
WScript.Echo "Assigned Schedule = {"
' Display advertisement properties
' ================================
set Advertisement_Properties = objAdvert.Properties_ 'get property set
set AssignedScheduleArray = Advertisement_Properties.Item("AssignedSchedule")
' Display advertisement properties
' ================================
Dim replacementScheduleArray()
intSize = 0
' Loop through current assignments, copy them to new array to update advertisement instance.
' Leave any assignments we want to remove out of the new array.
' ==========================================================================================
for i = 0 to UBound(AssignedScheduleArray)
'Retrieve next schedule token from instance array
'================================================
Dim SMS_ScheduleToken
Set SMS_ScheduleToken = AssignedScheduleArray(i)
'Display schedule token, ask if it should be deleted.
'====================================================
WScript.Echo AssignedScheduleArray.Name & " " & i & ":" & SMS_ScheduleToken.GetObjectText_()
If MsgBox("Do you want to delete this assignment?", 4, "Assignment:" & SMS_ScheduleToken.StartTime) <> vbYes Then
'Not deleting this schedule token, add it to the replacement array.
'==================================================================
redim preserve replacementScheduleArray(intSize)
set replacementScheduleArray(intSize) = SMS_ScheduleToken.Clone_()
intSize = intSize + 1
end if
next
WScript.Echo "}"
'Update the advertisement instance with the replacement schedule token array
'===========================================================================
if(IntSize < 1) then
'We removed all the schedule tokens.
objAdvert.Properties_.Item("AssignedSchedule") = null
else
' There is at least one value to keep
objAdvert.Properties_.Item("AssignedSchedule") = replacementScheduleArray
end if
objAdvert.Put_()
Comments
- Anonymous
September 18, 2007
PingBack from http://msdnrss.thecoderblogs.com/2007/09/18/how-to-delete-an-assignment-from-an-sms-2003-advertisement/