|
| 1 | +function Get-CIPPMSPAppInstallCommand { |
| 2 | + <# |
| 3 | + .SYNOPSIS |
| 4 | + Builds the install/uninstall command lines for an MSP RMM app for a single tenant. |
| 5 | + .DESCRIPTION |
| 6 | + Shared by Invoke-AddMSPApp (manual/queue deploy) and New-CIPPIntuneAppDeployment |
| 7 | + (the 'Deploy Intune Application Template' standard). Each parameter value may be: |
| 8 | + - a flat string, optionally containing %CIPP variables% that resolve per-tenant |
| 9 | + (Application Template shape), or |
| 10 | + - an object keyed by tenant customerId (legacy per-tenant deploy shape). |
| 11 | + Values are resolved for the tenant, run through Get-CIPPTextReplacement so any |
| 12 | + %variables% are substituted with the tenant's value, then escaped with |
| 13 | + ConvertTo-CIPPSafePwshArg before being placed on the command line. |
| 14 | + .PARAMETER RmmName |
| 15 | + The MSP tool identifier (datto, syncro, Huntress, automate, cwcommand, ninja, NCentral). |
| 16 | + .PARAMETER Params |
| 17 | + The params object from the app config / request body. |
| 18 | + .PARAMETER Tenant |
| 19 | + The tenant object, requires customerId and defaultDomainName. |
| 20 | + .PARAMETER PackageName |
| 21 | + Package name for ninja/NCentral installs (not stored under params). |
| 22 | + #> |
| 23 | + [CmdletBinding()] |
| 24 | + param( |
| 25 | + [Parameter(Mandatory = $true)] |
| 26 | + [string]$RmmName, |
| 27 | + |
| 28 | + $Params, |
| 29 | + |
| 30 | + [Parameter(Mandatory = $true)] |
| 31 | + $Tenant, |
| 32 | + |
| 33 | + [string]$PackageName |
| 34 | + ) |
| 35 | + |
| 36 | + $InstallParams = [PSCustomObject]$Params |
| 37 | + |
| 38 | + # Resolve a raw parameter value for this tenant: pick the per-tenant keyed value when the |
| 39 | + # value is an object (legacy shape), otherwise use it as-is (template shape), then replace |
| 40 | + # any %CIPP variables% using the tenant context. Returns the raw (unescaped) string. |
| 41 | + function Resolve-MSPValue { |
| 42 | + param($Value) |
| 43 | + if ($null -eq $Value) { return '' } |
| 44 | + if ($Value -is [string]) { |
| 45 | + $Resolved = $Value |
| 46 | + } elseif ($Value -is [System.Collections.IDictionary]) { |
| 47 | + $Resolved = [string]$Value[$Tenant.customerId] |
| 48 | + } elseif ($Value -is [pscustomobject]) { |
| 49 | + $Resolved = [string]$Value.$($Tenant.customerId) |
| 50 | + } else { |
| 51 | + $Resolved = [string]$Value |
| 52 | + } |
| 53 | + if ($Resolved -match '%') { |
| 54 | + $Resolved = Get-CIPPTextReplacement -TenantFilter $Tenant.defaultDomainName -Text $Resolved |
| 55 | + } |
| 56 | + return $Resolved |
| 57 | + } |
| 58 | + |
| 59 | + $DetectionScriptContent = $null |
| 60 | + |
| 61 | + switch ($RmmName) { |
| 62 | + 'datto' { |
| 63 | + $DattoUrl = ConvertTo-CIPPSafePwshArg -Value (Resolve-MSPValue $InstallParams.DattoURL) |
| 64 | + $DattoGuid = ConvertTo-CIPPSafePwshArg -Value (Resolve-MSPValue $InstallParams.DattoGUID) |
| 65 | + $installCommandLine = "powershell.exe -ExecutionPolicy Bypass .\install.ps1 -URL $DattoUrl -GUID $DattoGuid" |
| 66 | + $uninstallCommandLine = 'powershell.exe -ExecutionPolicy Bypass .\uninstall.ps1' |
| 67 | + } |
| 68 | + 'ninja' { |
| 69 | + $NinjaPackage = ConvertTo-CIPPSafePwshArg -Value ([string]$PackageName) |
| 70 | + $installCommandLine = "powershell.exe -ExecutionPolicy Bypass .\install.ps1 -InstallParam $NinjaPackage" |
| 71 | + $uninstallCommandLine = 'powershell.exe -ExecutionPolicy Bypass .\uninstall.ps1' |
| 72 | + } |
| 73 | + 'Huntress' { |
| 74 | + $HuntressOrgKey = ConvertTo-CIPPSafePwshArg -Value (Resolve-MSPValue $InstallParams.Orgkey) |
| 75 | + $HuntressAccountKey = ConvertTo-CIPPSafePwshArg -Value (Resolve-MSPValue $InstallParams.AccountKey) |
| 76 | + $installCommandLine = "powershell.exe -ExecutionPolicy Bypass .\install.ps1 -OrgKey $HuntressOrgKey -acctkey $HuntressAccountKey" |
| 77 | + $uninstallCommandLine = 'powershell.exe -ExecutionPolicy Bypass .\install.ps1 -Uninstall' |
| 78 | + } |
| 79 | + 'syncro' { |
| 80 | + $SyncroUrl = ConvertTo-CIPPSafePwshArg -Value (Resolve-MSPValue $InstallParams.ClientURL) |
| 81 | + $installCommandLine = "powershell.exe -ExecutionPolicy Bypass .\install.ps1 -URL $SyncroUrl" |
| 82 | + $uninstallCommandLine = 'powershell.exe -ExecutionPolicy Bypass .\uninstall.ps1' |
| 83 | + } |
| 84 | + 'NCentral' { |
| 85 | + $NCentralPackage = ConvertTo-CIPPSafePwshArg -Value ([string]$PackageName) |
| 86 | + $installCommandLine = "powershell.exe -ExecutionPolicy Bypass .\install.ps1 -InstallParam $NCentralPackage" |
| 87 | + $uninstallCommandLine = 'powershell.exe -ExecutionPolicy Bypass .\uninstall.ps1' |
| 88 | + } |
| 89 | + 'automate' { |
| 90 | + $ServerRaw = Resolve-MSPValue $InstallParams.Server |
| 91 | + $AutomateServer = ConvertTo-CIPPSafePwshArg -Value $ServerRaw |
| 92 | + $AutomateInstallerToken = ConvertTo-CIPPSafePwshArg -Value (Resolve-MSPValue $InstallParams.InstallerToken) |
| 93 | + $AutomateLocationId = ConvertTo-CIPPSafePwshArg -Value (Resolve-MSPValue $InstallParams.LocationID) |
| 94 | + $installCommandLine = "c:\windows\sysnative\windowspowershell\v1.0\powershell.exe -ExecutionPolicy Bypass .\install.ps1 -Server $AutomateServer -InstallerToken $AutomateInstallerToken -LocationID $AutomateLocationId" |
| 95 | + $uninstallCommandLine = "c:\windows\sysnative\windowspowershell\v1.0\powershell.exe -ExecutionPolicy Bypass .\uninstall.ps1 -Server $AutomateServer" |
| 96 | + $DetectionScriptContent = (Get-Content 'AddMSPApp\automate.detection.ps1' -Raw) -replace '##SERVER##', $ServerRaw |
| 97 | + } |
| 98 | + 'cwcommand' { |
| 99 | + $CwClientUrl = ConvertTo-CIPPSafePwshArg -Value (Resolve-MSPValue $InstallParams.ClientURL) |
| 100 | + $installCommandLine = "powershell.exe -ExecutionPolicy Bypass .\install.ps1 -Url $CwClientUrl" |
| 101 | + $uninstallCommandLine = 'powershell.exe -ExecutionPolicy Bypass .\uninstall.ps1' |
| 102 | + } |
| 103 | + default { |
| 104 | + throw "Unknown MSP app type '$RmmName'" |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + return [PSCustomObject]@{ |
| 109 | + InstallCommandLine = $installCommandLine |
| 110 | + UninstallCommandLine = $uninstallCommandLine |
| 111 | + DetectionScriptContent = $DetectionScriptContent |
| 112 | + } |
| 113 | +} |
0 commit comments