Monday, November 23, 2015

Here's a Script PowerShell to duplicate Package on SCCM 2012

Before Modify any Package on Production, take time to duplicate it.
It's a part of Best Practice, to duplicate a package befor modifing them.




Here's a Script PowerShell to duplicate Package on SCCM 2012

#------------------------

$ServerSCCM = "SCCMTest"
Import-Module '\\$ServerSCCM\d$\Program Files\Microsoft Configuration Manager\AdminConsole\bin\ConfigurationManager.psd1'
$pkgID = 'FR100F40'
$packageDestination = "\\Share\Soft\Repository_Serveurs\01. UAT\Packages"
$PackageList = @(gwmi -ComputerName $ServerSCCM -Namespace root\sms\site_FR1 -class SMS_Package -filter "PackageID='$pkgID'")

if ( $PackageList.count -gt 0 ) {
         foreach ( $package in $PackageList )
         {
          #Store name and location of package
          $pName = $package.name
          $pLocation = $package.PkgSourcePath
        
          #Extract folder name of package
          $folderName =$pLocation.Substring($pLocation.LastIndexOf("\")+1)
                  
          Write-Host "Package to duplicate : " $pName
          #Copie sources
          Copy-Item $pLocation  $packageDestination  -recurse
        
          #acces to SCCM site
          cd "$((Get-PSDrive -PSProvider CMSite).Name):"
        
          $ProgramList = Get-CMProgram -PackageId $pkgID
        
          #Create a copie of package
          $newPackage = New-CMPackage -Name "$pName - copie" -Path "$packageDestination\$folderName"
          #add programes
          foreach ( $Program in $ProgramList )
          {
         
           $np = New-CMProgram -PackageName "$pName - copie"  -CommandLine $Program.CommandLine -StandardProgramName $Program.ProgramName
           #Copie program Proreities
           $np.ProgramFlags = $Program.ProgramFlags
         
           #Commit changes
           $np.put()
         
          }
        
   
        
                  
        
           }
      
                 
        }
 

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.