@@ -146,6 +146,67 @@ public static void TemporaryModifyNetworkInfo(NetworkInfo networkInfo)
146146 }
147147
148148 private static void ExecutePowerShellCommand ( string command )
149+ {
150+ // If adding a new route, check if it exists first and use different approach
151+ if ( command . StartsWith ( "New-NetRoute" ) )
152+ {
153+ // Extract destination prefix and next hop from the command
154+ string destinationPrefix = ExtractParameter ( command , "-DestinationPrefix" ) ;
155+ string nextHop = ExtractParameter ( command , "-NextHop" ) ;
156+
157+ if ( ! string . IsNullOrEmpty ( destinationPrefix ) && ! string . IsNullOrEmpty ( nextHop ) )
158+ {
159+ // Create a modified command that checks if the route exists first
160+ string modifiedCommand = $@ "
161+ $route = Get-NetRoute -DestinationPrefix '{ destinationPrefix } ' -ErrorAction SilentlyContinue
162+ if ($route) {{
163+ # Route exists, remove it first
164+ Remove-NetRoute -DestinationPrefix '{ destinationPrefix } ' -Confirm:$False -ErrorAction SilentlyContinue
165+ }}
166+ # Now add the new route using the original parameters
167+ { command }
168+ " ;
169+
170+ ExecuteRawPowerShellCommand ( modifiedCommand ) ;
171+ return ;
172+ }
173+ }
174+
175+ // For other commands, execute normally
176+ ExecuteRawPowerShellCommand ( command ) ;
177+ }
178+
179+ private static string ExtractParameter ( string command , string paramName )
180+ {
181+ // Simple parameter extraction
182+ int startIndex = command . IndexOf ( paramName ) + paramName . Length ;
183+ if ( startIndex > paramName . Length )
184+ {
185+ startIndex = command . IndexOf ( "\" " , startIndex ) ;
186+ if ( startIndex > 0 )
187+ {
188+ int endIndex = command . IndexOf ( "\" " , startIndex + 1 ) ;
189+ if ( endIndex > startIndex )
190+ {
191+ return command . Substring ( startIndex + 1 , endIndex - startIndex - 1 ) ;
192+ }
193+ }
194+ else
195+ {
196+ // Try without quotes
197+ startIndex = command . IndexOf ( " " , command . IndexOf ( paramName ) + paramName . Length ) + 1 ;
198+ if ( startIndex > 0 )
199+ {
200+ int endIndex = command . IndexOf ( " " , startIndex ) ;
201+ if ( endIndex < 0 ) endIndex = command . Length ;
202+ return command . Substring ( startIndex , endIndex - startIndex ) ;
203+ }
204+ }
205+ }
206+ return string . Empty ;
207+ }
208+
209+ private static void ExecuteRawPowerShellCommand ( string command )
149210 {
150211 ProcessStartInfo processStartInfo = new ProcessStartInfo
151212 {
@@ -166,8 +227,11 @@ private static void ExecutePowerShellCommand(string command)
166227 string output = process . StandardOutput . ReadToEnd ( ) ;
167228 string error = process . StandardError . ReadToEnd ( ) ;
168229
169- if ( ! string . IsNullOrEmpty ( error ) )
230+ if ( ! string . IsNullOrEmpty ( error ) &&
231+ ! error . Contains ( "Instance MSFT_NetRoute already exists" ) &&
232+ ! error . Contains ( "ObjectNotFound" ) ) // Ignore errors when removing non-existent routes
170233 {
234+ Debug . WriteLine ( $ "PowerShell error: { error } ") ;
171235 throw new InvalidOperationException ( $ "Error executing PowerShell command: { error } ") ;
172236 }
173237 }
0 commit comments