#ProjectWise, #PowerShell, #PowerWiseScripting, PWPS_DAB

HowTo: FUNCTION To Retrieve Adminstrators in Current Datasource

In this post, we will show a simple function for retrieving all members of the primary ProjectWise Administrators group.

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

  • Select-PWSQL
  • Get-PWUsersInGroup

FUNCTION Definition

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

FUNCTION Get-PWAdministrators {
    <#
        .SYNOPSIS
Used to get all members of the primary Administrator group within the current datasource.

.DESCRIPTION
Returns an array of user objects for all Administrator accounts for the current datasource.

.EXAMPLE
# Returns all administrator accounts for the current datasource.
$Admins = Get-PWAdministrators -Verbose

.OUTPUTS Array of ProjectWise User Objects #>  

  [CmdletBinding()] param (...) # end param... BEGIN {...} # end BEGIN... PROCESS {...} # end PROCESS... END{...} # end END... } # end FUNCTION Get-PWAdministrators... Export-ModuleMember -Function Get-PWAdministrators

Parameters

There aren’t any parameters available for this function.

[CmdletBinding()]
param (
) # 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, Write-Host) 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-Host "[BEGIN] $StartTime - Entering '$CmdletName' Function..." -ForegroundColor Cyan  
} # end BEGIN...

PROCESS

Now, we will proceed to the PROCESS code block.

In the PROCESS section we will query the dms_grp table to get the primary administrator group which should be identified by the o_groupno  = 1. Once we have the name of the primary administrator group, we use it to get the members of said group. We then the user objects. 

PROCESS {
    # Get project / folder to search for documents.
    try {
        # Get primary administrator group object. The o_groupno should always be 1 for this group.
if( -not ($GroupName = (Select-PWSQL -SQLSelectStatement "SELECT o_groupname AS GROUPNAME FROM dms_grp WHERE o_groupno = 1").GROUPNAME)){
throw "Failed to retrieve the primary administrator group name."
}

# Get a list of all members of the Administrator group.

$Administrators = Get-PWUsersInGroup -GroupName $GroupName -ErrorAction Stop
Write-Host "[PROCESS] The '$GroupName' group contains $($Administrators.Count) members." -ForegroundColor Green

# Return array of user objects.
Write-Output $Administrators } catch { Write-Warning -Message "[PROCESS] An error occurred while attempting to retrieve the members of the primary administrators group."
Write-Warning -Message "[PROCESS] $($Error[0].Exception.Message)" } # end try/catch... } # end PROCESS...

END

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

END {

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

} # 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 Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

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