## p4revert-file ## aaron bockelie <# .SYNOPSIS Unlocks and reverts all files for a given client. .DESCRIPTION Reverting files for a different user and clientspec can be tedious. This function assists the task by automating the unlock and revert task. After the task is complete, a record of the reverted file is returned in a record object. You must have Perforce super permissions in order for this function to operate correctly. .EXAMPLE This example revert a single file, given a full perforce depot path. The user who opened the file is listed in the returned status. [PS] C:\Users\aaronadmin>p4revert-file //depot/tools/p4powershell/depots.xml revertcomplete client notreverted reverted -------------- ------ ----------- -------- True abockelie //depot/tools/p4powershe... The returned object contains the following objects: revertcomplete: a boolean value returning the status of the command. This is false if there are any files not reverted client: the name of the client operated against. notreverted: an array of depot files that could not be reverted. reverted: an array of depot files that were successfully reverted. useropened: the user that previously had the file open. .PARAMETER clientname The client you wish to revert and unlock all files on. .FUNCTIONALITY This function simplifies the administrative overhead of reverting any and all files for a given client. #> Function p4revert-file {param([Parameter(Mandatory = $true)]$depotfile)#begin function p4remove-object $servercheck = p4get-server $unlockstatus = $null $revertstatus = $null $cantrevertfiles = $null $errorprefs = $erroractionpreference #store original error action preference. $erroractionpreference = "SilentlyContinue" $UserElevatedPrivs = $false if ($servercheck.pingsuccess.equals($true)) { $fileobject = p4get "opened -a" $depotfile if (!$fileobject) { $fileobject = p4get "files " $depotfile if (!$fileobject) { $message = "Requested depot file " + $depotfile + " cannot be found in the server " + $servercheck.server write-error $message } else { $message = "Requested depot file " + $fileobject.depotfile + " was found, but not opened for edit." write-warning $message } } else { $authgroup = p4get-authgroups #grab the auth groups for this depot $authgroup = $authgroup[0].name #the first auth group in the array is always the parent authgroup $groupobject = p4get-group $authgroup #get the details of the authgroup (memberships, owners, subgroups etc) if (!$groupobject | %{$_.users -notcontains $client.owner}) { p4tempauth -grant -username $client.owner -authgroup $authgroup -justification ("Automated attempt to revert file "+ +" for user " +$client.owner) $UserElevatedPrivs = $true } $client = p4get-client $fileobject.client #get the client details that the file is checked out on. p4 login $client.owner >$null | out-null $activity = "Unlocking checked out files on client " + $client.client p4 unlock -f $fileobject.depotfile 2>$unlockstatus | out-null $activity = "Reverting checked out files on client " + $client.client p4 shelve -d -f $fileobject.depotfile 2>$shelvestatus | out-null p4 -c $client.client -H $client.Host -u $client.Owner revert -k $fileobject.depotfile 2>$revertstatus | out-null $cantrevertfiles = p4get "opened -C" $fileobject.depotfile if ($cantrevertfiles) { $revertstatus = $false $message = "Unable to revert file `'" + $fileobject.depotfile + "`'. Please check p4 server logs, permissions, or other criteria." write-error $message } else { $revertstatus = $true } } } if ($userelevatedprivs = $true) { p4tempauth -revoke -username $client.owner -authgroup $authgroup -silent $UserElevatedPrivs = $false } return new-object psobject -property @{"client" = $client.client;"reverted" = $fileobject.depotfile;"notreverted" = $cantrevertfiles;"revertcomplete" = $revertstatus;"useropened" = $fileobject.user} $erroractionpreference = $errorprefs }