A single function parameter that accepts a pw document object, url, urn, guid, folder/file path and returns a document object

Not sure if anyone else will find this useful but I got tired of writing functions that had different parameters for different ways of pointing at a projectwise document. so here's an example of a function that accepts one parameter -Document and parses the parameter to get it to a projectwise document object.

If a projectwise document object is passed in it just sends it through (if you wanted to you could have it re-fetch the document and add -populatepath or -slow)

If a string is passed it the string is evaluated and will fetch a projectwise document if it's a URL, URN, GUID, or Folder Path + Filename.

Function Example-Function {
	<#
	.SYNOPSIS
		Takes something that points at a ProjectWise Document and returns a
		ProjectWise Document object
	
	.DESCRIPTION
		Accepts a string or ProjectWise Document object from pwps_dab and returns
		a pwps_dab ProjectWise Document object.
		
		Strings can be a path to the file (including the filename), a ProjectWise
		URL or URN or just a GUID string.
		
	.PARAMETER Document
		Path to ProjectWise document. Can be a ProjectWise document object or a
		ProjectWise URL or a GUID or just a path.
		
	.EXAMPLE
		$path = 'Path\to\Projectwise\Filename.ext'
		$pwdoc = Example-Function -Document $path
	
	.EXAMPLE
		$url = 'pw://intsrv:dsource/Documents/Path/to/ProjectWise/Filename.ext'
		$pwdoc = Example-Function -Document $url

	.EXAMPLE
		$guid = '{e2e88a23-a959-4d09-ad29-bab7bf5275dc}'
		$pwdoc = Example-Function -Document $guid

	.EXAMPLE
		$urn = 'pw://intsrv:dsource/Documents/D{e2e88a23-a959-4d09-ad29-bab7bf5275dc}'
		$pwdoc = Example-Function -Document $urn
	#>
	[CmdletBinding()]
	Param (
		[Parameter(
			Mandatory, ValueFromPipeline, ValueFromPipelineByPropertyName,
			HelpMessage = 'ProjectWise document. Can be a path, guid, URL or ProjectWise document object.'
		)]
		[ValidateNotNullorEmpty()]
		[Object]$Document
	)

	$pwd = $null
	$docType = $Document.GetType()
	if ($docType -eq [PWPS_DAB.CommonTypes+ProjectWiseDocument]) {
		$pwd = $Document
	} elseif ($docType -eq [String]) {
		if ($document -like "pw:*") {
			# this extracts everything after the /Documents/ part of the URL
			# then lets the normal string handling deal with it
			$document = ($document.split('/\',5))[4]
		} 

		$warnResult = $null
		if (($document -like "{*}") -or ($document -like "D{*}")) {
			$document = $document.TrimStart("D")
			$pwd = Get-PWDocumentsByGUIDs -DocumentGUIDs $document -wa SilentlyContinue -wv WarnResult
			if (-not [string]::IsNullOrEmpty($warnResult)) {
				Throw $WarnResult
			}
		} else {
			# assume all other strings are a folder path with filename at the end
			$path = Split-Path $document -parent
			$file = Split-Path $document -leaf
			try {
				$pwd = Get-PWDocumentsBySearch -FolderPath $path -FileName $file -JustThisFolder -PopulatePath -wa SilentlyContinue -wv warnResult
			} catch {
				$pwd = $null
				$warnResult = "Error occurred retrieving document with GUID $($document). GUID may be invalid."
			}
			if (-not [string]::IsNullOrEmpty($warnResult)) {
				Throw $WarnResult
			}
		}
	} else {
		Throw "Invalid object type passed. Document parameter must be a ProjectWise document or a string object."
	}
	
	return $pwd
}

It assumes you're already logged into the correct projectwise datasource so for a url/urn it won't login in first, just extracts the path and then gets the document object.