## p4get ## Aaron Bockelie - adapted on regex splitter/combiner idea from Ralph Lewis <# .SYNOPSIS Runs perforce commands and returns powershell objects for the results. .DESCRIPTION Runs perforce commands and returns powershell objects for the results. Not all objects returned via perforce have all the possible attributes set. This means that sometimes you will get objects that have some attributes set, but other objects might lack the attribute entirely. .EXAMPLE p4get users This will return powershell objects with attributes named after the perforce fields, with the appropriate values set. .EXAMPLE p4get "files //depot/..." This will return powershell objects for all the files under //depot/... .PARAMETER Command Command to run, seperated by spaces for arguments for the command. If you are issuing a command that has multiple parts seperated by spaces, enclose the command in quotes. #> function p4get {param( [Parameter(Mandatory=$true,ValueFromRemainingArguments=$true)][string[]]$Command ) $result = @() $LastErrorActionPreference = $ErrorActionPreference $ErrorActionPreference = "Stop" $cmd = $Command -join " " $cmd = "p4 -ztag " + $cmd try { $p4Results = iex $cmd 2>&1 } catch { write-error $error[0] } #funny thing about $p4Results is that it is an array of strings, we want one big long one, so lets join them $p4ResultsFixed = $p4Results -join "`n" # ztag returns data in strings like this: #... User zpeterson #... Update 1173264248 #... Access 1289961618 #... FullName Zach Peterson #... Email zpeterson@example.com #... Password enabled # #Each block is seperated by an empty newline, each entry may or may not have all the attributes, each entry starts with ... # this regex finds all the 'blocks' like the above, returns them as a match collection $ms =[regex]::matches( $p4ResultsFixed, '(?m)((?:^\.\.\. \w+ (.|)+\n)+)') foreach( $m in $ms ) { # we will put the attributes into a hash to make a powershell object out of $h=@{} # split each group into lines $lines = $m.groups[0].value -split "`n" # Roll through each line foreach( $line in $lines ) { # Remove the beginning ... $line = $line -replace "^\.\.\. ","" # match the attribute name and attribute value if( $line -match "^(\w+) (.*)$" ) { # stick them in the hash $h[$matches[1]] = $matches[2] } } # if we found something, emit a powershell object with the appropriately named attributes and values if( $h ) { New-Object psobject -property $h } } } #end p4get