Public/New-SyncroRMMApplication.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
function New-SyncroRMMApplication {
    [CmdletBinding()]
    Param
    (
        [parameter(Position = 0, Mandatory = $true)]
        [ValidateNotNullOrEmpty()][String]$SyncroURL,
        
        [parameter(Position = 1, Mandatory = $true)]
        [ValidateNotNullOrEmpty()][string]$SyncroAPIKey,
        
        [parameter(Position = 2, Mandatory = $true)]
        [ValidateNotNullOrEmpty()][String]$SyncroPolicyID,

        [parameter(Position = 3, Mandatory = $true)]
        [ValidateNotNullOrEmpty()][String]$SyncroInstallerURL,

        [parameter(Position = 4, Mandatory = $true)]
        [ValidateNotNullOrEmpty()][String]$ApplicationId,

        [parameter(Position = 5, Mandatory = $true)]
        [ValidateNotNullOrEmpty()][String]$ApplicationSecret,

        [parameter(Position = 6, Mandatory = $true)]
        [ValidateNotNullOrEmpty()][String]$YourTenantID,

        [parameter(Position = 7, Mandatory = $true)]
        [ValidateNotNullOrEmpty()][String]$refreshtoken,

        [parameter(Position = 8, Mandatory = $true)]
        [ValidateNotNullOrEmpty()][bool]$AssignToAllDevices,

        [parameter(Position = 9, Mandatory = $true)]
        [ValidateNotNullOrEmpty()][string]$PackageName,
        
        [parameter(Position = 10, Mandatory = $true)]
        [ValidateNotNullOrEmpty()][string]$SyncroTenantVariable

    )
    Write-Verbose "Connecting to the Graph API for processing all clients"
    Connect-GraphAPI -ApplicationId $ApplicationId -ApplicationSecret $ApplicationSecret -Tenantid $YourTenantID -RefreshToken $refreshtoken
    Write-Verbose "Connecting to DRMM to get all tenants using tenant variable $SyncroTenantVariablee"
    $Tenants = Get-SyncroTenantInfo -URL $SyncroURL -key $SyncroAPIKey -VariableName $SyncroTenantVariable
    foreach ($Tenant in $Tenants) {
        write-verbose "Starting process for tenant $($tenant.name)"
        try {
            write-verbose "Downloading client for $($tenant.name) / $($tenant.tenantid)"
            #need to actually add the downloading of the agent.
            $null = New-item -ItemType Directory "$ENV:Temp\$($tenant.id)" -ErrorAction SilentlyContinue
            (New-Object System.Net.WebClient).DownloadFile("$($SyncroinstallerURL)", "$ENV:Temp\$($tenant.id)\SyncroSetup.exe")
            $params = @{
                packagename          = $PackageName
                packageversion       = '1.0'
                packageinstallcmd    = "SyncroSetup.exe --console --customerid $($tenant.id) --policyid $SyncroPolicyID"
                packageuninstallcmd  = "SyncroSetup.exe /X"
                packagedetectionpath = "C:\Program Files\RepairTech\Syncro"
                packagedetectionfile = "Update.exe"
                InstallerPath        = "$ENV:Temp\$($tenant.id)\SyncroSetup.exe"
                TenantID             = $Tenant.tenantid
            }
            write-verbose "Starting to create package for $($tenant.name)"
            $NewPackage = New-IntunePackage @Params
            if ($AssignToAllDevices) { Set-IntunePackageAssign -PackageID $NewPackage.id }
            $Cleanup = Get-ChildItem "$ENV:Temp\$($tenant.id)" | Remove-Item -Force
        }
        catch {
            write-error "Failed for tenant $($tenant.name): $($_.Exception.Message)"
        }

    }
}