none
Copy Packages with all Properties RRS feed

  • Question

  • Hi,

    is there an easy way to copy an existing Package with all it's Properties into a new one where only the Name and Source Path changed ? Or do i need to read all properties (quiet a lot) before and write it back to the new one ?

    thanks

    Monday, August 23, 2010 4:31 PM

All replies

  • Probably not very helpful to you if you are building everything in your code “from scratch”, but… if you are just migrating packages from one server to another, there is a tool called “CopySCCMObj” that will copy SCCM (and SMS) objects (including packages). I have obtained a copy through our TAM. Very nice and dead easy to use.

     

    I have a similar problem (in that I want to create packages). I have done a script that does physical apps but creating APP-V packages appears to me to be even more complicated with even more things to set. :)

    I didn’t find any shortcuts, but then I was creating new PKGs not copying ‘em, so I didn’t look that hard.  

     

    Just a pointer...

    Tuesday, August 24, 2010 11:37 AM
  • Hey,

    thanks for the pointer ! I know that tool and it is quite good for migration. But as you said, i need a more "from the scratch" thing. I went through the SDK Doc. and wrote down each possible propertie in my code....boring !! ;-)

     

    This is a first shot....

     public string copyPackage(string sourcePackageId, string targetName, bool copyPrograms,Dictionary<string,string> packageParameters)
        {
          try
          {
            Helper.TraceMessage("Trying to copy the Package with ID " + sourcePackageId + " to " + targetName, Helper.TraceLevel.Information);
    
            // Getting the source Package
            IResultObject sourcePackage = connectionManager.GetInstance(@"SMS_Package.PackageID='" + sourcePackageId + "'");
    
            // Getting the Propertie Values 
            IQueryPropertyItem Description = sourcePackage["Description"];
            IQueryPropertyItem Language = sourcePackage["Language"];
            IQueryPropertyItem Manufacturer = sourcePackage["Manufacturer"];   
            IQueryPropertyItem PackageType = sourcePackage["PackageType"];
            IQueryPropertyItem PkgFlags = sourcePackage["PkgFlags"];
            IQueryPropertyItem PkgSourceFlag = sourcePackage["PkgSourceFlag"];
            IQueryPropertyItem PkgSourcePath = sourcePackage["PkgSourcePath"];
            IQueryPropertyItem PreferredAddressType = sourcePackage["PreferredAddressType"];
            IQueryPropertyItem Priority = sourcePackage["Priority"];
            IQueryPropertyItem RefreshPkgSourceFlag = sourcePackage["RefreshPkgSourceFlag"];
            IQueryPropertyItem RefreshSchedule = sourcePackage["RefreshSchedule"];
            IQueryPropertyItem ShareName = sourcePackage["ShareName"];
            IQueryPropertyItem ShareType = sourcePackage["ShareType"];
            IQueryPropertyItem StoredPkgPath = sourcePackage["StoredPkgPath"];
            IQueryPropertyItem Version = sourcePackage["Version"];
    
            // Creating the new Package
            IResultObject targetPackage = connectionManager.CreateInstance("SMS_Package");
    
            // Writting back the Properties
            targetPackage["Name"].StringValue = targetName;
            targetPackage["Description"].StringValue = Description.StringValue;
            targetPackage["Language"].StringValue = Language.StringValue;
            targetPackage["Manufacturer"].StringValue = Manufacturer.StringValue;
            targetPackage["PackageType"].IntegerValue = PackageType.IntegerValue;
            targetPackage["PkgFlags"].IntegerValue = PkgFlags.IntegerValue;
    
            if (packageParameters.ContainsKey("PkgSourceFlag")) { targetPackage["PkgSourceFlag"].IntegerValue = Convert.ToInt32(packageParameters["PkgSourceFlag"]); } else { targetPackage["PkgSourceFlag"].IntegerValue = PkgSourceFlag.IntegerValue; }
            if (packageParameters.ContainsKey("PkgSourcePath")) { targetPackage["PkgSourcePath"].StringValue = packageParameters["PkgSourcePath"]; } else { targetPackage["PkgSourcePath"].StringValue = PkgSourcePath.StringValue; }
            
            targetPackage["PreferredAddressType"].StringValue = PreferredAddressType.StringValue;
            targetPackage["Priority"].IntegerValue = Priority.IntegerValue;
            targetPackage["RefreshPkgSourceFlag"].BooleanValue = RefreshPkgSourceFlag.BooleanValue;
            targetPackage["RefreshSchedule"].ObjectArrayValue = RefreshSchedule.ObjectArrayValue;
            targetPackage["ShareName"].StringValue = ShareName.StringValue;
            targetPackage["ShareType"].IntegerValue = ShareType.IntegerValue;
            targetPackage["StoredPkgPath"].StringValue = StoredPkgPath.StringValue;
            targetPackage["Version"].StringValue = Version.StringValue;
    
            // Put and Get the Package
            targetPackage.Put();
            targetPackage.Get();
    
            // If copy Program Flags true, loop through each Program and copy it too
            if (copyPrograms)
            {
              foreach (string program in getProgramsByPackageId(sourcePackageId))
    	        {
                copyProgram(sourcePackageId, program, targetPackage["PackageID"].StringValue, program); //Copy the Package 
    	        }
              
              
            }
    
            // New Package ID
            return targetPackage["PackageID"].StringValue;
    
          }
          catch (Exception ex)
          {
            Helper.TraceMessage("Error while copying the Package with ID " + sourcePackageId + " to Package with Name " + targetName + ".\r\n" + ex.Message, Helper.TraceLevel.Error);
            throw ex;
          }
        
        }
    
    
    Friday, August 27, 2010 7:12 AM
  • You could optimize this a bit, and make it less boring by reading all the propertynames from the propertynames attribute and cycle through them in that way.
    "Everyone is an expert at something" Kim Oppalfens Configmgr expert for lack of any other expertise. http://www.scug.be/blogs/sccm
    Monday, August 30, 2010 8:28 AM
    Moderator
  • The following code snippet details how to "cycle through the propertynames"

     string sourcePackageId = "WS10003F";
    
       IResultObject sourcePackage = Connect().GetInstance(@"SMS_Package.PackageID='" + sourcePackageId + "'");
         
       string[] Props = sourcePackage.PropertyNames;
    
       foreach (string Item in sourcePackage.Properties.PropertyNames)
       {
    
        IQueryPropertyItem PropItem = sourcePackage[Item];
        MessageBox.Show(Item + " " + PropItem.ObjectValue);
    
       }
    

     Clark Bohs

    • Edited by Clark_Bohs Monday, January 24, 2011 11:49 PM better
    Monday, January 24, 2011 11:39 PM
  • http://pleasepressanykey.blogspot.com/2010/08/vbscript-migrate-packages-from-sms-to.html
    This one might work (in a static way), but I like Kim's solution better (because it cycles through them dynamically).
    Tuesday, February 1, 2011 2:32 PM
    Moderator