## p4get-client ## aaron bockelie <# .SYNOPSIS Returns all client objects in server, or a single specific client. .DESCRIPTION This function will return all clientspecs on the server, or a single named spec. Calling the function with no parameter will return all clientspecs in the server. Calling the function with a clientspec name will return a specific clientspec if it exists. .EXAMPLE Return all clients on a server. [PS] p4get-userclient Owner : aaron.bockelie Access : 1292030248 LineEnd : local SubmitOptions : submitunchanged Host : abockelie Root : c:\work\infosys Description : Created by aaron.bockelie. Options : noallwrite noclobber nocompress unlocked nomodtime normdir Update : 1291761503 client : abockelie Owner : aaron.bockelie Access : 1291767376 LineEnd : local SubmitOptions : submitunchanged ... ... .EXAMPLE Return a specific client from the server. [PS] p4get-userclient abockelie Owner : aaron.bockelie Access : 1292030248 LineEnd : local SubmitOptions : submitunchanged Host : abockelie Root : c:\work\infosys Description : Created by aaron.bockelie. Options : noallwrite noclobber nocompress unlocked nomodtime normdir Update : 1291761503 client : abockelie .PARAMETER clientname Optional parameter to return a specific client. #> Function p4get-client {param([array]$clientnames)#begin function p4remove-object function unchunk_client #This is a nested function to unchunk client object from p4get. This function is not available outside of p4get-client. {param($chunkedclient) $clientchunks = p4get "client -o `"$chunkedclient`"" #p4get returns properties from a single client as two chunks, due to the way perforce likes to stick random carraige return/linefeeds in the comments section. Thanks perforce! $intermediateprop = @() #create an intermediate array to store our properties that we will comb out from clientchunks. foreach ($chunk in $clientchunks) #for each of the two chunks (or more), parse each property in the chunk. (chunk.psobject.properties) { foreach ($property in $chunk.psobject.properties){$intermediateprop += $property} #insert each property into an intermediate table. } $viewobjects = @() $unchunkedclient = new-object pscustomobject #instantiate a new $client object for return from the base function. foreach ($property in $intermediateprop) #pull out the properties we care about and insert them with add-member into the new object. { if ($property.name -notmatch "view") # skip the view, we will enumerate it in a moment. { add-member -inputobject $unchunkedclient -membertype $property.membertype -name $property.name -value $property.value -force } if ($property.name -match "view") #now handle the line if it is a view. do not add as a member yet, we need to perform additional sorting to it. { $viewobjects += @{$property.name = $property.value} } } #since the view is randomly sorted, and the numeral (viewxx) is a string not an int, it's hard to match. Let's comb it together into order. $i=-1 #set increment to -1 (we want to start at 0) $view = @() do { foreach ($object in $viewobjects) { if ($object.keys -eq "View"+$i) { $view += $object.values } } $i++ } while ($i -le $viewobjects.count) add-member -inputobject $unchunkedclient -membertype NoteProperty -name View -value $view $unchunkedclient.Access = [datetime]::ParseExact($unchunkedclient.Access, "yyyy/MM/dd HH:mm:ss",$null) $unchunkedclient.Update = [datetime]::ParseExact($unchunkedclient.Update, "yyyy/MM/dd HH:mm:ss",$null) $unchunkedclient } $servercheck = p4get-server if ($servercheck.pingsuccess.equals($true)) { #get a fast list of all clients on server. We'll use this to match up client names for what is requested and refuse to return a client if it does not exist. Otherwise we'll return a clientspec that looks correct but is not there. (ooh a ghost) $allClients = p4get clients if (!$clientnames) { $cmd = "p4get clients" $clients = iex $cmd | %{$_.client} p4get-client $clients } else { if ($clientnames.count -gt 100) { $progressexpectation = "This might take a while." } else { $progressexpectation = "Just a moment." } $i=0 $clientNamesCount = $clientNames.count foreach ($client in $clientnames) { if ($allClients | ?{$_.client -eq $client}) { unchunk_client $client write-progress -id 15 -activity ("Returning " + $clientNamesCount + " full clientspec(s). " + $progressexpectation) -status ("Returning client " + $client) -percentcomplete ($i/$clientNamesCount*100) } else { write-error "Could not locate client $client" -category ObjectNotFound } $i++ } write-progress -id 15 -completed -activity "Complete." -status "Complete." } } }