poshbox.psm1

<#
    .NOTES
    --------------------------------------------------------------------------------
     Code generated by: SAPIEN Technologies, Inc., PowerShell Studio 2019 v5.6.157
     Generated on: 1/21/2019 2:29 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
        ===========================================================================
    #>

    
    #.EXTERNALHELP en-US\poshbox-help.xml
    
    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[]]$fields,
            [String]$usertype,
            [String]$externalappuserid,
            [Parameter(ParameterSetName = 'autopage')]
            [Switch]$autopaginate
        )
        If ($PSCmdlet.ParameterSetName -like "autopage")
        {
            $usertask = $adminclient.UsersManager.GetEnterpriseUsersAsync($PSBoundParameters.SearchString, 0, 1000, $PSBoundParameters.fields, $PSBoundParameters.usertype, $PSBoundParameters.externalappuserid, $true)
        }
        Else
        {
            $usertask = $adminclient.UsersManager.GetEnterpriseUsersAsync($PSBoundParameters.SearchString, $PSBoundParameters.offset, $PSBoundParameters.limit, $PSBoundParameters.fields, $PSBoundParameters.usertype, $PSBoundParameters.externalappuserid, $false)
        }
        $usertask.Wait()
        If ($usertask.IsFaulted -eq $true)
        {
            Write-error $usertask.Exception.InnerException
            return
        }
        else
        {
            return $usertask.Result
        }
    }
    function set-boxuserlogin
    {
        param
        (
            [parameter(Mandatory = $true)]
            [System.String]$id,
            [parameter(Mandatory = $true)]
            [string]$newlogin,
            [Parameter()]
            [String[]]$fields
        )
        If ($PSBoundParameters.Keys -notcontains "fields")
        {
            $change = $adminclient.UsersManager.ChangeUsersLoginAsync($id, $newlogin)
        }
        Else
        {
            $change = $adminclient.UsersManager.ChangeUsersLoginAsync($id, $newlogin, $fields)
        }
        $change.wait()
        If ($change.IsFaulted -eq $true)
        {
            Write-Error $change.Exception.InnerException
            return
        }
        Else
        {
            return $change.Status
        }
    }
    function new-boxuser
    {
        param
        (
            [parameter(Mandatory = $true)]
            [hashtable]$userdetails,
            [String[]]$fields
        )
        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
            If ($PSBoundParameters -notcontains "fields")
            {
                $newuser = $adminclient.UsersManager.CreateEnterpriseUserAsync($userrequest)
            }
            Else
            {
                $newuser = $adminclient.UsersManager.CreateEnterpriseUserAsync($userrequest, $fields)
            }
            
            $newuser.Wait()
            If ($newuser.IsFaulted -eq $true)
            {
                Write-error $newuser.Exception.InnerException
                return
            }
            Else
            {
                return $newuser.Result
            }
        }
    }
    
    function new-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)
        {
            Write-error $emailaliasproc.Exception.InnerException
            return
        }
        Else
        {
            return $emailaliasproc.Result
        }
    }
    function remove-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)
        {
            Write-Error $delete.Exception.InnerException
            return
        }
        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]$timespan
        )
        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)
        {
            Write-error $upload.Exception.InnerException
            return
        }
        Else
        {
            return $upload.Result
        }
    }
    Function remove-boxemailalias
    {
        param (
            [Parameter(Mandatory = $true)]
            [String]$userid,
            [Parameter(Mandatory = $true)]
            [String]$aliasid
        )
        $removealias = $adminclient.UsersManager.DeleteEmailAliasAsync($userid, $aliasid)
        $removealias.Wait()
        If ($removealias.IsFaulted -eq $true)
        {
            Write-error $removealias.Exception.InnerException
            return
        }
        Else
        {
            return $removealias.Result
        }
    }
    function Get-boxemailalias
    {
        param (
            [Parameter(Mandatory = $true)]
            [String]$userid
        )
        $getalias = $adminclient.UsersManager.GetEmailAliasesAsync($userid)
        $getalias.Wait()
        If ($getalias.IsFaulted -eq $true)
        {
            Write-error $getalias.Exception.InnerException
            return
        }
        Else
        {
            return $getalias.Result
        }
    }
    function Get-boxuserinfo
    {
        param (
            [Parameter(Mandatory = $true)]
            [String]$userid
        )
        $userinfo = $adminclient.UsersManager.GetUserInformationAsync($userid)
        $userinfo.Wait()
        If ($userinfo.IsFaulted -eq $true)
        {
            Write-error $userinfo.Exception.InnerException
            return
        }
        Else
        {
            return $userinfo.Result
        }
    }
    function invite-usertobox
    {
        param (
            [Parameter(Mandatory = $true)]
            [String]$exuseremail,
            [System.String[]]$fields
        )
        Try
        {
            $boxob = Get-ItemProperty -Path "HKCU:\Software\boxmodule" -ErrorAction Stop
        }
        Catch
        {
            throw "Authentication information is missing from the environment. Please run `"connect-box`" to retrieve needed authentication info."
        }
        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 "Authentication information is missing from the environment. Please run `"connect-box`" to retrieve needed authentication info."
        }
        $act = New-Object Box.V2.Models.BoxActionableByRequest
        $act.Login = $exuseremail
        $req = New-Object Box.V2.Models.BoxRequestEntity
        $req.Id = (Get-ItemProperty "HKCU:\Software\boxmodule").enterpriseid
        $inviteob = New-Object Box.V2.Models.BoxUserInviteRequest
        $inviteob.ActionableBy = $act
        $inviteob.Enterprise = $req
        If ($PSBoundParameters.Keys -notcontains "fields")
        {
            $invreq = $adminclient.UsersManager.InviteUserToEnterpriseAsync($inviteob)
        }
        Else
        {
            $invreq = $adminclient.UsersManager.InviteUserToEnterpriseAsync($inviteob, $fields)
        }
        $invreq.Wait()
        If ($invreq.IsFaulted -eq $true)
        {
            Write-error $invreq.Exception.InnerException
            return
        }
        Else
        {
            return $invreq.Result
        }
        
    }
    
    function Get-boxuserinvite
    {
        param (
            [Parameter(Mandatory = $true)]
            [String]$inviteid,
            [System.String[]]$fields
        )
        If ($PSBoundParameters.Keys -notcontains "fields")
        {
            $invitereq = $adminclient.UsersManager.GetUserInviteAsync($inviteid)
        }
        Else
        {
            $invitereq = $adminclient.UsersManager.GetUserInviteAsync($inviteid, $fields)
        }
        $invitereq.Wait()
        If ($invitereq.IsFaulted -eq $true)
        {
            Write-error $invitereq.Exception.InnerException
            return
        }
        Else
        {
            return $invitereq.Result
        }
        
    }
    function set-boxuser
    {
        param (
            [Parameter(Mandatory = $true)]
            [hashtable]$updateinfo,
            [System.String[]]$fields
        )
        $boxuserreq = New-Object Box.V2.Models.BoxUserRequest -Property $updateinfo
        If ($PSBoundParameters.Keys -notcontains "fields")
        {
            $userupdate = $adminclient.UsersManager.UpdateUserInformationAsync($boxuserreq)
        }
        Else
        {
            $userupdate = $adminclient.UsersManager.UpdateUserInformationAsync($boxuserreq, $fields)
        }
        $userupdate.Wait()
        If ($userupdate.IsFaulted -eq $true)
        {
            Write-error $userupdate.Exception.InnerException
            return
        }
        Else
        {
            return $userupdate.Result
        }
    }
    
    function new-boxfolder
    {
        param (
            [Parameter(Mandatory = $true)]
            [String]$foldername,
            [Parameter(Mandatory = $true)]
            [String]$parentid,
            [string[]]$fields
        )
        #$folderreq = New-Object Box.V2.Models.BoxFolderRequest -Property $foldreq
        $folderreq = @{
            name = "$foldername"
            parent = @{
                id = "$parentid"
            }
        }
        If ($PSBoundParameters.Keys -notcontains "fields")
        {
            $boxfolreq = $adminclient.FoldersManager.CreateAsync($folderreq)
        }
        Else
        {
            $boxfolreq = $adminclient.FoldersManager.CreateAsync($folderreq, $fields)
        }
        $boxfolreq.Wait()
        If ($boxfolreq.IsFaulted -eq $true)
        {
            Write-error $boxfolreq.Exception.InnerException
            return
        }
        Else
        {
            return $yu.Result
        }
        
    }
    function get-boxfolderitems
    {
        [CmdletBinding(DefaultParameterSetName = "auto")]
        param (
            [string]$folderid = "0",
            [Parameter(ParameterSetName = 'noauto')]
            [int]$offset = 0,
            [Parameter(ParameterSetName = 'noauto')]
            [int]$limit = 1000,
            [Parameter(ParameterSetName = 'auto')]
            [Switch]$autopage,
            [System.String[]]$fields
        )
        If ($PSCmdlet.ParameterSetName -like "auto")
        {
            $foldlist = $adminclient.FoldersManager.GetFolderItemsAsync($folderid, 1000, 0, $PSBoundParameters.fields, $true)
        }
        Else
        {
            $foldlist = $adminclient.FoldersManager.GetFolderItemsAsync($folderid, $limit, $offset, $PSBoundParameters.fields, $false)
        }
        $foldlist.Wait()
        If ($foldlist.IsFaulted -eq $true)
        {
            Write-error $foldlist.Exception.InnerException
            return
        }
        Else
        {
            return $foldlist.Result
        }
    }
    function new-boxfoldersharelink
    {
        param (
            [Parameter(Mandatory = $true)]
            [String]$foldid,
            [ValidateSet("open", "collaborators", "company")]
            [System.String]$accesstype = "open",
            [String]$password,
            [System.String[]]$fields
        )
        $sharelink = [Box.V2.Models.BoxSharedLinkRequest]::new()
        $sharelink.Access = $accesstype
        If ($PSBoundParameters.Keys -contains "password")
        {
            $sharelink.Password = $password
        }
        If ($PSBoundParameters.Keys -notcontains "fields")
        {
            $sharelinkreq = $adminclient.FoldersManager.CreateSharedLinkAsync($foldid, $sharelink)
        }
        Else
        {
            $sharelinkreq = $adminclient.FoldersManager.CreateSharedLinkAsync($foldid, $sharelink, $fields)
        }
        $sharelinkreq.Wait()
        If ($sharelinkreq.IsFaulted -eq $true)
        {
            Write-error $sharelinkreq.Exception.InnerException
            return
        }
        Else
        {
            return $sharelinkreq.Result
        }
    }
    function copy-boxfolder
    {
        param (
            [Parameter(Mandatory = $true)]
            [String]$folderid,
            [Parameter(Mandatory = $true)]
            [String]$parentfolderid,
            [System.String[]]$fields
        )
        $folreq = @{
            id = "$folderid"
            parent = @{
                id = "$parentfolderid"
            }
        }
        If ($PSBoundParameters.Keys -notcontains "fields")
        {
            $proc = $adminclient.FoldersManager.CopyAsync($folreq)
        }
        Else
        {
            $proc = $adminclient.FoldersManager.CopyAsync($folreq, $fields)
        }
        $proc.Wait()
        If ($proc.IsFaulted -eq $true)
        {
            Write-error $proc.Exception.InnerException
            return
        }
        Else
        {
            return $proc.Result
        }
    }
    
    function remove-boxfolder
    {
        param (
            [Parameter(Mandatory = $true)]
            [String]$folderid,
            [Switch]$recurse
        )
        If ($recurse)
        {
            $folddelete = $adminclient.FoldersManager.DeleteAsync($folderid, $true)
        }
        Else
        {
            $folddelete = $adminclient.FoldersManager.DeleteAsync($folderid)
        }
        $folddelete.Wait()
        If ($folddelete.IsFaulted -eq $true)
        {
            Write-error $folddelete.Exception.InnerException
            return
        }
        Else
        {
            return $folddelete.Status
        }
    }
    function copy-boxfile
    {
        param (
            [Parameter(Mandatory = $true)]
            [System.String]$fileid,
            [Parameter(Mandatory = $true)]
            [String]$parentfolderid,
            [System.String[]]$fields
        )
        $copyreq = @{
            id = "$fileid"
            parent = @{
                id = "$parentfolderid"
            }
        }
        If ($PSBoundParameters.Keys -notcontains "fields")
        {
            $copyfile = $adminclient.FilesManager.CopyAsync($copyreq)
        }
        Else
        {
            $copyfile = $adminclient.FilesManager.CopyAsync($copyreq, $fields)
        }
        $copyfile.Wait()
        If ($copyfile.IsFaulted -eq $true)
        {
            Write-error $copyfile.Exception.InnerException
            return
        }
        Else
        {
            return $copyfile.Result
        }
    }
    function remove-boxfoldersharedlink
    {
        param (
            [Parameter(Mandatory = $true)]
            [System.String]$folderid
        )
        $delshare = $adminclient.FoldersManager.DeleteSharedLinkAsync($sharedlinkid)
        $delshare.Wait()
        If ($delshare.IsFaulted -eq $true)
        {
            Write-error $delshare.Exception.InnerException
            return
        }
        Else
        {
            return $delshare.result
        }
    }
    function get-boxfolderinfo
    {
        param (
            [Parameter(Mandatory = $true)]
            [String]$folderid,
            [System.String[]]$fields
        )
        If ($PSBoundParameters.Keys -notcontains "fields")
        {
            $folinfo = $adminclient.FoldersManager.GetInformationAsync($folderid)
        }
        Else
        {
            $folinfo = $adminclient.FoldersManager.GetInformationAsync($folderid, $fields)
        }
        $folinfo.Wait()
        If ($folinfo.IsFaulted -eq $true)
        {
            Write-error $folinfo.Exception.InnerException
            return
        }
        Else
        {
            return $folinfo.Result
        }
        
    }
    function get-boxfoldercollaborations
    {
        param (
            [Parameter(Mandatory = $true)]
            [ValidateNotNullOrEmpty][String]$foldid,
            [System.String[]]$fields
        )
        If ($PSBoundParameters.Keys -notcontains "fields")
        {
            $foldcollab = $adminclient.FoldersManager.GetCollaborationsAsync($foldid)
        }
        Else
        {
            $foldcollab = $adminclient.FoldersManager.GetCollaborationsAsync($foldid, $fields)
        }
        $foldcollab.Wait()
        If ($foldcollab.IsFaulted -eq $true)
        {
            Write-error $foldcollab.Exception.InnerException
            return
        }
        Else
        {
            return $foldcollab.Result
        }
    }
    function Get-boxtrashedfolderitems
    {
        [CmdletBinding(DefaultParameterSetName = "autopage")]
        param (
            [Parameter(ParameterSetName = 'autopage')]
            [switch]$autopage,
            [Parameter(ParameterSetName = "noautopage", Mandatory = $true)]
            [int]$limit,
            [Parameter(ParameterSetName = 'noautopage', Mandatory = $true)]
            [int]$offset,
            [System.String[]]$fields
        )
        If ($PSCmdlet.ParameterSetName -like "autopage")
        {
            $trasheditems = $adminclient.FoldersManager.GetTrashItemsAsync(1000, 0, $PSBoundParameters.fields, $true)
        }
        Else
        {
            $trasheditems = $adminclient.FoldersManager.GetTrashItemsAsync($PSBoundParameters.limit, $PSBoundParameters.offset, $PSBoundParameters.fields, $false)
        }
        $trasheditems.Wait()
        If ($trasheditems.IsFaulted -eq $true)
        {
            Write-error $trasheditems.Exception.InnerException
            return
        }
        Else
        {
            return $trasheditems.Result
        }
    }
    function get-trashedboxfolder
    {
        param (
            [Parameter(Mandatory = $true)]
            [String]$foldid,
            [System.String[]]$fields
        )
        If ($PSBoundParameters.Keys -notcontains "fields")
        {
            $trashfold = $adminclient.FoldersManager.GetTrashedFolderAsync($foldid)
        }
        Else
        {
            $trashfold = $adminclient.FoldersManager.GetTrashedFolderAsync($foldid, $fields)
        }
        $trashfold.Wait()
        If ($trashfold.IsFaulted -eq $true)
        {
            Write-error $trashfold.Exception.InnerException
            return
        }
        Else
        {
            return $trashfold.Result
        }
    }
    function remove-boxtrashfolder
    {
        param (
            [Parameter(Mandatory = $true)]
            [String]$foldid
        )
        $purgefold = $adminclient.FoldersManager.PurgeTrashedFolderAsync($foldid)
        $purgefold.Wait()
        If ($purgefold.IsFaulted -eq $true)
        {
            Write-error $purgefold.Exception.InnerException
            return
        }
        Else
        {
            return $purgefold.Status
        }
    }
    function restore-trashedboxfolder
    {
        param (
            [Parameter(Mandatory = $true)]
            [String]$trashedfolderid,
            [String]$restoretofolderid,
            [System.String[]]$fields
        )
        If ($PSBoundParameters.Keys -contains "restoretofolderid")
        {
            $foldreq = @{
                id = "$trashedfolderid"
                parent = @{
                    id = "$restoretofolderid"
                }
            }
        }
        Else
        {
            $foldreq = @{
                id = "$trashedfolderid"
            }
        }
        
        If ($PSBoundParameters.Keys -notcontains "fields")
        {
            $restorefold = $adminclient.FoldersManager.RestoreTrashedFolderAsync($foldreq)
        }
        Else
        {
            $restorefold = $adminclient.FoldersManager.RestoreTrashedFolderAsync($foldreq, $fields)
        }
        $restorefold.Wait()
        If ($restorefold.IsFaulted -eq $true)
        {
            Write-error $restorefold.Exception.InnerException
            return
        }
        Else
        {
            return $restorefold.Result
        }
    }
    
    function set-boxfolderinfo
    {
        param (
            [Parameter(Mandatory = $true)]
            [String]$foldid,
            [String]$parentfolderid,
            [String]$description,
            [Switch]$folderuploademail,
            [String]$name,
            [System.String[]]$fields
        )
        $foldreq = New-Object Box.V2.Models.BoxFolderRequest
        $foldreq.Id = $foldid
        If ($PSBoundParameters.Keys -contains "parentfolderid")
        {
            $entity = New-Object Box.V2.Models.BoxRequestEntity
            $entity.Id = $parentfolderid
            $entity.Type = "folder"
            $foldreq.Parent = $entity
        }
        If ($PSBoundParameters.Keys -contains "description")
        {
            $foldreq.Description = $description
        }
        If ($folderuploademail)
        {
            $email = New-Object Box.V2.Models.BoxEmailRequest
            $foldreq.FolderUploadEmail = $email
        }
        If ($PSBoundParameters.Keys -contains "name")
        {
            $foldreq.Name = $name
        }
        If ($PSBoundParameters.Keys -notcontains "fields")
        {
            $folderupdate = $adminclient.FoldersManager.UpdateInformationAsync($foldreq)
        }
        Else
        {
            $folderupdate = $adminclient.FoldersManager.UpdateInformationAsync($foldreq, $fields)
        }
        $folderupdate.Wait()
        If ($folderupdate.IsFaulted -eq $true)
        {
            Write-error $folderupdate.Exception.InnerException
            return
        }
        Else
        {
            return $folderupdate.Result
        }
    }
    function get-boxcollections
    {
        $getcoll = $adminclient.CollectionsManager.GetCollectionsAsync()
        $getcoll.Wait()
        If ($getcoll.IsFaulted -eq $true)
        {
            Write-error $getcoll.Exception.InnerException
            return
        }
        Else
        {
            return $getcoll.Result
        }
    }
    function Get-boxcollectionitems
    {
        [CmdletBinding(DefaultParameterSetName = "autopage")]
        param (
            [Parameter(Mandatory = $true)]
            [String]$collectionid,
            [Parameter(ParameterSetName = "noauto")]
            [ValidateNotNull][int]$offset = 0,
            [Parameter(ParameterSetName = 'noauto')]
            [ValidateNotNull][int]$limit = 1000,
            [Parameter(ParameterSetName = 'autopage')]
            [switch]$autopaginate,
            [System.String[]]$fields
        )
        If ($PSCmdlet.ParameterSetName -like "noauto")
        {
            $boxcollections = $adminclient.CollectionsManager.GetCollectionItemsAsync($collectionid, $limit, $offset, $PSBoundParameters.fields, $false)
        }
        Else
        {
            $boxcollections = $adminclient.CollectionsManager.GetCollectionItemsAsync($collectionid, 1000, 0, $PSBoundParameters.fields, $true)
        }
        $boxcollections.Wait()
        If ($boxcollections.IsFaulted -eq $true)
        {
            Write-error $boxcollections.Exception.InnerException
            return
        }
        Else
        {
            return $boxcollections.Result
        }
    }
    function addremove-boxcollectionsforfolder
    {
        param (
            [Parameter(Mandatory = $true)]
            [String]$folderid,
            [Parameter(Mandatory = $true)]
            [String]$collectionid
        )
        $boxent = New-object Box.V2.Models.BoxRequestEntity
        $boxent.Id = $collectionid
        $boxent.Type = "folder"
        $boxcollreq = New-Object Box.V2.Models.BoxCollectionsRequest
        $boxcollreq.Collections = $boxent
        $boxcollectionreq = $adminclient.CollectionsManager.CreateOrDeleteCollectionsForFolderAsync($folderid, $boxcollreq)
        $boxcollectionreq.Wait()
        If ($boxcollectionreq.IsFaulted -eq $true)
        {
            Write-error $boxcollectionreq.Exception.InnerException
            return
        }
        Else
        {
            return $boxcollectionreq.Result
        }
    }
    function addremove-boxcollectionsforfile
    {
        param (
            [Parameter(Mandatory = $true)]
            [String]$fileid,
            [Parameter(Mandatory = $true)]
            [String]$collectionid
        )
        $boxentr = New-object Box.V2.Models.BoxRequestEntity
        $boxentr.Id = $collectionid
        $boxentr.Type = "file"
        $boxcollreqr = New-Object Box.V2.Models.BoxCollectionsRequest
        $boxcollreqr.Collections = $boxentr
        $boxcollectionreqr = $adminclient.CollectionsManager.CreateOrDeleteCollectionsForFolderAsync($fileid, $boxcollreqr)
        $boxcollectionreqr.Wait()
        If ($boxcollectionreqr.IsFaulted -eq $true)
        {
            Write-error $boxcollectionreqr.Exception.InnerException
            return
        }
        Else
        {
            return $boxcollectionreqr.Result
        }
    }
    function get-boxfileinfo
    {
        param (
            [Parameter(Mandatory = $true)]
            [String]$fileid,
            [System.String[]]$fields
        )
        If ($PSBoundParameters.Keys -notcontains "fields")
        {
            $fileinfo = $adminclient.FilesManager.GetInformationAsync($fileid)
        }
        Else
        {
            $fileinfo = $adminclient.FilesManager.GetInformationAsync($fileid, $fields)
        }
        $fileinfo.Wait()
        If ($fileinfo.IsFaulted -eq $true)
        {
            Write-error $fileinfo.Exception.InnerException
            return
        }
        Else
        {
            return $fileinfo.Result
        }
    }
    function new-boxcomment
    {
        param (
            [Parameter(Mandatory = $true)]
            [ValidateSet("file", "discussion", "comment")]
            [String]$boxtype,
            [Parameter(Mandatory = $true)]
            [String]$entityid,
            [Parameter(Mandatory = $true)]
            [String]$comment,
            [System.String[]]$fields
        )
        $entreq = New-Object Box.V2.Models.BoxRequestEntity
        $entreq.Id = $entityid
        $entreq.Type = $boxtype
        $commreq = New-Object Box.V2.Models.BoxCommentRequest
        $commreq.Item = $entreq
        $commreq.Message = $comment
        If ($PSBoundParameters.Keys -notcontains "fields")
        {
            $createcomment = $adminclient.CommentsManager.AddCommentAsync($commreq)
        }
        else
        {
            $createcomment = $adminclient.CommentsManager.AddCommentAsync($commreq, $fields)
        }
        $createcomment.Wait()
        If ($createcomment.IsFaulted -eq $true)
        {
            Write-error $createcomment.Exception.InnerException
            return
        }
        Else
        {
            return $createcomment.Result
        }
    }
    function remove-boxcomment
    {
        param (
            [Parameter(Mandatory = $true)]
            [String]$commentid
        )
        $deletecomment = $adminclient.CommentsManager.DeleteAsync($commentid)
        $deletecomment.Wait()
        If ($deletecomment.IsFaulted -eq $true)
        {
            Write-error $deletecomment.Exception.InnerException
            return
        }
        Else
        {
            return $deletecomment.Status
        }
    }
    function get-boxfilecomment
    {
        param (
            [Parameter(Mandatory = $true)]
            [String]$fileid,
            [System.String[]]$fields
        )
        If ($PSBoundParameters.Keys -notcontains "fields")
        {
            $filecomments = $adminclient.FilesManager.GetCommentsAsync($fileid)
        }
        Else
        {
            $filecomments = $adminclient.FilesManager.GetCommentsAsync($fileid, $fields)
        }
        $filecomments.Wait()
        If ($filecomments.IsFaulted -eq $true)
        {
            Write-error $filecomments.Exception.InnerException
            return
        }
        Else
        {
            return $filecomments.Result
        }
    }
    function Get-boxcommentinfo
    {
        param (
            [Parameter(Mandatory = $true)]
            [System.String]$commentid,
            [System.String[]]$fields
        )
        If ($PSBoundParameters.Keys -notcontains "fields")
        {
            $commentinfo = $adminclient.CommentsManager.GetInformationAsync($commentid)
        }
        Else
        {
            $commentinfo = $adminclient.CommentsManager.GetInformationAsync($commentid, $fields)
        }
        $commentinfo.Wait()
        If ($commentinfo.IsFaulted -eq $true)
        {
            Write-error $commentinfo.Exception.InnerException
            return
        }
        Else
        {
            return $commentinfo.Result
        }
        
    }
    function set-boxcomment
    {
        param (
            [Parameter(Mandatory = $true)]
            [String]$commentid,
            [Parameter(Mandatory = $true)]
            [String]$comment,
            [System.String[]]$fields
        )
        Try
        {
            $commenttest = Get-boxcommentinfo -commentid $commentid -ErrorAction Stop
        }
        Catch
        {
            Write-Error "The id is not associated with a valid comment."
            return
        }
        $commreq = New-Object Box.V2.Models.BoxCommentRequest
        $commreq.Message = $comment
        If ($PSBoundParameters.Keys -notcontains "fields")
        {
            $updatecomment = $adminclient.CommentsManager.AddCommentAsync($commentid, $commreq)
        }
        else
        {
            $updatecomment = $adminclient.CommentsManager.AddCommentAsync($commentid, $commreq, $fields)
        }
        $updatecomment.Wait()
        If ($updatecomment.IsFaulted -eq $true)
        {
            Write-Error $updatecomment.Exception.InnerException
            return
        }
        Else
        {
            return $updatecomment.Result
        }
        
    }
    function get-boxenterpriseevents
    {
        [CmdletBinding(DefaultParameterSetName = "default")]
        param (
            [int]$limit = 500,
            [string]$offset = $null,
            [System.String[]]$eventtypes = $null,
            [datetime]$createdbefore,
            [datetime]$createdafter
        )
        If ($PSBoundParameters -contains "createdbefore")
        {
            If ($PSBoundParameters -contains "createdafter")
            {
                $getboxevents = $adminclient.EventsManager.EnterpriseEventsAsync($limit, $offset, $eventtypes, $createdafter, $createdbefore)
            }
            Else
            {
                $getboxevents = $adminclient.EventsManager.EnterpriseEventsAsync($limit, $offset, $eventtypes, $null, $createdbefore)
            }
        }
        Else
        {
            If ($PSBoundParameters -contains "createdafter")
            {
                $getboxevents = $adminclient.EventsManager.EnterpriseEventsAsync($limit, $offset, $eventtypes, $createdafter)
            }
            Else
            {
                $getboxevents = $adminclient.EventsManager.EnterpriseEventsAsync($limit, $offset, $eventtypes)
            }
        }
        
        $getboxevents.Wait()
        If ($getboxevents.IsFaulted -eq $true)
        {
            Write-Error $getboxevents.Exception.InnerException
            return
        }
        Else
        {
            return $getboxevents.result
        }
    }
    function get-boxuserevents
    {
        param (
            [int]$limit = 500,
            [ValidateSet("all", "changes", "sync")]
            [string]$usereventtype = "all",
            [string]$streamposition = "now",
            [switch]$dedupeEvents
        )
        If ($dedupeEvents -eq $true)
        {
            $userevents = $adminclient.EventsManager.UserEventsAsync($limit, $usereventtype, $streamposition, $true)
        }
        Else
        {
            $userevents = $adminclient.EventsManager.UserEventsAsync($limit, $usereventtype, $streamposition, $false)
        }
        $userevents.Wait()
        If ($userevents.IsFaulted -eq $true)
        {
            Write-Error $userevents.Exception.InnerException
            return
        }
        Else
        {
            return $userevents.Result
        }
        
    }
    
    function upload-boxfilenewversion
    {
        [CmdletBinding()]
        param (
            [Parameter(Mandatory = $true)]
            [String]$filename,
            [Parameter(Mandatory = $true)]
            [String]$fileid,
            [Parameter(Mandatory = $true)]
            [String]$path,
            [String[]]$fields,
            [Parameter(Mandatory = $true)]
            [system.TimeSpan]$timespan
        )
        If (!(Test-Path $path))
        {
            Write-Error "The file $path does not exist. Try again."
            return
        }
        $filestream = [System.IO.File]::OpenRead($path)
        $fileversionup = $adminclient.FilesManager.UploadNewVersionAsync($filename, $fileid, $filestream, $null, $PSBoundParameters.fields, $timespan, $null, $true, $null)
        $fileversionup.Wait()
        If ($fileversionup.IsFaulted -eq $true)
        {
            Write-error $fileversionup.Exception.InnerException
            return
        }
        Else
        {
            return $fileversionup.Result
        }
        
    }
    function new-boxfilesharelink
    {
        param (
            [Parameter(Mandatory = $true)]
            [String]$fileid,
            [ValidateSet("open", "collaborators", "company")]
            [String]$accesstype = "open",
            [String]$password,
            [System.String[]]$fields
        )
        $sharelink = [Box.V2.Models.BoxSharedLinkRequest]::new()
        $sharelink.Access = $accesstype
        If ($PSBoundParameters.Keys -contains "password")
        {
            $sharelink.Password = $password
        }
        If ($PSBoundParameters.Keys -notcontains "fields")
        {
            $sharelinkreq = $adminclient.FilesManager.CreateSharedLinkAsync($fileid, $sharelink)
        }
        Else
        {
            $sharelinkreq = $adminclient.FilesManager.CreateSharedLinkAsync($fileid, $sharelink, $fields)
        }
        $sharelinkreq.Wait()
        If ($sharelinkreq.IsFaulted -eq $true)
        {
            Write-error $sharelinkreq.Exception.InnerException
            return
        }
        Else
        {
            return $sharelinkreq.Result
        }
    }
    function remove-boxfile
    {
        param (
            [Parameter(Mandatory = $true)]
            [String]$fileid
        )
        $deletefile = $adminclient.FilesManager.DeleteAsync($fileid)
        $deletefile.Wait()
        If ($deletefile.IsFaulted -eq $true)
        {
            Write-error $deletefile.Exception.InnerException
            return
        }
        Else
        {
            return $deletefile.Status
        }
    }
    function get-boxfileversions
    {
        param (
            [Parameter(Mandatory = $true)]
            [System.String]$fileid,
            [System.String[]]$fields
        )
        If ($PSBoundParameters.Keys -notcontains "fields")
        {
            $fileversions = $adminclient.FilesManager.ViewVersionsAsync($fileid)
        }
        Else
        {
            $fileversions = $adminclient.FilesManager.ViewVersionsAsync($fileid, $fields)
        }
        $fileversions.Wait()
        If ($fileversions.IsFaulted -eq $true)
        {
            Write-error $fileversions.Exception.InnerException
            return
        }
        Else
        {
            return $fileversions.Result
        }
        
    }
    function remove-boxoldfileversion
    {
        param (
            [Parameter(Mandatory = $true)]
            [String]$fileid,
            [Parameter(Mandatory = $true)]
            [String]$versionid
        )
        $removeversion = $adminclient.FilesManager.DeleteOldVersionAsync($fileid, $versionid)
        $removeversion.Wait()
        If ($removeversion.IsFaulted -eq $true)
        {
            Write-error $removeversion.Exception.InnerException
            return
        }
        Else
        {
            return $removeversion.Result
        }
        
    }
    function remove-boxfilesharelink
    {
        param (
            [Parameter(Mandatory = $true)]
            [String]$fileid
        )
        $deletesharelink = $adminclient.FilesManager.DeleteSharedLinkAsync($fileid)
        $deletesharelink.Wait()
        If ($deletesharelink.IsFaulted -eq $true)
        {
            Write-error $deletesharelink.Exception.InnerException
            return
        }
        Else
        {
            return $deletesharelink.Result
        }
        
    }
    function new-boxcollaborator
    {
        param (
            [Parameter(Mandatory = $true)]
            [ValidateSet("file", "discussion", "comment", "folder", "retention_policy", "enterprise", "user", "group", "web_link", "file_version", "metadata_template")]
            [String]$boxtype,
            [Parameter(Mandatory = $true)]
            $itemid,
            [Parameter(Mandatory = $true)]
            [String]$inviteduserid,
            [Parameter(Mandatory = $true)]
            [ValidateSet("Editor", "Viewer", "previewer", "Uploader", "PreviewerUploader", "ViewerUploader", "CoOwner")]
            [String]$role,
            [ValidateSet("accepted", "rejected")]
            [String]$status,
            [datetime]$expires,
            [System.String[]]$fields,
            [Switch]$notify
        )
        $boxent = New-object Box.V2.Models.BoxRequestEntity
        $boxent.Type = $boxtype
        $boxent.Id = $itemid
        $colluser = New-object Box.V2.Models.BoxCollaborationUserRequest
        $colluser.Id = $inviteduserid
        $collabreq = New-object Box.V2.Models.BoxCollaborationRequest
        $collabreq.Item = $boxent
        $collabreq.AccessibleBy = $colluser
        $collabreq.Role = $role
        If ($PSBoundParameters.Keys -contains "status")
        {
            $collabreq.Status = $status
        }
        If ($PSBoundParameters.Keys -contains "expires")
        {
            $collabreq.ExpiresAt = $expires
        }
        If ($PSBoundParameters.Keys -contains "notify")
        {
            $addcolla = $adminclient.CollaborationsManager.AddCollaborationAsync($collabreq, $PSBoundParameters.fields, $true)
        }
        Else
        {
            $addcolla = $adminclient.CollaborationsManager.AddCollaborationAsync($collabreq, $PSBoundParameters.fields, $false)
        }
        $addcolla.Wait()
        If ($addcolla.IsFaulted -eq $true)
        {
            Write-error $addcolla.Exception.InnerException
            return
        }
        Else
        {
            return $addcolla.Result
        }
        
    }
    function Get-boxcollaborationinfo
    {
        param (
            [Parameter(Mandatory = $true)]
            [String]$collaborationid,
            [System.String[]]$fields
        )
        If ($PSBoundParameters.Keys -notcontains "fields")
        {
            $collabinfo = $adminclient.CollaborationsManager.GetCollaborationAsync($collaborationid)
        }
        else
        {
            $collabinfo = $adminclient.CollaborationsManager.GetCollaborationAsync($collaborationid, $fields)
        }
        $collabinfo.Wait()
        If ($collabinfo.IsFaulted -eq $true)
        {
            Write-error $collabinfo.Exception.InnerException
            return
        }
        Else
        {
            return $collabinfo.Result
        }
        
    }
    function get-pendingboxcollaboration
    {
        param (
            [System.String[]]$fields
        )
        If ($PSBoundParameters.Keys -notcontains "fields")
        {
            $pendingcoll = $adminclient.CollaborationsManager.GetPendingCollaborationAsync()
        }
        Else
        {
            $pendingcoll = $adminclient.CollaborationsManager.GetPendingCollaborationAsync($fields)
        }
        $pendingcoll.Wait()
        If ($pendingcoll.IsFaulted -eq $true)
        {
            Write-error $pendingcoll.Exception.InnerException
            return
        }
        Else
        {
            return $pendingcoll.Result
        }
        
    }
    function remove-boxcollaboration
    {
        param (
            [Parameter(Mandatory = $true)]
            [String]$collaborationid
        )
        $removecollab = $adminclient.CollaborationsManager.RemoveCollaborationAsync($collaborationid)
        $removecollab.Wait()
        If ($removecollab.IsFaulted -eq $true)
        {
            Write-error $removecollab.Exception.InnerException
            return
        }
        Else
        {
            return $removecollab.Status
        }
        
    }
    function get-boxfilecollaborations
    {
        param (
            [Parameter(Mandatory = $true)]
            [String]$fileid,
            [System.String[]]$fields
        )
        If ($PSBoundParameters.Keys -notcontains "fields")
        {
            $filecollab = $adminclient.FilesManager.GetCollaborationsAsync($fileid)
        }
        Else
        {
            $filecollab = $adminclient.FilesManager.GetCollaborationsAsync($fileid, $fields)
        }
        $filecollab.Wait()
        If ($filecollab.IsFaulted -eq $true)
        {
            Write-error $filecollab.Exception.InnerException
            return
        }
        Else
        {
            return $filecollab.Result
        }
        
    }
    function get-boxfiledownloaduri
    {
        param (
            [Parameter(Mandatory = $true)]
            [String]$fileid,
            [String]$versionid
        )
        If ($PSBoundParameters.Keys -notcontains "versionid")
        {
            $filedown = $adminclient.FilesManager.GetDownloadUriAsync($fileid)
        }
        Else
        {
            $filedown = $adminclient.FilesManager.GetDownloadUriAsync($fileid, $versionid)
        }
        $filedown.Wait()
        If ($filedown.IsFaulted -eq $true)
        {
            Write-error $filedown.Exception.InnerException
            return
        }
        Else
        {
            return $filedown.Result
        }
        
    }
    function get-boxfiletasks
    {
        param (
            [Parameter(Mandatory = $true)]
            [String]$fileid,
            [System.String[]]$fields
        )
        If ($PSBoundParameters.Keys -notcontains "fields")
        {
            $filetasks = $adminclient.FilesManager.GetFileTasks($fileid)
        }
        Else
        {
            $filetasks = $adminclient.FilesManager.GetFileTasks($fileid, $fields)
        }
        $filetasks.Wait()
        If ($filetasks.IsFaulted -eq $true)
        {
            Write-error $filetasks.Exception.InnerException
            return
        }
        Else
        {
            return $filetasks.Result
        }
        
    }
    function lock-boxfile
    {
        param (
            [Parameter(Mandatory = $true)]
            [String]$fileid,
            [DateTime]$expiresat,
            [Switch]$preventdownload
        )
        $lock = New-Object Box.V2.Models.BoxFileLock
        If ($PSBoundParameters.Keys -contains "expiresat")
        {
            $lock.ExpiresAt = $expiresat
        }
        If ($preventdownload)
        {
            $lock.IsDownloadPrevented = $true
        }
        Else
        {
            $lock.IsDownloadPrevented = $false
        }
        $lockreq = New-Object Box.V2.Models.BoxFileLockRequest
        $lockreq.Lock = $lock
        $lockfile = $adminclient.FilesManager.LockAsync($lockreq, $fileid)
        $lockfile.Wait()
        If ($lockfile.IsFaulted -eq $true)
        {
            Write-error $lockfile.Exception.InnerException
            return
        }
        Else
        {
            return $lockfile.Result
        }
        
    }
    Export-ModuleMember -Function connect-box, get-boxusers, set-boxuserlogin, new-boxuser, remove-boxuser, new-boxemailalias, transfer-boxusercontent, upload-boxfile, Get-boxemailalias, Get-boxuserinfo, Get-boxuserinvite, invite-usertobox, remove-boxemailalias, set-boxuser, new-boxfolder, get-boxfolderitems, new-boxfoldersharelink, copy-boxfolder, remove-boxfolder, remove-boxfoldersharedlink, get-boxfolderinfo, get-boxfoldercollaborations, Get-boxtrashedfolderitems, get-trashedboxfolder, remove-boxtrashfolder, restore-trashedboxfolder, set-boxfolderinfo, get-boxcollections, Get-boxcollectionitems, addremove-boxcollectionsforfolder, addremove-boxcollectionsforfile, get-boxfileinfo, new-boxcomment, remove-boxcomment, get-boxfilecomment, Get-boxcommentinfo, set-boxcomment, get-boxenterpriseevents, get-boxuserevents, copy-boxfile, upload-boxfilenewversion, remove-boxfile, get-boxfileversions, remove-boxoldfileversion, new-boxfilesharelink, remove-boxfilesharelink, new-boxcollaborator, Get-boxcollaborationinfo, get-pendingboxcollaboration, remove-boxcollaboration, get-boxfilecollaborations, get-boxfiledownloaduri, get-boxfiletasks, lock-boxfile