#PowerShell, #PowerWiseScripting, #ProjectWise, PWPS_DAB

HowTo: FUNCTION Remove-MyPWDocumentExplicitAccessControlByDocument

In this post, I’ll show a function I created for removing explicit document access control for ProjectWise document objects provided. This is different than the previous post for ALL explicit document access control. I realize there are rare occasions where explicit document access control may be required.

FUNCTION Definition

The following is the function wrapper which will contain the remainder of the code.

FUNCTION Remove-MyPWDocumentExplicitAccessControlByDocument {
    <#
    .Synopsis
    Removes explicit access control entries from document objects.
    .DESCRIPTION
    Used to remove explicit document access control from each document provided. 
    .EXAMPLE
    The following removes explicit document access control.
    $pwDocument = Show-PWDocumentSelectionDialog
    Remove-MyPWDocumentExplicitAccessControlByDocument -DocumentsToRemoveAccessControl $pwDocument -Verbose
    #>
    [CmdletBinding()]
    param (...) # end param...

    BEGIN {...} # end BEGIN...

    PROCESS {...} # end PROCESS...

    END{...} # end END...
} # end FUNCTION Remove-MyPWDocumentExplicitAccessControl...
Export-ModuleMember -Function Remove-MyPWDocumentExplicitAccessControlByDocument

Parameters

First thing we need to do is create our parameter and value pairs. The help messages will explain the purpose for each parameter.

param (
    # Document(s) to remove explicit access control from.
    [ValidateNotNullOrEmpty()]
    [Parameter(Mandatory=$true)]
    [PWPS_DAB.CommonTypes+ProjectWiseDocument[]] $DocumentsToRemoveAccessControl
) # end param...

BEGIN

I want to take a second and point out a couple of conventions that I use in my scripts. First, you may notice that I put a comment at the end of many of the blocks of code. Doing this makes it easier for me to determine where a block of code starts and ends. Comes in handy when troubleshooting a script.  I also add text ([BEGIN], [PROCESS], [END]) in the output messages (Write-Verbose, Write-Warning, Write-Error) corresponding to the section of the script I am in. Again, this makes it obvious where something occurs within the script.

BEGIN { 
 
    $CmdletName = $MyInvocation.MyCommand.Name
    $StartTime = Get-Date
    Write-Verbose -Message "[BEGIN] $StartTime - Entering '$CmdletName' Function..."

    #region SET DEFAULTS

    [int] $valueIgnoreParents = [PWWrapper+AccessControlSelectionFlags]::IgnoreParents
    [int] $valueIgnoreDefault = [PWWrapper+AccessControlSelectionFlags]::IgnoreDefault
    [int] $valueFlags = $valueIgnoreParents + $valueIgnoreDefault
    [int] $ObjType = [PWWrapper+AccessObjectProperty]::ObjType
    [int] $ObjID1 = [PWWrapper+AccessObjectProperty]::ObjID1
    [int] $ObjID2 = [PWWrapper+AccessObjectProperty]::ObjID2
    [int] $Workflow = [PWWrapper+AccessObjectProperty]::Workflow
    [int] $State = [PWWrapper+AccessObjectProperty]::State
    [int] $MemberType = [PWWrapper+AccessObjectProperty]::MemberType
    [int] $MemberID = [PWWrapper+AccessObjectProperty]::MemberId
    [int] $AccessMask = [PWWrapper+AccessObjectProperty]::AccessMask

    #endregion SET DEFAULTS
} # end BEGIN...

PROCESS

Now, we will proceed to the PROCESS code block.
Here we will look at how to get all of the documents with explicit access control applied. How to obtain the access control entries for each document and subsequently remove them.

PROCESS {
    Write-Verbose -Message "[PROCESS] There are $($DocumentsToRemoveAccessControl.Count) document(s) to remove explicit access control from."

    foreach($doc in $DocumentsToRemoveAccessControl){
        try {
            $projectID = $doc.ProjectID
            $documentID = $doc.DocumentID
            Write-Verbose -Message "[PROCESS] Processing '$($doc.Name)' document."
            # Get document access control.
            $iPtr = [pwwrapper]::aaApi_SelectAccessControlDataBuffer($valueFlags, 4, $projectID, $documentID, 0, 0, [pwwrapper+AccessMaskFlags]::None)
            if($iPtr -eq [Intptr]::Zero){
               throw "Failed to get document access control."
            }

            [int] $iPtrCount = [PWWrapper]::aaApi_DmsDataBufferGetCount($iPtr);

            for ([int] $iRow = 0; $iRow -lt $iPtrCount; $iRow++) { 
                [int] $iObjType = [PWWrapper]::aaApi_DmsDataBufferGetNumericProperty($iPtr, $ObjType, $iRow);
                if($iObjType -eq 0){
                    continue
                }
                [int] $iObjID1 = [PWWrapper]::aaApi_DmsDataBufferGetNumericProperty($iPtr, $ObjID1, $iRow);
                [int] $iObjID2 = [PWWrapper]::aaApi_DmsDataBufferGetNumericProperty($iPtr, $ObjID2, $iRow);
                [int] $iWorkFlow = [PWWrapper]::aaApi_DmsDataBufferGetNumericProperty($iPtr, $Workflow, $iRow);
                [int] $iState = [PWWrapper]::aaApi_DmsDataBufferGetNumericProperty($iPtr, $State, $iRow);
                [int] $iMemberType = [PWWrapper]::aaApi_DmsDataBufferGetNumericProperty($iPtr, $MemberType, $iRow);
                [int] $iMemberID = [PWWrapper]::aaApi_DmsDataBufferGetNumericProperty($iPtr, $MemberID, $iRow);
                [int] $iAccessMask = [PWWrapper]::aaApi_DmsDataBufferGetNumericProperty($iPtr, $AccessMask, $iRow);

                if([PWWrapper]::aaApi_RemoveAccessList(4, $projectID, $documentID, $iWorkFlow, $iState, $iMemberType, $iMemberID)){
                    Write-Verbose -Message "[PROCESS] Successfully removed access control."
                } else {
                    Write-Warning -Message "[PROCESS] Failed to remove access control."
                }
            }

            # Free databuffer.
            [PWWrapper]::aaApi_DmsDataBufferFree($iPtr)

        } catch {
            Write-Warning -Message "[PROCESS] $_"
        } # end try/catch...
    }

} # end PROCESS...

END

Lastly, we will proceed to the END block of code.

END {

    $EndTime = Get-Date
    Write-Verbose -Message "[END] It took $($EndTime - $StartTime) to complete the process."
    Write-Verbose -Message "[END] $EndTime - Exiting '$CmdletName' Function..."

} # end END...

EXAMPLE

In the following example I will show how to use the cmdlet.

# The following finds and removes all explicit document access control.
$pwDocuments = Show-PWDocumentSelectionDialog
Remove-MyPWDocumentExplicitAccessControlByDocument -DocumentsToRemoveAccessControl $pwDocuments -Verbose

Experiment with it and have fun. Hopefully, you find this useful. Please let me know if you have any questions or comments.  If you like this post, please click the Like button at the bottom of the page.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.