Frequently I want to verify a document, folder or user exists before I do something so that if it doesn't exist I can give a decent message in the log or to the user (me). But using the various Get- functions to retrieve the object and do something if that fails seems slow if I don't need all the info?
Would it be possible to get test cmdlet that return much quicker if the object does or doesn't exist?
Test-PWDocument -InputFolder -FolderPath -FolderID -InRoot -DocumentName (accepts wildcard) -FileName (accepts wildcard) Test-PWFolder -InputFolder -FolderPath -FolderID -InRoot -FolderName (accepts wildcard) Test-PWUser -UserName -UserID -Email -Identity
Where FolderPath/FolderID/InputFolder/InRoot is the parent directory to search in. I would not recurse the search into subfolders.
Hey Kevin,
Here is a function that I use which is similar to what you are trying to do. It only works with documents and folders, but may be useful.
<# .Synopsis Test if folder or document exists. .DESCRIPTION Used to determine if a folder or document exists. .EXAMPLE Returns the document guid and name of any documents found. Uses wildcards. $Splat_Test = @{ Type = 'Document' FolderID = 179 Name = 'a111-view' FileName = '1.dwg' } $resultsDocuments = Find-PWIfExists @Splat_Test -UseWildCards -Verbose .EXAMPLE Returns the folder guid and name of any folders found. Uses wildcards. $Splat_Test = @{ Type = 'Folder' Name = 'CAD_Data' } $resultsFolders = Find-PWIfExists @Splat_Test -UseWildCards -Verbose .OUTPUTS Returns a list of datarows containing the object guid and name. #> FUNCTION Find-PWIfExists{ [CmdletBinding()] [OutputType([string])] Param ( # Type of object to test for. [Parameter(Mandatory=$true)] [ValidateNotNullOrEmpty()] [ValidateSet('Document', 'Folder')] [string] $Type, # Folder ID of object to test for (Document or Folder). [Parameter()] [ValidateNotNullOrEmpty()] [int] $FolderID, # Name of object to test for. [Parameter()] [ValidateNotNullOrEmpty()] [string] $Name, # Name of object to test for. [Parameter(ParameterSetName='Doc')] [ValidateNotNullOrEmpty()] [string] $FileName, [switch] $UseWildCards ) # end Param... BEGIN { $CmdletName = $MyInvocation.MyCommand.Name $StartTime = Get-Date Write-Host -Message "[BEGIN] $StartTime - Entering '$CmdletName' Function..." -ForegroundColor Green } # end BEGIN... PROCESS{ #region TEST FOR FOLDER if($Type.ToLower().Equals('folder')){ try { if([string]::IsNullOrEmpty($Name)){ throw "Folder Name must be included." } $Where = "WHERE o_projectname LIKE '%$NAME%'" if( -not ($UseWildCards)){ $Where = $Where.Replace('%', [string]::Empty) } $SQLSelectStatement = "SELECT o_projguid AS ProjectGUID, o_projectname AS Name FROM dms_proj $Where" Write-Host "SQL query '$SQLSelectStatement'" -ForegroundColor Green $results = Select-PWSQL -SQLSelectStatement $SQLSelectStatement -ErrorAction Stop Write-Host "$($results.Rows.Count) documents returned." -ForegroundColor Green } catch { Write-Warning -Message $Error[0].Exception.Message } } #endregion TEST FOR FOLDER #region TEST FOR DOCUMENT if($Type.ToLower().Equals('document')){ try{ if([string]::IsNullOrEmpty($Name) -and [string]::IsNullOrEmpty($FileName)){ throw "Either Name or FileName must be included." } $Where = [string]::Empty if( -not ([string]::IsNullOrEmpty($FolderID))){ $Where = "WHERE o_projectno = $FolderID" } if( -not ([string]::IsNullOrEmpty($Name))){ if($Where.Contains('WHERE')){ $Where = "$Where AND o_itemname LIKE '%$Name%'" } else { $Where = "WHERE o_itemname LIKE '%$Name%'" } } if( -not ([string]::IsNullOrEmpty($FileName))){ if($Where.Contains('WHERE')){ $Where = "$Where AND o_filename LIKE '%$FileName%'" } else { $Where = "WHERE o_filename LIKE '%$FileName%'" } } if( -not ([string]::IsNullOrEmpty($Where))){ if( -not ($UseWildCards)){ $Where = $Where.Replace('%', [string]::Empty) } $SQLSelectStatement = "SELECT o_docguid AS DocGUID, o_itemname AS NAME, o_filename AS FileName FROM dms_doc $Where" Write-Host "SQL query '$SQLSelectStatement'" -ForegroundColor Green $results = Select-PWSQL -SQLSelectStatement $SQLSelectStatement -ErrorAction Stop Write-Host "$($results.Rows.Count) documents returned." -ForegroundColor Green } } catch { Write-Warning -Message $Error[0].Exception.Message } } #endregion TEST FOR DOCUMENT if( -not ([string]::IsNullOrEmpty($results))){ Write-Output $results } else { Write-Host "No documents found." -ForegroundColor Cyan } } # end PROCESS... END { $EndTime = Get-Date Write-Verbose -Message "[END] It took $($EndTime - $StartTime) to complete the process." -Verbose Write-Verbose -Message "[END] $EndTime - Exiting '$CmdletName' Function..." -Verbose } # end END... } # end FUNCTION Find-PWIfExists... Export-ModuleMember -Function Find-PWIfExists