I sat all day with google and tried out code and at the and I was successful in putting together someting that actually worked. The sample script creates both the application and the deployment type at the same time. I would rather use a prebuilt application
and just add the deployment type but that is work for another day. Hope you find it usefull.
I did find a good reference here
http://blogs.technet.com/b/chrad/archive/2012/09/12/configmgr-2012-pcm-walking-through-pcm-plug-in-capabilities.aspx but I could not get the DetectionMethod to work in my code. But I got it working with both VBScript and PowerShell detection scripts and
I'm fine for now with that.
-------------ExampleCode---------------
#Anders Hannus 2013-03-14
#Some code from:
http://blog.lechar.nl/2012/04/03/creating-an-sccm-2012-application-with-powershell/
#Load assemblies
[Reflection.Assembly]::LoadFile("D:\Microsoft Configuration Manager\AdminConsole\bin\Microsoft.ConfigurationManagement.ApplicationManagement.dll") | out-null
[Reflection.Assembly]::LoadFile("D:\Microsoft Configuration Manager\AdminConsole\bin\Microsoft.ConfigurationManagement.ApplicationManagement.MsiInstaller.dll") | out-null
$wminamespace = "Root\SMS\Site_S02"
$sccmserver = hostname
#Application info. Normaly read from LDAP database but hardcoded in this example.
$LTUappMaker = "LTU" #Maker of application
$LTUappName = "testapp" #Name of application
$LTUappVersion = "1.0" #Version of application
$LTUpackageName = "install_ltukerberos_ltu.cmd" #Command to install application
$description = "Registry keys so that Windows can find the KRB servers for Realm LTU.SE" #Description of what the program is/does.
$displayname = "TEST testapp 1.0" #Diplayname, created from LTUappMaker, LTUappName and LTUappVersion but might be adjusted.
$app = "APP00001" #Application id in our database
$source = "\\server\sccmsource\$app" #Source directory for application.
$LTUappCheckPath = "C:\Windows\notepad.exe" #file/folder to detect if application is installed.
write-host "Creating Application $displayname"
#Get scopeid
$identificationClass = [WMICLASS]"\\$($sccmserver)\$($wminamespace):SMS_Identification"
$cls = Get-WmiObject SMS_Identification -namespace $wminamespace -ComputerName $sccmserver -list
$tmp = $identificationClass.GetSiteID().SiteID
$scopeid = "ScopeId_$($tmp.Substring(1,$tmp.Length -2))"
#Create an unique id for the application and the deployment type
$newApplicationID = "Application_" + [guid]::NewGuid().ToString()
$newDeploymentTypeID = "DeploymentType_" + [guid]::NewGuid().ToString()
#Create SCCM 2012 object id for application and deploymenttype
$newApplicationID = New-Object Microsoft.ConfigurationManagement.ApplicationManagement.ObjectID($scopeid,$newApplicationID)
$newDeploymentTypeID = New-Object Microsoft.ConfigurationManagement.ApplicationManagement.ObjectID($scopeid,$newDeploymentTypeID)
#Create objects neccessary for the creation of the application
$newApplication = New-Object Microsoft.ConfigurationManagement.ApplicationManagement.Application($newApplicationID)
$newDeploymentType = New-Object Microsoft.ConfigurationManagement.ApplicationManagement.DeploymentType($newDeploymentTypeID,"Script")
#Setting Display Info
$newDisplayInfo = New-Object Microsoft.ConfigurationManagement.ApplicationManagement.AppDisplayInfo
$newDisplayInfo.Title = $displayname
$newDisplayInfo.Language = "sv-SE"
$newDisplayInfo.Description = $description
$newApplication.DisplayInfo.Add($newDisplayInfo)
#Setting default Language must be set and displayinfo must exist
$newApplication.DisplayInfo.DefaultLanguage = $newDisplayInfo.Language
$newApplication.Title = $displayname
$newApplication.Version = 1.0
$newApplication.Publisher = $LTUappMaker
$newApplication.SoftwareVersion = $LTUappVersion
$newApplication.Description = $app
#Add all content to the application
$newApplicationContent = [Microsoft.ConfigurationManagement.ApplicationManagement.ContentImporter]::CreateContentFromFolder($source)
$newApplicationContent.OnSlowNetwork = "Download"
#Deployment Type Script installer will be used
$newDeploymentType.Title = "$displayname - Script Installer"
$newDeploymentType.Version = 1.0
$newDeploymentType.Installer.InstallCommandLine = $LTUpackageName
$newDeploymentType.Installer.UninstallCommandLine = $LTUpackageName.replace("install","uninstall")
$newDeploymentType.Installer.Contents.Add($newApplicationContent)
#Detectionmethod
$testscript = "if (test-path ""$LTUappCheckPath"") {write-host ""The application is installed.""}"
$newDeploymentType.Installer.DetectionMethod = "Script"
$newDetectionScript = New-Object Microsoft.ConfigurationManagement.ApplicationManagement.Script
$newDetectionScript.Language = "PowerShell"
$newDetectionScript.Text = $testscript
$newDeploymentType.Installer.DetectionScript = $newDetectionScript
#Add the DeploymentType to the Application
$newApplication.DeploymentTypes.Add($newDeploymentType)
#Serialize the object to an xml file and stuff it into SCCM
$newApplicationXML = [Microsoft.ConfigurationManagement.ApplicationManagement.Serialization.SccmSerializer]::SerializeToSTring($newApplication,$true)
$applicationClass = [WMICLASS]"\\$($sccmserver)\$($wminamespace):SMS_Application"
$newApplication = $applicationClass.createInstance()
$newApplication.SDMPackageXML = $newApplicationXML
$tmp = $newApplication.Put()
#Reload the application to get the data
$newApplication.Get()