# Generate Application Content Items

Using PowerShell can be a nice way to automate the generate of Application content items and save a bit of time vs using the dashboard.  There are a few steps in creating content items with PowerShell, first making the "Shell" Content Item, then the version item inside of it.  Just a note, this is different than importing one you exported from another DeployR server, this is creating a completely new content item from scratch in an automated fashion.&#x20;

You'll be leveraging a few different DeployR Functions

* Get-DeployRApplication
* New-DeployRContentItem
* New-DeployRContentItemVersion

In this Example, we'll be creating the 7zip Application Content Item.\
I've downloaded the 7zip MSI to my sources folder and I have my installation command line, I'll plug them in as variables.\
\
First we'll create the Content Item Shell, once completed, we'll see this in the Dashboard.

{% hint style="info" %}
Note, anytime we're using the DeployR functions and interacting with DeployR, you'll need to import and connect to DeployR, you can find this on the Scripting for [DeployR Server page](https://documentation.2pintsoftware.com/deployr/powershell-modules/scripting-for-deployr-server)
{% endhint %}

<pre class="language-ps1"><code class="lang-ps1"><strong>$AppName = '7zip'
</strong>$NewDRCI = New-DeployRContentItem -Type Folder -Name $AppName -Description "Script Generated" -Purpose Application
</code></pre>

Once the Content Item shell is created, we'll populate the version with the details:

```ps1
$AppSourceFolder = 'D:\DeployRSources\Applications\7-Zip\26.00'
$AppDescription = '26.00'
$InstallationCommandLine = 'msiexec.exe /i "7z2600-x64.msi" /quiet /norestart'
New-DeployRContentItemVersion -ContentItemId $NewDRCI.id -SourceFolder $AppSourceFolder -InstallationCommandLine $InstallationCommandLine -Description $AppDescription
```

Overall, it's pretty straight forward to create an Application Content Item in DeployR.  Once you know the basics, you can start to get more fancy, I've created several functions to simplify automation of generating these applications.  These functions are:

* New-DeployRApp
  * This is used for creating a brand new app
* Test-DeployRAppExists
  * Used for automation when automating generating apps to determine if you're trying to generate one that is already there.
* Update-DeployRApp
  * Used to update an App already in DeployR by appending a new Version

```powershell
#Function to Create New Apps in DeployR
Function New-DeployRApp {
    Param (
        [string]$AppName,
        [string]$AppSourceFolder,
        [string]$AppDescription = "No Description Provided",
        [string]$InstallationCommandLine = ""
    )

    $NewDRCI = New-DeployRContentItem -Type Folder -Name $AppName -Description "Script Generated" -Purpose Application
    New-DeployRContentItemVersion -ContentItemId $NewDRCI.id -SourceFolder $AppSourceFolder -InstallationCommandLine $InstallationCommandLine -Description $AppDescription
}

#Function to Test if App is already in DeployR
Function Test-DeployRAppExists {
    Param (
        [string]$AppName,
        [string]$AppVersion,
        [Parameter(Mandatory=$false)]
        [PSObject[]]$AllApps
    )

    # Use provided AllApps collection if available, otherwise query DeployR
    if ($null -eq $AllApps -or $AllApps.Count -eq 0) {
        $AllApps = Get-DeployRApplication
    }

    $existingApp = $AllApps | Where-Object { $_.Name -eq $AppName } -ErrorAction SilentlyContinue
    if ($null -ne $existingApp) {
        $LatestVersion = ($existingApp.versions | Select-Object -ExpandProperty Description | Sort-Object -Descending | Select-Object -First 1)
        return [PSCustomObject]@{
            Name          = $existingApp.Name
            LatestVersion = $LatestVersion
        }
    } else {
        return $false
    }
}

##Function to Update App in DeployR by adding new Version (Append new version number)
Function Update-DeployRApp {
    Param (
        [string]$AppName,
        [string]$AppVersion = "No Description Provided",
        [string]$AppSourceFolder,
        [string]$InstallationCommandLine = "",
        [Parameter(Mandatory=$false)]
        [PSObject[]]$AllApps
    )

    # Use provided AllApps collection if available, otherwise query DeployR
    if ($null -eq $AllApps -or $AllApps.Count -eq 0) {
        $existingApp = Get-DeployRApplication -Name $AppName -ErrorAction Stop
    } else {
        $existingApp = $AllApps | Where-Object { $_.Name -eq $AppName } -ErrorAction SilentlyContinue | Select-Object -First 1
    }
    
    if ($null -ne $existingApp) {
        Write-Host "Updating DeployR Application: $AppName"
        $NewDRCIV = New-DeployRContentItemVersion -ContentItemId $existingApp.id -SourceFolder $AppSourceFolder -InstallationCommandLine $InstallationCommandLine -Description $AppVersion
    } else {
        Write-Host "DeployR Application not found: $AppName"
    }
}

```

With these functions, I've automated creating and updating several applications automatically in my DeployR Lab.  The full script is on our GitHub, <https://github.com/2pintsoftware/2Pint-DeployR/blob/main/Automation/Generate-AppContentItems.ps1>&#x20;
