#PowerShell, #PowerWiseScripting, #ProjectWise, PWPS_DAB

HowTo: Remove Users Without Membership

I recently had a request for a script to remove all user accounts from a ProjectWise datasource if a user account was not a member of any Group. In this post, I want to share what I came up with. Also, I took the idea a little further to include UserLists.

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

  • Get-PWUsersByMatch
  • Get-PWGroupsByUser
  • Get-PWUserListsByUser
  • Remove-PWUserByMatch

As previously stated, the intent of this script is to remove ALL user accounts from a ProjectWise datasource where a user is not a member of either a Group or a UserList.

There are a couple of caveat’s to consider.

  • What if you have a user account(s) you’d like to keep, regardless of membership?
  • What do you do with document ownership when a user account is removed and that user was the owner of a document(s)?

To address these concern above, I am going to create an array containing the name(s) of user accounts to be ignored in this process. I am also, going to specify the ProjectWise administrator as the new owner of any documents a user may be the owner of. This could be any user.

One final note, I am using the Write-Host for demonstration purposes only. I typically would be writing information to a log file.

# User names to be ignored.
[string[]] $pwUsersToSkip = @('pwadmin', 'brian.flaherty')

# The user specified will become the owner of a removed user's documents.
[string] $UserNameForItems = 'pwadmin'

Now, lets get a list of all ProjectWise users.

# Returns all of the users within the ProjectWise datasource.
$pwUsers = Get-PWUsersByMatch
Write-Host "$($pwUsers.Count) user objects returned." -ForegroundColor Green

Next, we will loop through all of the user objects returned. We will determine if each user is a member of a Group and/or a UserList. If they are, we skip them and move on to the next user object.

# Loop through all user objects returned.
foreach($pwUser in $pwUsers) {
    Write-Host "$($pwUser.Name)" -ForegroundColor Green

    # Skip the current user is in the pwUsersToSkip array.
    if( $pwUsersToSkip.Contains($pwUser.Name)){
        Write-Host " - skipping user" -ForegroundColor DarkGreen
        continue
    }

    # Get Group membership for the current user.
    $pwGroups = Get-PWGroupsByUser -UserName $pwUser.Name
    if($pwGroups.Count -gt 0){
        Write-Host " - user is a member of a Group(s)." -ForegroundColor DarkGreen
        continue
    }
    # Get UserList membership for the current user.
    $pwUserLists = Get-PWUserListsByUser -UserName $pwUser.Name
    if($pwUserLists.Count -gt 0){
        Write-Host " - user is a member of a UserList(s)." -ForegroundColor DarkGreen
        continue
    }

    # If not a member of a Group or Userlist, remove the user account.
    if( -not ($pwGroups.Count -gt 0 ) -and -not ($pwUserLists.Count -gt 0)) {
    Write-Host " - removing user" -ForegroundColor Magenta 
    Remove-PWUserByMatch -InputUsers $pwUser -UserNameForItems $UserNameForItems
}
} # end foreach($pwUser in $pwUsers...

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 )

Google photo

You are commenting using your Google 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.