Scripts/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 Get-FileName #($initialDirectory)
{
    [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") | Out-Null
    
    $OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
    $OpenFileDialog.Title = "Select the Box app config json"
    #$OpenFileDialog.initialDirectory = $initialDirectory
    $OpenFileDialog.filter = "Json files (*.json) | *.json"
    $OpenFileDialog.ShowDialog() | Out-Null
    $OpenFileDialog.filename
}
function import-appsettings
{
    $jsonfile = Get-FileName
    If (($jsonfile -eq $null) -or ($jsonfile -like ""))
    {
        throw 'You did not import a box app config json file. You will need to import this file before you can use the module. Please see the readme for more details.'
    }
    Else
    {
        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
}
Else
{
    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($tokenreal)
    $adminclient
}