Configuration Manager 2012 Add MSI Application with Powershell

I had the need to add a few MSI applications to my Configuration Manager environment and created a script to simplify this. The script will extract information from the MSI file and use the application name, application version and application manufacturer when adding the application to the SCCM. The script will also create a deployment type to the SCCM application.

To run the script just execute the following command in a PowerShell shell:
AddCMApp.ps1 –MSIFilePath ‘\\SCCM\PKG\App1\app1.msi’

PowerShell script

Param(
    [Parameter(Mandatory=$True)]
    [String]$MSIFilePath
)
Process
{
    # Variables
    $Manufacturer = “”
    $ProductVersion = “”
    $ProductName = “”
    $PWDBegin = $PWD
    $CMSiteCode = “P01” # Change this – This should be your site code
    $CMModule = ‘C:\Program Files (x86)\Microsoft Configuration Manager\AdminConsole\bin\ConfigurationManager.psd1’
 
    $MSIFileName = $MSIFilePath.split(“\”)[-1]
    Write-Host “FileName: ” $MSIFileName
 
    If ($MSIFileName.split(“.”)[-1].ToLower() -ne “msi”)
    {
        Write-Host “ERROR – The file “”$MSIFileName”” is not a MSI file”
        Break
    }
    If ((Test-Path $MSIFilePath) -eq $False)
    {
        Write-Host “ERROR – The path “”$MSIFilePath”” is not correct”
        Break
    }
   
    # Code inspired by http://winadminnotes.wordpress.com/2010/04/05/accessing-msi-file-as-a-database
    # Get MSI file information
    $WindowsInstaller = New-Object -com WindowsInstaller.Installer
    $Database = $WindowsInstaller.GetType().InvokeMember(“OpenDatabase”, “InvokeMethod”, $Null, $WindowsInstaller, @($MSIFilePath,0))
    $View = $Database.GetType().InvokeMember(“OpenView”, “InvokeMethod”, $Null, $Database, (“SELECT * FROM Property”))
    $View.GetType().InvokeMember(“Execute”, “InvokeMethod”, $Null, $View, $Null)
    $Record = $View.GetType().InvokeMember(“Fetch”, “InvokeMethod”, $Null, $View, $Null)
 
    While($Record -ne $Null)
    {
        $PropertyName = $Record.GetType().InvokeMember(“StringData”, “GetProperty”, $Null, $Record, 1)
        $PropertyValue = $Record.GetType().InvokeMember(“StringData”,”GetProperty”, $Null, $Record, 2)
 
        If ($PropertyName -eq “Manufacturer”)
        {
            Write-Host($PropertyName + ” = ” + $PropertyValue)
            $Manufacturer = $PropertyValue
        }
        If ($PropertyName -eq “ProductName”)
        {
            Write-Host($PropertyName + ” = ” + $PropertyValue)
            $ProductName = $PropertyValue
        }
        If ($PropertyName -eq “ProductVersion”)
        {
            Write-Host($PropertyName + ” = ” + $PropertyValue)
            $ProductVersion = $PropertyValue
        }
        If ($PropertyName -eq “ARPHELPLINK”)
        {
            Write-Host($PropertyName + ” = ” + $PropertyValue)
            $ARPHELPLINK = $PropertyValue
        }
        $Record = $View.GetType().InvokeMember(“Fetch”, “InvokeMethod”, $Null, $View, $Null)
    }
     
    Import-Module $CMModule
     $CMSiteCode = $CMSiteCode + “:”
     Set-Location $CMSiteCode
     
    If ((Get-CMApplication -Name “$ProductName $ProductVersion”) -ne $NULL)
    {
        Write-Host “ERROR – CM Application with name “”$ProductName $ProductVersion”” already exist”
    }
    Else
    {
        Write-Host “Adding CM Application: “”$ProductName $ProductVersion”””
        New-CMApplication -Name “$ProductName $ProductVersion” -Publisher $Manufacturer -SoftwareVersion $ProductVersion
    }   
   
    If ((Get-CMDeploymentType -ApplicationName “$ProductName $ProductVersion” -DeploymentTypeName $ProductName) -ne $NULL)
    {
        Write-Host “ERROR – CM Deployment Type with name “”$ProductName”” already exist”
        Set-Location $PWDBegin
        Break
    }
    Else
    {
        Write-Host “Adding CM Deployment Type: “”$ProductName”””
        Add-CMDeploymentType -ApplicationName “$ProductName $ProductVersion” -DeploymentTypeName “$ProductName” -AdministratorComment “Created by PowerShell” -InstallationProgram “msiexec /i $MSIFileName” -InstallationFileLocation $MSIFilePath -AutoIdentifyFromInstallationFile -ForceForUnknownPublisher $True -MsiInstaller
    }
 
    Set-Location $PWDBegin
}

Add many applications to SCCM 2012

To add more than one application just create a CMD file with the following information and put in the same directory as the PowerShell script file.

powershell.exe .\AddCMApp.ps1 -MSIFilePath ‘\\sccm2012\pkg\7z920-x64\7z920-x64.msi’
powershell.exe .\AddCMApp.ps1 -MSIFilePath ‘\\sccm2012\pkg\XML Notepad 2007\XmlNotepad.msi’
powershell.exe .\AddCMApp.ps1 -MSIFilePath ‘\\sccm2012\pkg\RDCMan\RDCMan.msi’
powershell.exe .\AddCMApp.ps1 -MSIFilePath ‘\\sccm2012\pkg\Java(TM) 6 Update 33\Java_6_Update_33.msi’

Downloads

AddCMApp Apps

2 Responses to Configuration Manager 2012 Add MSI Application with Powershell

  1. ssake2010@gmail.com August 27, 2013 at 14:14 #

    First of all I will thanks lot you will share information to os .
    I opened powershell 3.0 on my sccm sp1 and try run script .

    I got error my file is not MSI File.

    PS P01:\> C:\script\AddCMApp.ps1
    cmdlet AddCMApp.ps1 at command pipeline position 1
    Supply values for the following parameters:
    MSIFilePath: “\\SCCMP01\Deploy\Appl$\Adobe\Adobe Acrobat 10\AcroPro.msi”
    FileName: AcroPro.msi”
    ERROR – The file “AcroPro.msi”” is not a MSI file

    • Jonathan Andersson August 27, 2013 at 21:11 #

      Hi,
      Nice to hear you have use for the script.

      Could you please try to run it on one line like this?
      .\AddCMApp.ps1 –MSIFilePath “\\SCCMP01\Deploy\Appl$\Adobe\Adobe Acrobat 10\AcroPro.msi”

      Please write me back if that worked for you.

      Regards,
      Jonathan

Leave a Reply