Wednesday, April 29, 2015

Powershell: SCCM 2012 bulk package creation script

This script is intended for those who want to create multiple packages in sccm at once.
Please test the script and make necessary changes before you use it in production.
This script is provided as is and without any warranty. Author is not responsible for any damages which might be caused by running this script without making changes prior to running it.

No error handling and minimal validations done in the script.

-----------------------------------------------------------------------------
#author lanabhat@gmail.com

#This script creates packages one by one from the details given in the CSV file,also creates programs, creates the collection.


#Package creation script, will create a package from given parameters, creates programs, creates the collection


#csv file header Package_RAW.csv

#SharePath,MsiPath,PackageName,PackageDescription,Manufacturer,Version,Lang,InstallCMD,UninstalllCMD,SpaceMB,SilentInstallCMD,CollectionName #note sometimes excel removes .0 etc ex 1.0 becomes 1

#If you are using an MSi following fields are not required PackageName,PackageDescription,Manufacturer,Version,Lang


#Run the CreateCollectionNSetDeployment.ps1 once the binary replication is enabled and package is distributed to DPs.



$list=Import-Csv $env:userprofile\desktop\Package_RAW.csv


$list | foreach{

$msipath=$_.MsiPath;

$msipackage=$false;


if($msipath -ne "")

{

$msipackage=$true

}

else

{

  $msipackage=$false

}

$pkgSrvShr=$_.SharePath; #ex: "\\myshare\software\Microsoft\Visio\VisioPro2013"

$name=$_.PackageName; #"Visio 2013 Pro"

$description=$_.PackageDescription; #"Visio 2013 Pro"

$manufacturer=$_.Manufacturer; #"Microsoft"

$ver=$_.Version;  #"2013"

$lng=$_.Lang;#"English"

$installcmdline=$_.InstallCMD;#"Install.vbs"

$SilentInstallCMDline=$_.SilentInstallCMD;

$uincommandline=$_.UninstalllCMD;#"Uninstall.vbs"

$space=$_.SpaceMB;#"450"


$lmtClctn ="CAS00001"

$clctnName=$_.CollectionName;

$rfrshScd=New-CMSchedule -RecurInterval Hours -RecurCount 4


#This is the standard query used to get the machines which are part of a ARS group. ARS group is $clctnName        

$qStmt="select SMS_R_SYSTEM.ResourceID,SMS_R_SYSTEM.ResourceType,SMS_R_SYSTEM.Name,SMS_R_SYSTEM.SMSUniqueIdentifier,SMS_R_SYSTEM.ResourceDomainORWorkgroup,SMS_R_SYSTEM.Client from SMS_R_System where SMS_R_System.SystemGroupName = 'DOMAIN\\"+$clctnName+"'"

    

#This is in pipeline to copy the package to server.

#Copy-Item -Path $pkgFileSrc -Destination $pkgSrvShr -Recurse



if($msipackage -eq $false) #If MsiPath is not entered in the excel, package is considered as non definition package.

{

$mypackage=New-CMPackage -Name $name -Description $description  -Language $lng -Manufacturer $manufacturer -Path $pkgSrvShr -Version $ver

}

else #below code for package from definition

{


$mypackage=New-CMPackage -FromDefinition -PackagePath $msipath -SourceFileType AlwaysObtainSourceFile -SourceFolderPath $pkgSrvShr -SourceFolderPathType UncNetworkPath -verbose


#Code below to delete the standard programs created by Msi.

$delprogs=@("Per-system attended","Per-system unattended","Per-system uninstall","Per-user attended","Per-user unattended","Per-user uninstall")

  

foreach($pro in $delprogs)

{

  $dpgm=Get-CMPackage -Id $pkgID | Get-CMProgram -ProgramName $pro

  if($dpgm -ne $null)

  {

   Remove-CMProgram -PackageId $pkgID -ProgramName $pro -force -verbose

  }

}

}


#$pkgid=CAS00098 - cisco webex 7.3.3


$pkgid=$mypackage.PackageID;


#Find out how to enable Binary differential replication




$pgm1=Get-CMPackage -Id $pkgID | Get-CMProgram -ProgramName "InstallProgram"

$pgm2=Get-CMPackage -Id $pkgID | Get-CMProgram -ProgramName "SilentInstall"

$pg3=Get-CMPackage -Id $pkgID | Get-CMProgram -ProgramName "Uninstall"


if(($pgm1 -eq $null)-and ($installcmdline -ne ""))

{

  New-CMProgram -CommandLine $cmdline -PackageId $pkgID -StandardProgramName $stdpgmname -DiskSpaceRequirement $space -DiskSpaceUnit $dunit -Duration 720 -ProgramRunType WhetherOrNotUserIsLoggedOn -RunMode RunWithAdministrativeRights -RunType Normal -UserInteraction 1 -verbose

}

if(($pgm2 -eq $null) -and ($bincommandline -ne ""))

{

  New-CMProgram -CommandLine $bincommandline -PackageId $pkgID -StandardProgramName $binstall -DiskSpaceRequirement $space -DiskSpaceUnit $dunit -Duration 720 -ProgramRunType WhetherOrNotUserIsLoggedOn -RunMode RunWithAdministrativeRights -RunType Hidden -UserInteraction 0 -verbose

}

if(($pgm3 -eq $null) -and ($uincommandline -ne ""))

{

  New-CMProgram -CommandLine $uincommandline -PackageId $pkgID -StandardProgramName $unin -DiskSpaceRequirement "Unknown" -Duration 720 -ProgramRunType WhetherOrNotUserIsLoggedOn -RunMode RunWithAdministrativeRights -RunType Normal -UserInteraction 0 -verbose

}



read-host "Please enable Binary replication and press enter to start distribution"



#syntax

#Start-CMContentDistribution -DeploymentPackageId <String[]> [-CollectionName <String> ] [-DistributionPointGroupName <String> ] [-DistributionPointName <String> ] [-Confirm] [-WhatIf] [ <CommonParameters>]


#get all DP groups and distribute package to each group

Get-CMDistributionPointGroup | %{Start-CMContentDistribution -PackageId $pkgID -DistributionPointGroupName $_.Name -verbose}


#below two servers are not part of any DP grop si distribute separately.

Start-CMContentDistribution -PackageId $pkgID -DistributionPointName "dp1.mydomain.com" -verbose

Start-CMContentDistribution -PackageId $pkgID -DistributionPointName "dp2.mydomain.com" -verbose


#Enter N or enable read-host for enabling collection creation choice. yes or no

$a="Y" #read-host "Create collection Y or N"


if($a -eq "Y")

{


#Script below for collection creation


   write-host "Creating a collection with Name: $clctnName"  

  


    $cmnt="";


    $pkg=Get-CMPackage -Id $pkgID | select Name,Version

    if($pkg -ne $null)

    {

     $cmnt=$pkg.Name+" "+$pkg.Version

    }


    #$rfrshScd=$_.RefreshSchedule; #type <IResultObject>



    $clctn=Get-CMDeviceCollection -Name $clctnName


    if($clctn -eq $null)

    {

     write-host "Collection not found, creation collection"

     New-CMDeviceCollection -LimitingCollectionId $lmtClctn -Name $clctnName -Comment $cmnt -RefreshSchedule $rfrshScd  -RefreshType Periodic -verbose

     $clctn=Get-CMDeviceCollection -Name $clctnName

     Add-CMDeviceCollectionQueryMembershipRule -Collection $clctn -QueryExpression $qStmt -RuleName $clctn.Name -verbose

     #move to the self service folder

     Move-CMObject -FolderPath "CAS:\DeviceCollection\_Software Deployment Collections\" -InputObject $clctn -verbose

     write-host "Collection created and moved to self services folder"

    }

    else

    {

     write-host "Collection already exists, not created again"

    }

} #end of collection creation Y

else #If user selects N for collection creation.

{

  "No collection created"

}

}
























































































































































-----------------------------------------------
email: lanabhat@gmail.com
Phone: 9743289373
Please provide your suggestions and also feel free to ask your queries.
-----------------------------------------------

No comments:

Post a Comment