#PowerShell, #PowerWiseScripting, #ProjectWise, PWPS_DAB

HowTo: Get-PWOwner

In this post, we will be creating a PowerShell function which will return the owner(s) of a specified ProjectWise Group or UserList.

All of the ProjectWise related cmdlets are available using the PWPS_DAB module. At the time of this post, I am using version 24.01. Take a look at the help for each of the cmdlets to become familiar with their functionality, available parameters, etc.

  • Select-PWSQL
  • Get-PWUsersByMatch
  • Get-PWUserLists

FUNCTION Definition

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

FUNCTION Get-PWOwner {
    <#
    .Synopsis
    Returns the owner(s) for the specified group/userlist.
    .DESCRIPTION
    Returns the owner(s) for the specified group/userlist.
    .EXAMPLE
    Returns the owner(s) of the specified group.
    $owner = Get-PWOwner -Type Group -Name 'GroupName' -Verbose
    .EXAMPLE
    Returns the owner(s) of the specified userlist.
    $owner = Get-PWOwner -Type UserList -Name 'UserListName' -Verbose
    #>

    [CmdletBinding()]
    param (...) # end param...

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

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

    END{...} # end END...
} # end FUNCTION Get-PWOwner...
Export-ModuleMember -Function Get-PWOwner

Parameters

First thing we need to do is create our parameter and value pairs. The help messages will explain the purpose for each parameter. You will see that I have incorporated “ValidateSet” for the Type parameter.

[CmdletBinding()]
param ( 
    # Member type ot get owner(s). Group or UserList.
    [ValidateNotNullOrEmpty()] 
    [ValidateSet('Group', 'UserList')]
    [Parameter(Mandatory = $true)]
    [string] $Type,

    # Name of ProjectWise Group or UserList to get owner(s).
    [ValidateNotNullOrEmpty()]
    [Parameter(Mandatory = $true)]
    [string] $Name
) # 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
    $Splat_Log = @{
        Path = $LogFilePathName 
        Cmdlet = $CmdletName
        Verbose = $true
    }
    Write-PWPSLog @Splat_Log -Message "[BEGIN] $(Get-Date) - Entering '$CmdletName' Function..." -Level Info 

    # Set the variables based on the type. Specifies the sql table and columnname to query.
    $tableName = [string]::Empty
    $itemProperty = [string]::Empty
    if($Type -eq 'Group'){
        $tableName = 'dms_grp'
        $itemProperty = 'o_groupname'
    } elseif($Type -eq 'UserList'){
        $tableName = 'dms_ulst'
        $itemProperty = 'o_name'
    }
} # end BEGIN...

PROCESS

Now, we will proceed to the PROCESS code block. Here the function will run a query to determine if the provided Group or UserList has an owner(s). If it does, the type and name will be returned for each.

PROCESS {
    # Get project / folder to search for documents.
    try {
        $results = Select-PWSQL "SELECT * FROM dms_acce WHERE o_aclno IN (SELECT o_aclno FROM $tableName WHERE $itemProperty LIKE '$Name')"
        if([string]::IsNullOrEmpty($results)){
            Write-Warning -Message "[PROCESS] '$Name' does not have any owners."
        } else {
            Write-Verbose -Message "[PROCESS] '$Name' has $($results.Rows.Count) owner(s)." -Verbose

        foreach($row in $results.Rows){
            $memberType = [string]::Empty
            switch ($row.o_memtype) {
                 1 {
                     $memberType = 'User'
                     $memberName = Get-PWUsersByMatch -UserId $row.o_memberno | Select-Object -ExpandProperty UserName
                     break
                 }
                 3 {
                    $memberType = 'UserList'
                    $memberName = Get-PWUserLists -UserListID $row.o_memberno | Select-Object -ExpandProperty Name
                    break
                }
                Default {}
            }

            [PSCustomObject]@{
                MemberType = $MemberType
                MemberName = $MemberName
            }
        } # end foreach($row in $results.Rows...
    } catch {
        Write-Warning -Message $_
    } # 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...

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.