In this post, we will be creating a PowerShell function which will determine whether or not an Active Directory ( AD) group has synchronized with a ProjectWise (PW) datasource. This is useful after adding a new AD group to be synchronized with a PW datasource. It will look for the AD group every 30 seconds until it is found. The interval is configurable.
All of the ProjectWise related cmdlets are available using the PWPS_DAB module. At the time of this post, I am using version 23..4.1. Take a look at the help for each of the cmdlets to become familiar with their functionality, available parameters, etc.
- Get-PWGroups
FUNCTION Definition
The following is the function wrapper which will contain the remainder of the code.
FUNCTION Verify-ADGroupHasSyncd { <# .Synopsis Used to verify if a Group has been sync'd. .DESCRIPTION Used to verify if a AD group has been sync'd with the current datasource. Runs a do/while loop until the group is found. Plays a sound when it is. .EXAMPLE Loops until the specified group is found. Verify-ADGroupHasSyncd -GroupName 'CAD_Designers' -Verbose .EXAMPLE Loops until the specified group is found. Pauses for 60 seconds between tries. Verify-ADGroupHasSyncd -GroupName 'CAD_Designers' -Interval 60 -Verbose #> [CmdletBinding()] param (...) # end param... BEGIN {...} # end BEGIN... PROCESS {...} # end PROCESS... END{...} # end END... } # end FUNCTION Verify-ADGroupSyncd...
Parameters
First thing we need to do is create our parameter and value pairs. The help messages will explain the purpose for each parameter.
[CmdletBinding()] param ( # Name of the group to verify has been sync'd. [ValidateNotNullOrEmpty()] [Parameter( Mandatory = $true, Position = 0)] [string] $GroupName, # Interval used to pause the loop. Defaults to 30 seconds. [ValidateNotNullOrEmpty()] [Parameter()] [int] $Interval ) # 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..."
} # end BEGIN...
PROCESS
Now, we will proceed to the PROCESS code block. Here we will continually check for the specified Active Directory group until it is found. Once found, a message will be presented and the loop will stop.
PROCESS {
# Loop until a specific record is returned.
$GroupFound = $false
Write-Verbose -Message "[PROCESS] $GroupName..."
do {
if(Get-PWGroups -GroupName $GroupName){
$GroupFound = $true
Write-Verbose -Message "[PROCESS] '$GroupName' found."
# Play a sound
[System.Media.SystemSounds]::Hand.Play()
} else {
Write-Warning -Message "[PROCESS] '$GroupName' not found."
Start-Sleep -Seconds $Interval
}
} while (-not $GroupFound)
} # 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...
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.
