poshbox.psm1

<#
    .NOTES
    --------------------------------------------------------------------------------
     Code generated by: SAPIEN Technologies, Inc., PowerShell Studio 2019 v5.6.156
     Generated on: 12/24/2018 8:40 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
        ===========================================================================
    #>

    
    $enterpriseid = Get-ItemProperty -Path "HKCU:\Software\boxmodule" -Name "enterpriseid" -ErrorAction Stop
    $clientid = Get-ItemProperty -Path "HKCU:\Software\boxmodule" -Name "clientid" -ErrorAction Stop
    $clientsecret = Get-ItemProperty -Path "HKCU:\Software\boxmodule" -Name "clientsecret" -ErrorAction Stop
    $passphrase = Get-ItemProperty -Path "HKCU:\Software\boxmodule" -Name "passphrase" -ErrorAction Stop
    $privatekey = Get-ItemProperty -Path "HKCU:\Software\boxmodule" -Name "privatekey" -ErrorAction Stop
    $publickeyid = Get-ItemProperty -Path "HKCU:\Software\boxmodule" -Name "publickeyid" -ErrorAction Stop
    $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)
    
    function connect-box
    {
        param
        (
            [Parameter()]
            [string]$id
        )
        $enterpriseid = Get-ItemProperty -Path "HKCU:\Software\boxmodule" -Name "enterpriseid" -ErrorAction Stop
        $clientid = Get-ItemProperty -Path "HKCU:\Software\boxmodule" -Name "clientid" -ErrorAction Stop
        $clientsecret = Get-ItemProperty -Path "HKCU:\Software\boxmodule" -Name "clientsecret" -ErrorAction Stop
        $passphrase = Get-ItemProperty -Path "HKCU:\Software\boxmodule" -Name "passphrase" -ErrorAction Stop
        $privatekey = Get-ItemProperty -Path "HKCU:\Software\boxmodule" -Name "privatekey" -ErrorAction Stop
        $publickeyid = Get-ItemProperty -Path "HKCU:\Software\boxmodule" -Name "publickeyid" -ErrorAction Stop
        $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()
        If (-not $id)
        {
            $global:adminclient = $boxjwt.AdminClient($global:tokenreal)
        }
        Else
        {
            $global:adminclient = $boxjwt.AdminClient($global: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 = $global:adminclient.UsersManager.GetEnterpriseUsersAsync($PSBoundParameters.SearchString, 0, 1000, $PSBoundParameters.userinfo, $PSBoundParameters.usertype, $PSBoundParameters.externalappuserid, $true)
        }
        Else
        {
            $usertask = $global: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 = $global: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 = $global: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 = $global: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 = $global:adminclient.UsersManager.DeleteEnterpriseUserAsync($id, $true, $true)
            }
            Else
            {
                $delete = $global:adminclient.UsersManager.DeleteEnterpriseUserAsync($id, $true, $false)
            }
        }
        Else
        {
            If ($force)
            {
                $delete = $global:adminclient.UsersManager.DeleteEnterpriseUserAsync($id, $false, $true)
            }
            Else
            {
                $delete = $global: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 = $global:adminclient.UsersManager.MoveUserFolderAsync($id, $ownerid, "0", $true)
        }
        Else
        {
            $transfer = $global: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 = $global: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