Function p4put-triggers {param([Parameter(Mandatory=$true)]$triggerArray,$insertAt,[switch]$force) $triggers = $null $additionalTriggers = $null # This is a small function to combine two arrays. There is an assumption that the arrays have an insert() method. # Given an index to start the insert, the rest of the array will be pushed down the stack. function insertArrayAtIndex {param($source,[int]$insertIndex,$values) if ($insertIndex -gt $source.count) { throw ("Index out of bounds for array! Max index is " + $source.count + ", you wanted " + $insertIndex + "`r`n") } $sourceIndex=0 foreach ($line in $values) { $source.Insert($insertIndex,$values[$sourceIndex]) > $null $sourceIndex++ $insertIndex++ } $source } $servercheck = p4get-server if ($servercheck.pingsuccess.equals($true)) { if ($insertAt -eq $null) #insertat is null, so wholesale replacement of triggers spec. Note that 0 (zero) is not null, and is actually handled as a case (top of array) { $newTriggers = "`r`nTriggers:`r`n" foreach ($line in $triggerArray) { $newTriggers += "`t" + $line + "`r`n" } $triggers = new-object System.Collections.ArrayList #create a list object for the original triggers. p4get-triggers | %{$triggers.Add($_)} > null #copy the text array into the list object. } else #we're going to insert before a line in the middle, before the beginning, or after the end. { $triggers = new-object System.Collections.ArrayList #create a list object for the original triggers. p4get-triggers | %{$triggers.Add($_)} > null #copy the text array into the list object. $additionalTriggers = new-object System.Collections.ArrayList #create a list object for the additional triggers. $triggerArray | %{$additionalTriggers.Add($_)} > null #copy the text array into the list object. if ($insertAt.GetType().fullname -eq "System.Int32") { $newTriggers = insertArrayAtIndex -source $triggers -insertIndex $insertAt -values $additionalTriggers } if ($insertAt.GetType().fullname -eq "System.String") { if (($insertAt -match "bottom") -or ($insertAt -match "end")) { "inserted as bottom string" $newTriggers = insertArrayAtIndex -source $triggers -insertIndex $triggers.count -values $additionalTriggers } if (($insertAt -match "top") -or ($insertAt -like "begin*")) { "inserted as top string" $newTriggers = insertArrayAtIndex -source $triggers -insertIndex 0 -values $additionalTriggers } } } $answer = read-host -Prompt "View changes to trigger before saving?[y/n]" #get input for where we want to get the wsdl. could be url or file. if (!$force) { $consoleUI = (get-host).ui.rawui $originalFGColor = $consoleUI.ForegroundColor $consoleUI.ForegroundColor = "Magenta" "Old trigger list:" "`r`nTriggers:" foreach ($line in $triggers) { ("`t" + $line) } $consoleUI.ForegroundColor = "Green" write-host "`r`nNew trigger list:" $newTriggers $newTriggers > out.txt $consoleUI.ForegroundColor = $originalFGColor $answer = read-host -Prompt "Save Trigger?[y/n]" if ($answer -match "y") { try { $cmd = $newTriggers | p4 triggers -i write-host $cmd } catch { write-error $error[0] } } else { write-warning "Triggers not changed." break } } else { try { $newTriggers | p4 triggers -i } catch { write-error $error[0] }#just update trigger, don't confirm or anything. } } }