poshbox.psm1

<#
    .NOTES
    --------------------------------------------------------------------------------
     Code generated by: SAPIEN Technologies, Inc., PowerShell Studio 2019 v5.6.156
     Generated on: 12/27/2018 8:14 AM
     Generated by: whiggs
    --------------------------------------------------------------------------------
    .DESCRIPTION
        Script generated by PowerShell Studio 2019
#>



    <#
        ===========================================================================
         Created with: SAPIEN Technologies, Inc., PowerShell Studio 2018 v5.5.155
         Created on: 11/18/2018 11:55 AM
         Created by: whiggs
         Organization:
         Filename: poshbox.psm1
        -------------------------------------------------------------------------
         Module Name: poshbox
        ===========================================================================
    #>

    <#
    . .\import-appconfig.ps1
    $boxob = Get-ItemProperty -Path "HKCU:\Software\boxmodule" -ErrorAction Stop
    $boxconfig = New-Object -TypeName Box.v2.Config.Boxconfig -Argumentlist $boxob.clientid, $boxob.clientsecret, $boxob.enterpriseid, $boxob.privatekey, $boxob.passphrase, $boxob.publickeyid
    $boxJWT = New-Object -TypeName Box.V2.JWTAuth.BoxJWTAuth -Argumentlist $boxconfig
    $global:tokenreal = $boxJWT.AdminToken
    $global:adminclient = $boxjwt.AdminClient($global:tokenreal)
    $global:adminclient
    #>

    function connect-box
    {
        [CmdletBinding(DefaultParameterSetName = 'reg')]
        param
        (
            [Parameter(ParameterSetName = 'file', Mandatory = $true)]
            [ValidatePattern('^([a-zA-Z]:\\|\\\\)(((?![<>:"/\\|?*]).)+((?<![ .])\\)?)*\.json$')]
            [ValidateScript({Test-Path $_ -PathType Leaf})]
            [String]$path,
            [ValidatePattern("\d{4,}")]
            [string]$id
        )
        If ($path)
        {
            If (Test-Path "HKCU:\Software\boxmodule")
            {
                Remove-Item "HKCU:\Software\boxmodule" -Force
            }
            New-Item "HKCU:\Software\boxmodule" -Force
            $json = Get-Content $path | ConvertFrom-Json
            Set-ItemProperty -Path "HKCU:\Software\boxmodule" -Name "enterpriseid" -Value $json.enterpriseID -Force
            Set-ItemProperty -Path "HKCU:\Software\boxmodule" -Name "clientid" -Value $json.boxAppSettings.clientID -Force
            Set-ItemProperty -Path "HKCU:\Software\boxmodule" -Name "clientsecret" -Value $json.boxAppSettings.clientSecret -Force
            Set-ItemProperty -Path "HKCU:\Software\boxmodule" -Name "passphrase" -Value $json.boxAppSettings.appAuth.passphrase -Force
            Set-ItemProperty -Path "HKCU:\Software\boxmodule" -Name "privatekey" -Value $json.boxAppSettings.appAuth.privateKey -Force
            Set-ItemProperty -Path "HKCU:\Software\boxmodule" -Name "publickeyid" -Value $json.boxAppSettings.appAuth.publicKeyID -Force
        }
        Try
        {
            $boxob = Get-ItemProperty -Path "HKCU:\Software\boxmodule" -ErrorAction Stop
        }
        Catch
        {
            throw "The box authentication values have not been saved. Run this command again and provide the path to the json file with the authentication information."
        }
        If (($boxob.psobject.Properties.name -notcontains "enterpriseid") -or ($boxob.psobject.Properties.name -notcontains "clientid") -or ($boxob.psobject.Properties.name -notcontains "clientSecret") -or ($boxob.psobject.Properties.name -notcontains "passphrase") -or ($boxob.psobject.Properties.name -notcontains "privatekey") -or ($boxob.psobject.Properties.name -notcontains "publickeyid"))
        {
            throw "Box authentication values are missing. Run this command again and provide the path to the json file with the authentication information"
        }
        $boxconfig = New-Object -TypeName Box.v2.Config.Boxconfig -Argumentlist $boxob.clientid, $boxob.clientsecret, $boxob.enterpriseid, $boxob.privatekey, $boxob.passphrase, $boxob.publickeyid
        $boxJWT = New-Object -TypeName Box.V2.JWTAuth.BoxJWTAuth -Argumentlist $boxconfig
        $global:tokenreal = $boxJWT.AdminToken
        If (-not $id)
        {
            $global:adminclient = $boxjwt.AdminClient($tokenreal)
        }
        Else
        {
            $global:adminclient = $boxjwt.AdminClient($tokenreal, $id)
        }
        #return $global:adminclient
    }
    function get-boxusers
    {
        [CmdletBinding(DefaultParameterSetName = "autopage")]
        param
        (
            [Parameter()]
            [string]$SearchString,
            [Parameter(Mandatory = $true, ParameterSetName = "noautopage")]
            [ValidateRange(1, 1000)]
            [int]$limit,
            [Parameter(Mandatory = $true, ParameterSetName = "noautopage")]
            [int]$offset,
            [Parameter()]
            [String[]]$userinfo,
            [String]$usertype,
            [String]$externalappuserid,
            [Parameter(Mandatory = $true, ParameterSetName = 'autopage')]
            [Switch]$autopaginate
        )
        If ($PSCmdlet.ParameterSetName -like "autopage")
        {
            $usertask = $adminclient.UsersManager.GetEnterpriseUsersAsync($PSBoundParameters.SearchString, 0, 1000, $PSBoundParameters.userinfo, $PSBoundParameters.usertype, $PSBoundParameters.externalappuserid, $true)
        }
        Else
        {
            $usertask = $adminclient.UsersManager.GetEnterpriseUsersAsync($PSBoundParameters.SearchString, $PSBoundParameters.offset, $PSBoundParameters.limit, $PSBoundParameters.userinfo, $PSBoundParameters.usertype, $PSBoundParameters.externalappuserid, $false)
        }
        $usertask.Wait()
        If ($usertask.IsFaulted -eq $true)
        {
            return $usertask.Exception.InnerException
        }
        else
        {
            $psob = New-Object -TypeName System.Management.Automation.PSObject
            $psob | Add-Member -MemberType NoteProperty -Name "entries" -Value $usertask.Result.Entries
            $psob | Add-Member -MemberType NoteProperty -Name "TotalCount" -Value $usertask.Result.TotalCount
            return $psob
        }
    }
    function set-boxuserlogin
    {
        param
        (
            [parameter(Mandatory = $true)]
            [System.String]$id,
            [parameter(Mandatory = $true)]
            [string]$newlogin,
            [Parameter()]
            [String[]]$userinfo
        )
        $change = $adminclient.UsersManager.ChangeUsersLoginAsync($id, $newlogin, $PSBoundParameters.userinfo)
        $change.wait()
        If ($change.IsFaulted -eq $true)
        {
            $result = $change.Exception.InnerException
        }
        Else
        {
            $result = $change.Status
        }
        return $result
    }
    function new-boxuser
    {
        param
        (
            [parameter(Mandatory = $true)]
            [hashtable]$userdetails,
            [String[]]$userinfo
        )
        If (($userdetails.Keys -notcontains "name") -and ($userdetails.Keys -notcontains "login"))
        {
            Write-Error "The hashtable does not define the `"name`" or `"login`" keys. These must be present to create the new user."
            return
        }
        Else
        {
            $userrequest = New-Object Box.V2.Models.BoxUserRequest -Property $userdetails
            $newuser = $adminclient.UsersManager.CreateEnterpriseUserAsync($userrequest, $PSBoundParameters.userinfo)
            $newuser.Wait()
            If ($newuser.IsFaulted -eq $true)
            {
                return $newuser.Exception.InnerException
            }
            Else
            {
                return $newuser.Result
            }
        }
    }
    
    function add-boxemailalias
    {
        [CmdletBinding()]
        param (
            [Parameter(Mandatory = $true)]
            [String]$id,
            [Parameter(Mandatory = $true)]
            [String]$emailalias
        )
        $emailaliasproc = $adminclient.UsersManager.AddEmailAliasAsync($id, $emailalias)
        $emailaliasproc.Wait()
        If ($emailaliasproc.IsFaulted -eq $true)
        {
            return $emailaliasproc.Exception.InnerException
        }
        Else
        {
            return $emailaliasproc.Result
        }
    }
    function delete-boxuser
    {
        [CmdletBinding()]
        param (
            [Parameter(Mandatory = $true)]
            [String]$id,
            [Switch]$notify,
            [Switch]$force
        )
        If ($notify)
        {
            If ($force)
            {
                $delete = $adminclient.UsersManager.DeleteEnterpriseUserAsync($id, $true, $true)
            }
            Else
            {
                $delete = $adminclient.UsersManager.DeleteEnterpriseUserAsync($id, $true, $false)
            }
        }
        Else
        {
            If ($force)
            {
                $delete = $adminclient.UsersManager.DeleteEnterpriseUserAsync($id, $false, $true)
            }
            Else
            {
                $delete = $adminclient.UsersManager.DeleteEnterpriseUserAsync($id, $false, $false)
            }
        }
        $delete.Wait()
        If ($delete.IsFaulted -eq $true)
        {
            Return $delete.Exception.InnerException
        }
        Else
        {
            return $delete.Status
        }
        
    }
    
    function transfer-boxusercontent
    {
        [CmdletBinding()]
        param (
            [Parameter(Mandatory = $true)]
            [String]$id,
            [Parameter(Mandatory = $true)]
            [String]$ownerid,
            [Switch]$notify
        )
        If ($notify)
        {
            $transfer = $adminclient.UsersManager.MoveUserFolderAsync($id, $ownerid, "0", $true)
        }
        Else
        {
            $transfer = $adminclient.UsersManager.MoveUserFolderAsync($id, $ownerid, "0", $false)
        }
        $transfer.Wait()
        If ($transfer.IsFaulted -eq $true)
        {
            $transfer.Exception.InnerException
        }
        Else
        {
            $transfer.Result
        }
    }
    
    function upload-boxfile
    {
        [CmdletBinding()]
        param (
            [Parameter(Mandatory = $true)]
            [String]$filename,
            [Parameter(Mandatory = $true)]
            [String]$parentdirid,
            [Parameter(Mandatory = $true)]
            [String]$path,
            [String[]]$fields,
            [Parameter(Mandatory = $true)]
            [system.TimeSpan]$time
        )
        If (!(Test-Path $path))
        {
            Write-Error "The file path you provided does not exist."
            Return
        }
        $file = [System.IO.File]::OpenRead($path)
        $request = @{
            name = "$filename"
            parent = @{
                id = "$parentdirid"
            }
        }
        $upload = $adminclient.FilesManager.UploadAsync($request, $file, $PSBoundParameters.fields, $PSBoundParameters.time)
        $upload.Wait()
        If ($upload.IsFaulted -eq $true)
        {
            return $upload.Exception.InnerException
        }
        Else
        {
            return $upload.Result
        }
    }
    
    Export-ModuleMember -Function connect-box, get-boxusers, set-boxuserlogin, new-boxuser, delete-boxuser, add-boxemailalias, transfer-boxusercontent, upload-boxfile