import-appconfig.ps1

<#
    .NOTES
    ===========================================================================
     Created with: SAPIEN Technologies, Inc., PowerShell Studio 2018 v5.5.155
     Created on: 11/18/2018 7:17 PM
     Created by: whiggs
     Organization:
     Filename: import-appconfig.ps1
    ===========================================================================
    .DESCRIPTION
        A description of the file.
#>


function import-appsettings
{
        $jsonfile = Read-Host -Prompt "Please provide the full path for the json file containing box authentication info."
        $jsonfile = $jsonfile.Trim('"')
        If (!(Test-Path $jsonfile))
        {
            throw 'The path you provided is invalid. You will need to import a valid json file containing your box authentication information before you can use the module. Please see the readme for more details.'
        }
    
    Try
    {
        $json = Get-Content $jsonfile | ConvertFrom-Json -ErrorAction Stop
    }
    Catch
    {
        throw 'You did not select a valid box app config json file. There is a syntactical error in the file.'
    }
    If (!(Test-Path "HKCU:\Software\boxmodule"))
    {
        New-Item -Path "HKCU:\SOFTWARE" -Name "boxmodule" -Force
    }
    Try
    {
        Get-ItemProperty -Path "HKCU:\Software\boxmodule" -Name "enterpriseid" -ErrorAction Stop
    }
    Catch
    {
        Set-ItemProperty -Path "HKCU:\Software\boxmodule" -Name "enterpriseid" -Value $json.enterpriseID -Force
    }
    Try
    {
        Get-ItemProperty -Path "HKCU:\Software\boxmodule" -Name "clientid" -ErrorAction Stop
    }
    Catch
    {
        Set-ItemProperty -Path "HKCU:\Software\boxmodule" -Name "clientid" -Value $json.boxAppSettings.clientID -Force
    }
    Try
    {
        Get-ItemProperty -Path "HKCU:\Software\boxmodule" -Name "clientsecret" -ErrorAction Stop
    }
    Catch
    {
        Set-ItemProperty -Path "HKCU:\Software\boxmodule" -Name "clientsecret" -Value $json.boxAppSettings.clientSecret -Force
    }
    Try
    {
        Get-ItemProperty -Path "HKCU:\Software\boxmodule" -Name "passphrase" -ErrorAction Stop
    }
    Catch
    {
        Set-ItemProperty -Path "HKCU:\Software\boxmodule" -Name "passphrase" -Value $json.boxAppSettings.appAuth.passphrase -Force
    }
    Try
    {
        Get-ItemProperty -Path "HKCU:\Software\boxmodule" -Name "privatekey" -ErrorAction Stop
    }
    Catch
    {
        Set-ItemProperty -Path "HKCU:\Software\boxmodule" -Name "privatekey" -Value $json.boxAppSettings.appAuth.privateKey -Force
    }
    Try
    {
        Get-ItemProperty -Path "HKCU:\Software\boxmodule" -Name "publickeyid" -ErrorAction Stop
    }
    Catch
    {
        Set-ItemProperty -Path "HKCU:\Software\boxmodule" -Name "publickeyid" -Value $json.boxAppSettings.appAuth.publicKeyID -Force
    }
}

If (!(Test-Path "HKCU:\Software\boxmodule"))
{
    import-appsettings
}


    Try
    {
        $enterpriseid = Get-ItemProperty -Path "HKCU:\Software\boxmodule" -Name "enterpriseid" -ErrorAction Stop
    }
    Catch
    {
        import-appsettings
    }
    Try
    {
        $clientid = Get-ItemProperty -Path "HKCU:\Software\boxmodule" -Name "clientid" -ErrorAction Stop
    }
    Catch
    {
        import-appsettings
    }
    Try
    {
        $clientsecret = Get-ItemProperty -Path "HKCU:\Software\boxmodule" -Name "clientsecret" -ErrorAction Stop
    }
    Catch
    {
        import-appsettings
    }
    Try
    {
        $passphrase = Get-ItemProperty -Path "HKCU:\Software\boxmodule" -Name "passphrase" -ErrorAction Stop
    }
    Catch
    {
        import-appsettings
    }
    Try
    {
        $privatekey = Get-ItemProperty -Path "HKCU:\Software\boxmodule" -Name "privatekey" -ErrorAction Stop
    }
    Catch
    {
        import-appsettings
    }
    Try
    {
        $publickeyid = Get-ItemProperty -Path "HKCU:\Software\boxmodule" -Name "publickeyid" -ErrorAction Stop
    }
    Catch
    {
        import-appsettings
    }
    $boxconfig = New-Object -TypeName Box.v2.Config.Boxconfig($clientID.clientid, $clientSecret.clientsecret, $enterpriseID.enterpriseid, $privateKey.privatekey, $passphrase.passphrase, $publicKeyID.publickeyid)
    $boxJWT = New-Object -TypeName Box.V2.JWTAuth.BoxJWTAuth($boxconfig)
    $boxjwt
    $global:tokenreal = $boxJWT.AdminToken()
    $global:adminclient = $boxjwt.AdminClient($global:tokenreal)
    $global:adminclient