Silently remote deployment the GLPI Agent

Whenever you are using GLPI for your asset and ticket management it can be very important to use up to date asset information. The GLPI agent and GLPI inventory plugin when used together are a great set of tools to allow for this process to be automated. The only thing that can be a hassle is installing the GLPI agent on your endpoints.

I’ve created an deployment script that might help you or give you inspiration at the very least to complete your GLPI agent rollout. The script is not perfect but it works and allows the usage of nightly builds if you so require.

I made some assumptions on what flags you would want and currently they are not variables in the script so keep that in mind!

[CmdletBinding()]
param
(
    [Parameter(Mandatory=$true)][string]$ComputerName,
    [Parameter(Mandatory=$true)][pscredential]$Credential,
    [string]$Version = "Latest",
    [string]$Server = "https://glpi.example.com"
)

#Install arguments
$InstallArguments = "/qn ADDLOCAL=feat_COLLECT SERVER=$Server/marketplace/glpiinventory AGENTMONITOR=1 AGENTMONITOR_NEWTICKET_URL=$Server EXECMODE=1 RUNNOW=1 SCAN_PROFILES=1 HTTPD_IP=127.0.0.1"

#Temporary files to be used on the client machine
$TemporaryDirectory = "C:\GLPI_Install"
$TemporaryDownloadedAgent = "C:\GLPI_Install\GLPI-Agent.msi"

#Download link for the specified version and the time to wait for the download, Latest checks for the latest released GitHub version
$DownloadTimeSeconds = 10
if($Version -eq 'Latest')
{
    $ReleasesUri = 'https://github.com/glpi-project/glpi-agent/releases/latest'
    $DownloadLink = ((Invoke-WebRequest $ReleasesUri).links | Where-Object href -like "*-x64.msi").href
}
elseif ($Version -like "*-git*")
{
    $DownloadLink = "https://nightly.glpi-project.org/glpi-agent/GLPI-Agent-$Version-x64.msi"
}
else {
    $DownloadLink = "https://github.com/glpi-project/glpi-agent/releases/download/$Version/GLPI-Agent-$Version-x64.msi"
}

if (-Not ($DownloadLink))
{
    Write-Error "Something went wrong with retrieval of the GLPI download link"
    Exit
}

#Creation a connection to the client machine
Write-Verbose "Creating a new session to the specified computer"
$Session = New-PSSession $ComputerName -Credential $Credential

if (-Not($Session))
{
    Write-Error "Session could not be created, please try again"
    exit
}

#Installation
Write-Verbose "Performing download and installation of GLPI Agent"
Invoke-Command -Session $Session -ScriptBlock {
    if (-Not (Test-Path $Using:TemporaryDirectory))
    {
            New-Item -ItemType Directory -Path $Using:TemporaryDirectory | Out-Null
    }

    if (Test-Path $Using:TemporaryDownloadedAgent) {
        Remove-Item $Using:TemporaryDownloadedAgent
    }

    Invoke-WebRequest -Uri $Using:DownloadLink -OutFile $Using:TemporaryDownloadedAgent

    Start-Sleep -Seconds $Using:DownloadTimeSeconds

    if (Test-Path $Using:TemporaryDownloadedAgent)
    {
        Start-Process $Using:TemporaryDownloadedAgent -ArgumentList $Using:InstallArguments -Wait
    }

    Remove-Item $Using:TemporaryDirectory -Recurse -Force
}

#Removal of used connection
Write-Verbose "Remove used session as cleanup"
Remove-PSSession $Session

Write-Verbose "Script has been completed"

Leave a Comment