vbscript to create a SCCM advertisement.(Group AD"Install/Uninstall", Collections "Install/Uninstall", Packages, Programs"Install/Uninstall" with depentprogram, Deployment in dp, Advertisements )

The name of the program to run to meet the standard naming follows:


1 - app_ProductName_EditorName_VersionProduct_Language_Packageversion_treatment,
example app_microsoft_office2010_14.61_MUI_1.0_Uninstall.


2 - the name of the package shall not exceed 40 characters: len (app_ProductName_EditorName_VersionProduct_Language_Packageversion) = 40 character and must not contain special characters.

3 - example of sources contained in a package: 7zip.msi; app_7zip_7zip_7.0_ENG_1.0_U.vbs, app_7zip_7zip_7.0_ENG_1.0_I.vbs "_I for Install and _u For Uninstall"

 

The script creates:
- Two groups AD :  (Install / Uninstall) or under the Groups/1-Deploiement/Install and Groups/1-Deploiement/Uninstall

Function FCreateGroupAD(StrOUProcessing, Strflag)
Const ADS_GROUP_TYPE_GLOBAL_GROUP = &h2
Const ADS_GROUP_TYPE_SECURITY_ENABLED = &h80000000
Set objOU = GetObject("LDAP://ou=" & StrOUProcessing & StrLdapInformation)
Set objOU1 = objOU.Create("organizationalUnit", "ou=" & ArrNamePackage(1))
On Error Resume Next
objOU1.SetInfo
On Error GoTo 0
Set objOU2 = GetObject("LDAP://ou=" & ArrNamePackage(1) & ",ou="& StrOUProcessing & StrLdapInformation)
Set objGroup = objOU2.Create("Group", "cn=" & StrPackageName & Strflag)
objGroup.Put "sAMAccountName", StrPackageName & Strflag
objGroup.Put "groupType", ADS_GROUP_TYPE_GLOBAL_GROUP Or _
    ADS_GROUP_TYPE_SECURITY_ENABLED
On Error Resume Next
objGroup.SetInfo
On Error GoTo 0

end Function



- Two collections : based on the two groups mentioned above, collections will be created under the parent collection swd\legacy\Install and swd\ egacy\Uninstall.

Function CreateCollection(newName, queryForRule, StrparentCollection)
 Set swbemLocator=CreateObject("WbemScripting.SWbemLocator")
 Set swbemconnection=swbemLocator.ConnectServer(StrSCCMServerName, "root\sms")
 Set providerLoc=swbemconnection.InstancesOf("SMS_ProviderLocation")
 For Each Location In providerLoc
     If location.ProviderForLocalSite = True Then
         Set swbemconnection = swbemLocator.ConnectServer(Location.Machine, "root\sms\site_" + Location.SiteCode)
         Exit For
     End If
 Next
 'Update this collection on a schedule.
 Set Token = swbemconnection.Get("Sms_St_RecurInterval")
 Token.DaySpan = 1 'Use Token.HourSpan = 1 or for Token.MinuteSpan = 30 for test environment
 Token.startTime = GetStartTime
    ' Create the collection.
    Set newCollection = swbemconnection.Get("SMS_Collection").SpawnInstance_
    newCollection.Name = newName
 newCollection.Comment = newName + "."
    newCollection.OwnedByThisSite = True
 newCollection.RefreshSchedule = Array(Token)
 newCollection.RefreshType = 6
    ' Save the new collection and save the collection path for later.
    Set collectionPath = newCollection.Put_   
   ' IMPORTANT: If you do not specify the relationship, the new collection will not be visible in the console.
    Set newSubCollectToSubCollect = swbemconnection.Get("SMS_CollectToSubCollect").SpawnInstance_
    newSubCollectToSubCollect.parentCollectionID = StrparentCollection ' Define to what collection the new collection is subordinate.
    newSubCollectToSubCollect.subCollectionID = CStr(collectionPath.Keys("CollectionID"))
    newSubCollectToSubCollect.Put_  ' Save the subcollection information.
    ' Create a new collection rule object for validation.
    Set queryRule = swbemconnection.Get("SMS_CollectionRuleQuery")
    validQuery = queryRule.ValidateQuery(queryForRule)   ' Validate the query (good practice before adding it to the collection).
    ' Continue with processing, if the query is valid.
    If validQuery Then
        ' Create the query rule.
        Set newQueryRule = QueryRule.SpawnInstance_
        newQueryRule.QueryExpression = queryForRule
        newQueryRule.RuleName = newName + "."
        ' Add the new query rule to a variable.
        Set newCollectionRule = newQueryRule
        ' Get the collection.
        Set newCollection = swbemconnection.Get(collectionPath.RelPath)
        newCollection.AddMembershipRule newCollectionRule ' Add the rules to the collection.
        newCollection.RequestRefresh False ' Call RequestRefresh to initiate the collection evaluator.
   Else
     MsgBox("Invalid Query: " + queryForRule)
     End If
End Function

-Package : with two programs(Install/Uninstall) and Deployed sources on DP.

 

 

Sub CreatePackage()

 Set swbemLocator=CreateObject("WbemScripting.SWbemLocator")
 Set swbemconnection=swbemLocator.ConnectServer(StrSCCMServerName, "root\sms")
 Set providerLoc=swbemconnection.InstancesOf("SMS_ProviderLocation")
 For Each Location In providerLoc
     If location.ProviderForLocalSite = True Then
         Set swbemconnection = swbemLocator.ConnectServer(Location.Machine, "root\sms\site_" + Location.SiteCode)
         Exit For
     End If
 Next
 dbprogramFlagsInstall = -1995398144
 dbprogramFlagsUnInstall = -1995398144
 StrDepentProgramUnInstall = ""
 StrDepentProgramInstall = ""
 StrDepentPackageID = ""
 StrDepentProgram = InputBox("Enter the name of the package dealt with first","DepentProgram")
 If Not Isnull(StrDepentProgram) and StrDepentProgram <> "" Then
 Set objDepentPackageSCCM = swbemconnection.ExecQuery _
     ("Select * From SMS_Program where  ProgramName = '" & StrDepentProgram & StrInstallFlag & "'")
  For each objDepentPackage in objDepentPackageSCCM
      StrDepentPackageID =  objDepentPackage.PackageID
  Next
  If StrDepentPackageID = "" Or IsNull(StrDepentPackageID) Then
   MsgBox "Dependence is not found ", 16, "DepentProgram"
  Else
   dbprogramFlagsInstall = -1995398016
   StrDepentProgramInstall = StrDepentPackageID & ";;" & StrDepentProgram & StrInstallFlag
   IntUnInstallDepentProgram = MsgBox("Do you want to treat dependence Uninstall ?", 36, "UnInstall DepentProgram")
   If IntUnInstallDepentProgram = 6 Then
    StrDepentProgramUnInstall = StrDepentPackageID & ";;" & StrDepentProgram & StrUninstallFlag
    dbprogramFlagsUnInstall = -1995398016
   End If
  End If
 End If
  
 ' Create the new package object.
    Set newPackage = swbemconnection.Get("SMS_Package").SpawnInstance_
    ' Populate the new package properties.
    newPackage.Name = StrPackageName
    newPackage.Description = "Traitement du Package " & StrPackageName
    newPackage.PkgSourceFlag = newPackageSourceFlag
    newPackage.PkgSourcePath = StrPathLivraisonPackage
    newPackage.Put_ ' Save the package.
    Set Packages = swbemconnection.ExecQuery _
     ("Select * From SMS_Package where Name = '" & StrPackageName & "'")
 For Each objPackage in Packages    
     StrPackageID =  objPackage.PackageID
 Next
'=========================================================================================
 '=======================move the package to specific folder===========================
 Set objNewConsoleFolderItem = swbemconnection.Get _
 ("SMS_ObjectContainerItem").SpawnInstance_()
 objNewConsoleFolderItem.InstanceKey = StrPackageID
 objNewConsoleFolderItem.ObjectType=2 ' Package Node.
   On Error Resume Next
      Set folder = swbemconnection.Get("SMS_ObjectContainerNode").SpawnInstance_()
      folder.Name = ArrNamePackage(1)
      folder.ObjectType = 2
      folder.ParentContainerNodeID = ParentContainerNodeID
      folder.Put_
   On Error GoTo 0  
  Set objFoldersSCCM = swbemconnection.ExecQuery _
     ("Select * From SMS_ObjectContainerNode where  ObjectType = 2 and Name = '" & ArrNamePackage(1) & "'")
  For each objFolderSCCM in objFoldersSCCM
      StrcontainerNodeID =  objFolderSCCM.ContainerNodeID
  Next
 objNewConsoleFolderItem.ContainerNodeID = StrcontainerNodeID 'Container
 objNewConsoleFolderItem.Put_
    ' Output the new package name.
    ' ' wscript.echo "Created package: "  & newPackageDescription
'====================================================================================================
' Create distribution point object (this is not an actual distribution point).
    Set distributionPoint = swbemconnection.Get("SMS_DistributionPoint").SpawnInstance_
    ' Associate the existing package with the new distribution point object.
    distributionPoint.PackageID = StrPackageID    
    ' This query selects a single distribution point based on the provided SiteCode and ServerName.
    query = "SELECT * FROM SMS_SystemResourceList WHERE RoleName='SMS Management Point' AND ResourceType='Windows NT Server' AND SiteCode='" & siteCode & "' AND ServerName='" & StrSCCMServerName & "'"
    Set listOfResources = swbemconnection.ExecQuery(query, , wbemFlagReturnImmediately)
    ' The query returns a collection that needs to be enumerated (although we should only get one instance back).
    For Each resource In ListOfResources     
        distributionPoint.ServerNALPath = Resource.NALPath
        distributionPoint.SiteCode = Resource.SiteCode       
    Next
    ' Save the distribution point instance for the package.
    distributionPoint.Put_
    ' Display notification text.
    ' ' Wscript.Echo "Assigned package: " & distributionPoint.PackageID
'====================================================================================================
' Create the new program object.
    Set newProgram = swbemconnection.Get("SMS_Program").SpawnInstance_
    ' Populate the program properties.
    newProgram.PackageID = StrPackageID
    newProgram.ProgramName = StrPackageName & StrInstallFlag
    newProgram.Comment = "Traitement Du package du package " & StrPackageName & StrInstallFlag
    newProgram.programFlags = dbprogramFlagsInstall
    newProgram.DependentProgram = StrDepentProgramInstall
    newProgram.DiskSpaceReq = StrSizeFolder & " MB"
    newProgram.CommandLine = StrPackageName & StrInstallFlag & ".vbs"
    newProgram.Duration = "120"
    ' Save the new program and properties.
    On Error Resume Next
    newProgram.Put_
    If Err Then MsgBox "Une erreur s'est produite lors de la création du programme d'installation " & StrPackageName & StrInstallFlag, 16, "Programme sccm"
    On Error GoTo 0
    Set newProgramU = swbemconnection.Get("SMS_Program").SpawnInstance_
    newProgramU.PackageID = StrPackageID
    newProgramU.ProgramName = StrPackageName & StrUninstallFlag
    newProgramU.Comment = "Traitement Du package du package " & StrPackageName & StrUninstallFlag
    newProgramU.programFlags = dbprogramFlagsUninstall
    newProgramU.DependentProgram = StrDepentProgramUninstall
    newProgramU.DiskSpaceReq = ""
    newProgramU.CommandLine = StrPackageName & StrUninstallFlag & ".vbs"
    'newProgram.Duration = "120"
    ' Save the new program and properties.
    On Error Resume Next
    newProgramU.Put_
    If Err Then MsgBox "Une erreur s'est produite lors de la création du programme de désinstallation " & StrPackageName & StrUninstallFlag, 16, "Programme sccm"
    On Error GoTo 0

- Two advertisements

  '********************************* Create the new advertisement object.********************************************************
    Set newAdvertisement = swbemconnection.Get("SMS_Advertisement").SpawnInstance_
    Set oCollections = swbemconnection.ExecQuery _
     ("Select * From SMS_Collection where Name = '" & StrPackageName & StrInstallFlag & "'")
 For each objCollection in oCollections    
     StrCollectionID =  objCollection.CollectionID
 Next
    ' Populate the advertisement properties.
    newAdvertisement.CollectionID = StrCollectionID
    newAdvertisement.PackageID = StrPackageID
    newAdvertisement.ProgramName = StrPackageName & StrInstallFlag
    newAdvertisement.AdvertisementName = StrPackageName & StrInstallFlag
    newAdvertisement.Comment = "Traitement du package " & StrPackageName & StrInstallFlag
    newAdvertisement.IncludeSubCollection = False
    newAdvertisement.AdvertFlags = 33554432
    newAdvertisement.RemoteClientFlags = 8240
    newAdvertisement.AssignedScheduleEnabled = True
    newAdvertisement.IncludeSubCollection = False
    newAdvertisement.PresentTime = GetStartTime()
    newAdvertisement.PresentTimeEnabled = True
    newAdvertisement.AssignedSchedule = Array
    ' Save the new advertisement and properties.
    On Error Resume Next
    StrPath=newAdvertisement.Put_
    If Err Then MsgBox "Une erreur s'est produite lors de la création de l'advertisement d'installation " & StrPackageName & StrInstallFlag, 16, "advertisement sccm"
    On Error GoTo 0
    Set Advertisements = swbemconnection.ExecQuery _
     ("Select * From SMS_Advertisement where AdvertisementName = '" & StrPackageName & StrInstallFlag & "'")
 For Each objAdvertis In Advertisements    
     StrAdvertiseID =  objAdvertis.AdvertisementID
 Next

'==================================Move the Advertisement to specific Folder==============================
'=========================================================================================
 Set objNewConsoleFolderItem = swbemconnection.Get _
 ("SMS_ObjectContainerItem").SpawnInstance_()
 objNewConsoleFolderItem.InstanceKey = StrAdvertiseID
 objNewConsoleFolderItem.ObjectType=3 ' Advertisement Node.
   On Error Resume Next
      Set folder = swbemconnection.Get("SMS_ObjectContainerNode").SpawnInstance_()
      folder.Name = ArrNamePackage(1)
      folder.ObjectType = 3
      folder.ParentContainerNodeID = ParentContainerNodeIDAdvInst
      folder.Put_
   On Error GoTo 0  
      
  Set objFoldersSCCM = swbemconnection.ExecQuery _
     ("Select * From SMS_ObjectContainerNode where ObjectType = 3 and Name = '" & ArrNamePackage(1) & "'")
  For each objFolderSCCM in objFoldersSCCM
      StrcontainerNodeID =  objFolderSCCM.ContainerNodeID
  Next
 objNewConsoleFolderItem.ContainerNodeID = StrcontainerNodeID 'Container
 objNewConsoleFolderItem.Put_
'==========================================================================================================
'*********************************************************************************************************
  DateTime = GetStartTime
  Set Advertisement = swbemconnection.Get(StrPath)
  sAdvertisementID = Advertisement.AdvertisementID
  Set oAdvertisement = swbemconnection.Get("SMS_Advertisement.AdvertisementID='" & sAdvertisementID & "'")
  Set newST = swbemconnection.Get("SMS_ST_RecurInterval").SpawnInstance_()
  newST.DayDuration=20
  newST.DaySpan=1
  newST.IsGMT = False
  newST.StartTime = DateTime
  advertArray = oAdvertisement.AssignedSchedule
  onemore = ubound(advertArray) +  1
  ReDim Preserve advertArray(onemore)
  Set advertArray(ubound(advertArray)) = newST
  oAdvertisement.AssignedSchedule = advertArray
  'add the new assignment
  oAdvertisement.AssignedSchedule(onemore) = newST
  oAdvertisement.AssignedScheduleEnabled = True
  oAdvertisement.DeviceFlags = 0
  oAdvertisement.AdvertFlags = 40894464
  oAdvertisement.RemoteClientFlags = 8240
  On Error Resume Next
  oAdvertisement.Put_
     On Error GoTo 0

'*********************************************************************************************************
 Set oCollections = swbemconnection.ExecQuery _
  ("Select * From SMS_Collection where Name = '" & StrPackageName & StrUninstallFlag & "'")
 For each objCollection in oCollections    
     StrCollectionID =  objCollection.CollectionID
 Next
    ' Populate the advertisement properties.
    newAdvertisement.CollectionID = StrCollectionID
    newAdvertisement.PackageID = StrPackageID
    newAdvertisement.ProgramName = StrPackageName & StrUninstallFlag
    newAdvertisement.AdvertisementName = StrPackageName & StrUninstallFlag
    newAdvertisement.Comment = "Traitement du package " & StrPackageName
    newAdvertisement.IncludeSubCollection = False
    newAdvertisement.RemoteClientFlags = 8240
    newAdvertisement.AssignedScheduleEnabled = True
    newAdvertisement.AdvertFlags = 33554432
    newAdvertisement.PresentTime = GetStartTime()
    newAdvertisement.PresentTimeEnabled = True
    ' Save the new advertisement and properties.
    On Error Resume Next
    newAdvertisement.Put_
    If Err Then MsgBox "Une erreur s'est produite lors de la création de l'advertisement de désinstallation " & StrPackageName & StrUninstallFlag, 16, "advertisement sccm"
    On Error GoTo 0
    Set Advertisements = swbemconnection.ExecQuery _
     ("Select * From SMS_Advertisement where AdvertisementName = '" & StrPackageName & StrUninstallFlag & "'")
 For Each objAdvertis In Advertisements    
     StrAdvertiseID =  objAdvertis.AdvertisementID
 Next
'==================================Move the Advertisement to specific Folder==============================
'=========================================================================================
 Set objNewConsoleFolderItem = swbemconnection.Get _
 ("SMS_ObjectContainerItem").SpawnInstance_()
 objNewConsoleFolderItem.InstanceKey = StrAdvertiseID
 objNewConsoleFolderItem.ObjectType=3 ' Advertisement Node.
   On Error Resume Next
      Set folder = swbemconnection.Get("SMS_ObjectContainerNode").SpawnInstance_()
      folder.Name = ArrNamePackage(1)
      folder.ObjectType = 3
      folder.ParentContainerNodeID = ParentContainerNodeIDAdvUninst
      folder.Put_
   On Error GoTo 0  
      
  Set objFoldersSCCM = swbemconnection.ExecQuery _
     ("Select * From SMS_ObjectContainerNode where ObjectType = 3 and Name = '" & ArrNamePackage(1) & "'")
  For each objFolderSCCM in objFoldersSCCM
      StrcontainerNodeID =  objFolderSCCM.ContainerNodeID
  Next
 objNewConsoleFolderItem.ContainerNodeID = StrcontainerNodeID 'Container
 objNewConsoleFolderItem.Put_
    ' Output the new package name.
    ' ' wscript.echo "Created package: "  & newPackageDescription
'==========================================================================================================
    '*********************************************************************************************************
  Set AdvertisementU = swbemconnection.Get(StrPath)
  sAdvertisementID = AdvertisementU.AdvertisementID
  Set oAdvertisementU = swbemconnection.Get("SMS_Advertisement.AdvertisementID='" & sAdvertisementID & "'")
  Set newSTU = swbemconnection.Get("SMS_ST_RecurInterval").SpawnInstance_()
  newSTU.DayDuration=20
  newSTU.DaySpan=1
  newSTU.IsGMT = False
  newSTU.StartTime = DateTime
  advertArrayU = oAdvertisementU.AssignedSchedule
  onemore = ubound(advertArrayU) +  1
  ReDim Preserve advertArray(onemoreU)
  Set advertArrayU(ubound(advertArrayU)) = newSTU
  oAdvertisementU.AssignedSchedule = advertArrayU
  'add the new assignment
  oAdvertisementU.AssignedSchedule(onemoreU) = newSTU
  oAdvertisementU.AssignedScheduleEnabled = True
  oAdvertisementU.DeviceFlags = 0
  oAdvertisementU.AdvertFlags = 40894464
  oAdvertisementU.RemoteClientFlags = 8240
  On Error Resume Next
  oAdvertisementU.Put_
     On Error GoTo 0
 '*********************************************************************************************************
End Sub

Do you have a question about automating tasks sccm. Ask it here:     http://systemcentervnext.com/forums/p/209/330.aspx#330

 

Monssef MESTOUR

MONSSEF@MESTOUR.COM

 

Published 12-04-2011 1:06 by MESTOUR Monssef
Filed under: , ,