Be careful: Never run the script in your production until you've tested it!!!
'Option Explicit
On Error Resume Next
Dim strSourceServer : strSourceServer = "SMSServer"
Dim strTargetServer : strTargetServer = "SCCMServer"
Dim strSourceSiteCode : strSourceSiteCode = "000"
Dim strTargetSiteCode : strTargetSiteCode = "000"
Dim strNewSourcePath : strNewSourcePath = "SCCMServer"
Dim strOldSourcePath : strOldSourcePath = "SMSServer"
Dim objSourceWMIService, objTargetWMIService
Dim objPackage, AllPackages
Dim strContainerID, strContainerName
Dim bolScheduleAvailable : bolScheduleAvailable = False
Dim Token, schedule, schedtype
Dim strPkgManufacturer, strPkgName, strPkgVersion, strPkgLanguage, strPkgDescription, strPkgSource, strPkgSourceFlag
Dim strPkgExtendedData, strPkgExtendedDataSize, strPkgForcedDisconnectDelay, strPkgForcedDisconnectEnabled
Dim strPkgForcedDisconnectRetries, strPkgIcon, strPkgiconSize,strPkgIgnoreAddressSchedule,strPkgFlags, strPkgPriority
Dim strPkgMIFName, strPkgMIFPublisher, srtPktMIFVersion, strPkgMIFFilename, strPkgPreferedAddressType
Dim strPkgRefreshSourceFlag, strPkgShareName, strPkgShareType, strPkgSourceData, strPkgSourceVersion
Dim strNewPackageID
Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objLogFile : Set objLogFile = objFSO.CreateTextFile("MigratePackages.log")
Set objSourceWMIService = GetObject("winmgmts://" & strSourceServer & "\root\sms\site_" & strSourceSiteCode)
Set objTargetWMIService = GetObject("winmgmts://" & strTargetServer & "\root\sms\site_" & strTargetSiteCode)
objLogFile.WriteLine Now &" - Migrating Packages from " &strSourceServer &" to " &strTargetServer
'First create all Containers
Call CreateContainer
'Loop trought all packages exsisting on the source server
Set AllPackages = objSourceWMIService.ExecQuery("SELECT * FROM SMS_Package")
For Each objPackage In AllPackages
objLogFile.WriteLine Now &" - Migrating Package " &objPackage.PackageID
'Get Package settings
strPkgName = objPackage.Name
strPkgManufacturer = objPackage.Manufacturer
strPkgVersion = objPackage.Version
strPkgLanguage = objPackage.Language
strPkgDescription = objPackage.Description
strPkgSource = objPackage.PkgSourcePath
strPkgSourceFlag = objPackage.PkgSourceFlag
strPkgExtendedData = objPackage.extendedData
strPkgExtendedDataSize = objPackage.extendedDataSize
strPkgForcedDisconnectDelay = objPackage.forcedDisconnectDelay
strPkgForcedDisconnectEnabled = objPackage.forcedDisconnectEnabled
strPkgForcedDisconnectRetries = objPackage.forcedDisconnectNumretries
strPkgIcon = objPackage.Icon
strPkgiconSize = objPackage.IconSize
strPkgIgnoreAddressSchedule = objPackage.IgnoreAddressSchedule
strPkgMIFFilename = objPackage.MifFileName
strPkgMIFName = objPackage.MifName
strPkgMIFPublisher = objPackage.MifPublisher
srtPktMIFVersion = objPackage.MifVersion
strPkgFlags = objPackage.PKGFlags
strPkgPreferedAddressType = objPackage.PreferredAddressType
strPkgPriority = objPackage.Priority
strPkgRefreshSourceFlag = objPackage.RefreshPkgSourceFlag
strPkgShareName = objPackage.ShareName
strPkgShareType = objPackage.ShareType
strPkgSourceData = objPackage.SourceDate
strPkgSourceVersion = objPackage.SourceVersion
'Get Package Refresh Schedule
Set objPackage = objSourceWMIService.Get("SMS_Package='" & objPackage.PackageID & "'" )
On Error Resume Next
Set schedule = objPackage.RefreshSchedule(0)
On Error GoTo 0
If err.number = 424 Then
objLogFile.WriteLine Now &" - Package has no Schedule " &objPackage.PackageID
ElseIf err.number <> 0 Then
objLogFile.WriteLine Now &" - An Error (" &err.number &") occured while getting the Refresh Schedule from " &objPackage.PackageID
Else
On Error Resume Next
schedtype=Schedule.Path_.Class
Select Case (schedtype)
Case "SMS_ST_RecurInterval"
Set Token = objTargetWMIService.Get("SMS_ST_RecurInterval").SpawnInstance_()
If schedule.MinuteSpan <> 0 Then Token.MinuteSpan = schedule.MinuteSpan
If schedule.HourSpan <> 0 Then Token.HourSpan = schedule.HourSpan
If schedule.DaySpan <> 0 Then Token.DaySpan = schedule.DaySpan
If schedule.MinuteDuration <> 0 Then Token.MinuteDuration = schedule.MinuteDuration
If schedule.HourDuration <> 0 Then Token.HourDuration = schedule.HourDuration
If schedule.DayDuration <> 0 Then Token.DayDuration = schedule.DayDuration
Token.StartTime = schedule.StartTime
bolScheduleAvailable = True
objLogFile.WriteLine Now &" - SMS_ST_RecurInterval Schedule available " &objPackage.PackageID
Case "SMS_ST_RecurWeekly"
Set Token = objTargetWMIService.Get("SMS_ST_RecurWeekly").SpawnInstance_()
If schedule.MinuteDuration <> 0 Then Token.MinuteDuration = schedule.MinuteDuration
If schedule.HourDuration <> 0 Then Token.HourDuration = schedule.HourDuration
If schedule.DayDuration <> 0 Then Token.DayDuration = schedule.DayDuration
If schedule.ForNumberOfWeeks <> 0 Then Token.ForNumberOfWeeks = schedule.ForNumberOfWeeks
If schedule.Day <> 0 Then Token.Day = schedule.Day
Token.StartTime = schedule.StartTime
bolScheduleAvailable = True
objLogFile.WriteLine Now &" - SMS_ST_RecurWeekly Schedule available " &objPackage.PackageID
Case "SMS_ST_RecurMonthlyByDate"
Set Token = objTargetWMIService.Get("SMS_ST_RecurMonthlyByDate").SpawnInstance_()
If schedule.MinuteDuration <> 0 Then Token.MinuteDuration = schedule.MinuteDuration
If schedule.HourDuration <> 0 Then Token.HourDuration = schedule.HourDuration
If schedule.DayDuration <> 0 Then Token.DayDuration = schedule.DayDuration
If schedule.ForNumberOfMonths <> 0 Then Token.ForNumberOfMonths = schedule.ForNumberOfMonths
If schedule.MonthDay <> 0 Then Token.MonthDay = schedule.MonthDay
Token.StartTime = schedule.StartTime
bolScheduleAvailable = True
objLogFile.WriteLine Now &" - SMS_ST_RecurMonthlyByDate Schedule available " &objPackage.PackageID
Case "SMS_ST_RecurMonthlyByWeekday"
Set Token = objTargetWMIService.Get("SMS_ST_RecurMonthlyByWeekday").SpawnInstance_()
If schedule.MinuteDuration <> 0 Then Token.MinuteDuration = schedule.MinuteDuration
If schedule.HourDuration <> 0 Then Token.HourDuration = schedule.HourDuration
If schedule.DayDuration <> 0 Then Token.DayDuration = schedule.DayDuration
If schedule.ForNumberOfMonths <> 0 Then Token.ForNumberOfMonths = schedule.ForNumberOfMonths
If schedule.WeekOrder <> 0 Then Token.WeekOrder = schedule.WeekOrder
Token.StartTime = schedule.StartTime
objLogFile.WriteLine Now &" - SMS_ST_RecurMonthlyByWeekday Schedule available " &objPackage.PackageID
bolScheduleAvailable = True
Case "SMS_ST_NonRecurring"
Set Token = objTargetWMIService.Get("SMS_ST_NonRecurring").SpawnInstance_()
If schedule.MinuteDuration <> 0 Then Token.MinuteDuration = schedule.MinuteDuration
If schedule.HourDuration <> 0 Then Token.HourDuration = schedule.HourDuration
If schedule.DayDuration <> 0 Then Token.DayDuration = schedule.DayDuration
Token.StartTime = schedule.StartTime
bolScheduleAvailable = True
objLogFile.WriteLine Now &" - SMS_ST_NonRecurring Schedule available " &objPackage.PackageID
Case Else
bolScheduleAvailable = False
objLogFile.WriteLine Now &" - Unknown Schedule (skipping) " &objPackage.PackageID
End Select
End If
'Get the container id of the package
strContainerID = GetContainerID(objPackage.PackageID)
'Get the containers name of the package
strContainerName = ContainerIDToName(strContainerID)
objLogFile.WriteLine Now &" - Old Container of the Package is " &strContainerName
'Get the new containers id
strNewContainerID = GetNewContainerID(strContainerName)
objLogFile.WriteLine Now &" - New Container ID of the Package is " &strNewContainerID
'Create the Package and get the new Package ID
strNewPackageID = CreatePackage()
Call CreatePrograms(objPackage.PackageID, strNewPackageID)
'Move the Package into the Container
Call MovePackageInToContainer(strNewContainerID, strNewPackageID)
Next
WScript.Echo "Done"
objLogFile.WriteLine Now &" - Finish"
Sub CreateContainer
Dim AllSourceContainer, objSourceContainer
Dim objTargetContainer
Set AllSourceContainer = objSourceWMIService.ExecQuery("SELECT * FROM SMS_ObjectContainerNode WHERE ObjectType='2'")
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 GetContainerID(PackageID)
Dim AllContainers, objContainer
Set AllContainers = objSourceWMIService.ExecQuery("SELECT * FROM SMS_ObjectContainerItem WHERE ObjectType='2' AND InstanceKey='" &PackageID &"'")
For Each objContainer In AllContainers
GetContainerID = objContainer.ContainerNodeID
Next
End Function
Function GetNewContainerID(ContainerName)
Dim AllSourceContainer, objSourceContainer
Set AllSourceContainer = objTargetWMIService.ExecQuery("SELECT * FROM SMS_ObjectContainerNode WHERE ObjectType='2' and Name = '" &ContainerName &"'")
For Each objSourceContainer In AllSourceContainer
GetNewContainerID = objSourceContainer.ContainerNodeID
Next
End Function
Function ContainerIDToName(ContainerID)
Dim AllContainers, objContainer
Set AllContainers = objSourceWMIService.ExecQuery("SELECT * FROM SMS_ObjectContainerNode WHERE ObjectType='2' AND ContainerNodeID='" &ContainerID &"'")
For Each objContainer In AllContainers
ContainerIDToName = objContainer.Name
Next
End Function
Function CreatePackage()
Dim newPackage, PKGSourcePath
Dim Package
objLogFile.WriteLine Now &" - Creating the Package " &strPkgManufacturer &" " &strPkgName &" " &strPkgVersion &" " &strPkgLanguage
Set newPackage = objTargetWMIService.Get("SMS_Package").SpawnInstance_
newPackage.Name = strPkgName
newPackage.Description = strPkgDescription
newPackage.extendedData = strPkgExtendedData
newPackage.extendedDataSize = strPkgExtendedDataSize
newPackage.forcedDisconnectDelay = strPkgForcedDisconnectDelay
newPackage.forcedDisconnectEnabled = strPkgForcedDisconnectEnabled
newPackage.forcedDisconnectNumretries = strPkgForcedDisconnectRetries
newPackage.Icon = strPkgIcon
newPackage.IconSize = strPkgiconSize
newPackage.IgnoreAddressSchedule = strPkgIgnoreAddressSchedule
newPackage.Language = strPkgLanguage
newPackage.Manufacturer = strPkgManufacturer
newPackage.MifFileName = strPkgMIFFilename
newPackage.MifName = strPkgMIFName
newPackage.MifPublisher = strPkgMIFPublisher
newPackage.MifVersion = srtPktMIFVersion
newPackage.PKGFlags = strPkgFlags
newPackage.PkgSourceFlag = strPkgSourceFlag
PKGSourcePath= Replace(Lcase(strPkgSource),Lcase(strOldSourcePath),Lcase(strNewSourcePath))
newPackage.PkgSourcePath = PKGSourcePath
newPackage.PreferredAddressType = strPkgPreferedAddressType
newPackage.Priority = strPkgPriority
newPackage.RefreshPkgSourceFlag = strPkgRefreshSourceFlag
newPackage.ShareName = strPkgShareName
newPackage.ShareType = strPkgShareType
newPackage.SourceDate = strPkgSourceData
newPackage.SourceVersion= strPkgSourceVersion
newPackage.Version = strPkgVersion
'Create Refresh Schedule for Package
If bolScheduleAvailable = True Then
newPackage.RefreshSchedule = Array(Token)
End If
Path = newPackage.Put_
'Get automatically assigned package ID
Set Package=objTargetWMIService.Get(Path)
CreatePackage = Package.PackageID
'Clean Variables
On Error Resume Next
Set Token = Nothing
Set schedule = Nothing
Set newPackage = Nothing
bolScheduleAvailable = False
schedtype = ""
End Function
Function MovePackageInToContainer(ContainerID, PackageID)
Dim folderItem
If ContainerID <> "" Then
objLogFile.WriteLine Now &" - Moving Package " &PackageID &" into Container " &ContainerID
Set folderItem = objTargetWMIService.Get("SMS_ObjectContainerItem").SpawnInstance_()
folderItem.InstanceKey = PackageID
folderItem.ObjectType = "2"
folderItem.ContainerNodeID = ContainerID
folderItem.Put_
Else
objLogFile.WriteLine Now &" - No need to move the Package " &PackageID &", because it's listed in the root"
End If
End Function
Function CreatePrograms(OldPackageID, NewPackageID)
Dim AllPrograms, objProgram
Dim newProgram
Set AllPrograms = objSourceWMIService.ExecQuery("SELECT * FROM SMS_Program WHERE PackageID='" &OldPackageID &"'")
If Not AllPrograms Is Nothing Then
For Each objProgram In AllPrograms
objLogFile.WriteLine Now &" - Creating the Program " &objProgram.ProgramName &" in Package " &OldPackageID
Set newProgram = objTargetWMIService.Get("SMS_Program").SpawnInstance_()
newProgram.CommandLine = objProgram.commandline
newProgram.Comment = objProgram.comment
newProgram.Description = objProgram.description
newProgram.Deviceflags = objProgram.deviceflags
newProgram.DiskSpaceReq = objProgram.DiskSpaceReq
newProgram.DriveLetter = objProgram.DriveLetter
newProgram.Duration = objProgram.Duration
newProgram.extendedData = objProgram.extendedData
newProgram.extendedDataSize = objProgram.extendedDataSize
newProgram.icon = objProgram.icon
newProgram.iconsize = objProgram.iconSize
newProgram.MSIFilePath = objProgram.MSIFilePath
newProgram.MSIProductID = objProgram.MSIProductID
newProgram.PackageID = NewPackageID
newProgram.ProgramFlags = objProgram.ProgramFlags
newProgram.ProgramName = objProgram.ProgramName
newProgram.RemovalKey = objProgram.RemovalKey
newProgram.Requirements = objProgram.Requirements
newProgram.SupportedOperatingSystems = objProgram.SupportedOperatingSystems
newProgram.WorkingDirectory = objProgram.WorkingDirectory
On Error Resume Next
newProgram.Put_
Next
End If
End Function
1 comment:
This was a lovely blog poost
Post a Comment