try
{
    Write-Host "Load XML config file" -foregroundcolor black -backgroundcolor yellow

    $xdoc = [xml] (get-content "C:\\config.xml")

    $url = $xdoc.Tenant.Admin.site $username = $xdoc.Tenant.Admin.username $password = $xdoc.Tenant.Admin.password $appfilepath = $xdoc.Tenant.appfilepath $Password = $password |ConvertTo-SecureString -AsPlainText -force

    Write-Host "Global variables loaded succeefully" -foregroundcolor black -backgroundcolor Green } catch { Write-Host "Problem in loading the XML or parsing the variables : $_.Exception.Message" -foregroundcolor black -backgroundcolor Red return }

    try 
    { 
        Write-Host "Load CSOM DLLs" -foregroundcolor black -backgroundcolor yellow Set-Location
        $loadInfo1 = [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client") $loadInfo2 = [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime")
        Write-Host "CSOM DLLs loaded succeefully" -foregroundcolor black -backgroundcolor Green
    }
    catch { 
        Write-Host "Problem in loading CSOM DLLs : $_.Exception.Message" -foregroundcolor black -backgroundcolor Red 
        return 
    }

    try { 
        Write-Host "Authenticate tenant site $url and get ClientContext object" -foregroundcolor black -backgroundcolor yellow
        $context = New-Object Microsoft.SharePoint.Client.ClientContext($url) $credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $password) $context.Credentials = $credentials
        $web = $context.Web $site = $context.Site $context.Load($web) $context.Load($site) $context.ExecuteQuery()
        Write-Host "Authentication to online site $url and get ClientContext DLLS succeeful" -foregroundcolor black -backgroundcolor Green
    }
    catch 
    {
        Write-Host "Unable to authenticate to online site. Error : $_.Exception.Message" -foregroundcolor black -backgroundcolor Red 
        return 
    }
    
    $appIoStream = New-Object IO.FileStream("C:\\TestApp.app" ,[System.IO.FileMode]::Open) 
    $appInstance = $web.LoadAndInstallApp($appIoStream) | Out-Null $context.ExecuteQuery() 
    Write-Host $appInstance.Id
}
catch
{
    Write-Host "Unable to Install App Error : $_.Exception.Message" -foregroundcolor black -backgroundcolor Red 
    return
}