8/20/2010

vbScript - Migrate Advertisements from SMS to SCCM

This script will migrate all advertisements from on site sms/sccm to another one
Be careful: Never run the script in your production until you've tested it!!!


'On Error Resume Next
'Option Explicit

Dim strSourceServer : strSourceServer = "SMSServer"
Dim strTargetServer : strTargetServer = "SCCMServer"
Dim strSourceSiteCode : strSourceSiteCode = "000"
Dim strTargetSiteCode : strTargetSiteCode = "000"
Dim objSourceWMIService, objTargetWMIService
Dim AllPackages, objPackage
Dim strProgramName, strCurPackageVersion, strCurpackageLanguage, strCurPackageManufacturer
Dim Token, schedule, schedtype
Dim Path
Dim strNewCollectionsID, strNewPackageID, strNewContainerID
Dim AllAdvertisements, objAdvertisement, newAdvertisement, Advertisement
Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objLogFile : Set objLogFile = objFSO.CreateTextFile("MigrateAdvertisements.log")

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

'Creating Container
Call CreateContainer

Set AllAdvertisements = objSourceWMIService.ExecQuery("Select * From SMS_Advertisement")
For Each objAdvertisement In AllAdvertisements

strProgramName = objAdvertisement.ProgramName
objLogFile.WriteLine Now &" - The program " &strProgramName &" is assigned to the Advertisement " &objAdvertisement.AdvertisementName

'Get Container ID of the Advertisement
strNewContainerID = GetNewContainerID(ContainerIDToName(GetContainerID(objAdvertisement.AdvertisementID)))
objLogFile.WriteLine Now &" - New ContainerID for Advertisement " &objAdvertisement.AdvertisementName &" is " &strNewContainerID

'Get New Collections ID
strNewCollectionsID = GetNewCollectionsID(CollectionIDToName(objAdvertisement.CollectionID))
objLogFile.WriteLine Now &" - The CollectionID " &strNewCollectionsID &" will be assigned to " &objAdvertisement.AdvertisementName

'Get New Package ID
strNewPackageID = GetNewPackageID(PackageIDToName(objAdvertisement.PackageID))
objLogFile.WriteLine Now &" - The PackageID " &strNewPackageID &" will be assigned to " &objAdvertisement.AdvertisementName

'Create Advertisement
If strNewCollectionsID <> "" And strProgramName <> "" And strNewPackageID <> "" Then
Set newAdvertisement = objTargetWMIService.Get("SMS_Advertisement").SpawnInstance_()
Set lazyproperties = objSourceWMIService.get("sms_advertisement.advertisementid='" & objAdvertisement.advertisementid & "'")

newAdvertisement.assignedscheduleenabled = lazyproperties.assignedscheduleenabled
newAdvertisement.assignedscheduleisgmt = lazyproperties.assignedscheduleisgmt
newAdvertisement.expirationtimeenabled = lazyproperties.expirationtimeenabled
newAdvertisement.expirationtimeisgmt = lazyproperties.expirationtimeisgmt
newAdvertisement.assignedschedule = lazyproperties.assignedschedule
newAdvertisement.presenttimeisgmt=lazyproperties.presenttimeisgmt

newAdvertisement.AdvertisementName = objAdvertisement.AdvertisementName
newAdvertisement.AdvertFlags= objAdvertisement.AdvertFlags
newAdvertisement.CollectionID = strNewCollectionsID
newAdvertisement.DeviceFlags = DeviceFlags
newAdvertisement.comment = objAdvertisement.Comment
newAdvertisement.ExpirationTime= objAdvertisement.ExpirationTime
newAdvertisement.HierarchyPath = objAdvertisement.HierarchyPath
newAdvertisement.IncludeSubCollection = objAdvertisement.IncludeSubCollection
newAdvertisement.PackageID = strNewPackageID
newAdvertisement.PresentTime=objAdvertisement.PresentTime
newAdvertisement.PresentTimeEnabled=objAdvertisement.PresentTimeEnabled
newAdvertisement.ProgramName = strProgramName
newAdvertisement.RemoteClientFlags = objAdvertisement.RemoteClientFlags
newAdvertisement.Priority = objAdvertisement.Priority
newAdvertisement.TimeFlags = objAdvertisement.TimeFlags
Path = newAdvertisement.Put_

'Get automatically assigned Advertisement ID
Set Advertisement=objTargetWMIService.Get(Path)
'Moving the Advertisements into Container
Call MoveAdvertisementInToContainer(strNewContainerID, Advertisement.AdvertisementID)

Else
objLogFile.WriteLine Now &" - ERROR: Advertisement " &objAdvertisement.AdvertisementName &" could not be migrated. Maybe a Collection, Package or Program does not exist"
End If

strProgramName = ""
strNewPackageID = ""
strNewCollectionsID = ""

Next
WScript.Echo "Done"


Function CollectionIDToName(CollectionID)
Dim AllCollections, objCollection

Set AllCollections = objSourceWMIService.ExecQuery("SELECT * FROM SMS_Collection WHERE CollectionID='" &CollectionID &"'")
For Each objCollection In AllCollections
CollectionIDToName = objCollection.Name
Next
End Function

Function GetNewCollectionsID(CollectionName)
Dim AllCollections, objCollection

Set AllCollections = objTargetWMIService.ExecQuery("SELECT * FROM SMS_Collection WHERE Name='" &CollectionName &"'")
For Each objCollection In AllCollections
GetNewCollectionsID = objCollection.CollectionID
Next
End Function

Function PackageIDToName(PackageID)
Dim AllPackages, objPackage

Set AllPackages = objSourceWMIService.ExecQuery("SELECT * FROM SMS_Package WHERE PackageID='" &PackageID &"'")
For Each objPackage In AllPackages
PackageIDToName = objPackage.Name
strCurPackageVersion = objPackage.Version
strCurPackageManufacturer = objPackage.Manufacturer
strCurpackageLanguage = objPackage.Language
Next
End Function

Function GetNewPackageID(PackageName)
Dim AllPackages, objPackage

'Make sure the Correct Package is choosen
Set AllPackages = objTargetWMIService.ExecQuery("SELECT * FROM SMS_Package WHERE Name='" &PackageName _
&"' AND Version='" &strCurPackageVersion _
&"' AND Language='" &strCurpackageLanguage _
&"' AND Manufacturer='" &strCurPackageManufacturer &"'")

For Each objPackage In AllPackages
GetNewPackageID = objPackage.PackageID
Next
strCurPackageVersion = ""
strCurpackageLanguage = ""
strCurPackageManufacturer = ""
End Function

Sub CreateContainer

Dim AllSourceContainer, objSourceContainer
Dim objTargetContainer

Set AllSourceContainer = objSourceWMIService.ExecQuery("SELECT * FROM SMS_ObjectContainerNode WHERE ObjectType='3'")
For Each objSourceContainer In AllSourceContainer

objLogFile.WriteLine Now &" - Migrating Container " &objSourceContainer.Name
Set objTargetContainer = objTargetWMIService.Get("SMS_ObjectContainerNode").SpawnInstance_()
objTargetContainer.Name = objSourceContainer.Name
objTargetContainer.ObjectType = objSourceContainer.ObjectType
objTargetContainer.ParentContainerNodeID = objSourceContainer.ParentContainerNodeID
On Error Resume Next
objTargetContainer.Put_
On Error GoTo 0

Next

End Sub

Function MoveAdvertisementInToContainer(ContainerID, AdvertisementID)
Dim folderItem

If ContainerID <> "" Then

objLogFile.WriteLine Now &" - Moving Advertisement " &AdvertisementID &" into Container " &ContainerID

Set folderItem = objTargetWMIService.Get("SMS_ObjectContainerItem").SpawnInstance_()
folderItem.InstanceKey = AdvertisementID
folderItem.ObjectType = "3"
folderItem.ContainerNodeID = ContainerID
folderItem.Put_
Else
objLogFile.WriteLine Now &" - No need to move the Advertisement " &AdvertisementID &", because it's listed in the root"
End If

End Function

Function GetContainerID(AdvertisementID)
Dim AllContainers, objContainer

Set AllContainers = objSourceWMIService.ExecQuery("SELECT * FROM SMS_ObjectContainerItem WHERE ObjectType='3' AND InstanceKey='" &AdvertisementID &"'")
For Each objContainer In AllContainers
GetContainerID = objContainer.ContainerNodeID
Next

End Function

Function ContainerIDToName(ContainerID)
Dim AllContainers, objContainer

Set AllContainers = objSourceWMIService.ExecQuery("SELECT * FROM SMS_ObjectContainerNode WHERE ObjectType='3' AND ContainerNodeID='" &ContainerID &"'")
For Each objContainer In AllContainers
ContainerIDToName = objContainer.Name
Next

End Function

Function GetNewContainerID(ContainerName)
Dim AllSourceContainer, objSourceContainer

Set AllSourceContainer = objTargetWMIService.ExecQuery("SELECT * FROM SMS_ObjectContainerNode WHERE ObjectType='3' and Name = '" &ContainerName &"'")

For Each objSourceContainer In AllSourceContainer
GetNewContainerID = objSourceContainer.ContainerNodeID
Next
End Function

No comments: