@@ -32,14 +32,19 @@ Import-Module .\common.psm1 -Force -ArgumentList ($LogFileName) -WarningAction S
3232function Invoke-OnHostOrVM {
3333 param (
3434 [Parameter (Mandatory = $true , Position = 0 )][ScriptBlock ] $ScriptBlock ,
35- [Parameter (Mandatory = $false )][object []] $ArgumentList = @ ()
35+ [Parameter (Mandatory = $false )][object []] $ArgumentList = @ (),
36+ [Parameter (Mandatory = $false )][System.Management.Automation.Runspaces.PSSession ] $Session
3637 )
3738 if ($script :ExecuteOnHost ) {
3839 & $ScriptBlock @ArgumentList
3940 } elseif ($script :ExecuteOnVM ) {
4041 $Credential = New-Credential - Username $script :Admin - AdminPassword $script :AdminPassword
4142 if ($script :VMIsRemote ) {
42- Invoke-Command - ComputerName $script :VMName - Credential $Credential - ScriptBlock $ScriptBlock - ArgumentList $ArgumentList - ErrorAction Stop
43+ if ($null -ne $Session ) {
44+ Invoke-Command - Session $Session - ScriptBlock $ScriptBlock - ArgumentList $ArgumentList - ErrorAction Stop
45+ } else {
46+ Invoke-Command - ComputerName $script :VMName - Credential $Credential - ScriptBlock $ScriptBlock - ArgumentList $ArgumentList - ErrorAction Stop
47+ }
4348 } else {
4449 Invoke-Command - VMName $script :VMName - Credential $Credential - ScriptBlock $ScriptBlock - ArgumentList $ArgumentList - ErrorAction Stop
4550 }
@@ -120,7 +125,8 @@ function Remove-eBPFProgram {
120125function Start-ProcessHelper {
121126 param (
122127 [Parameter (Mandatory = $true )] [string ] $ProgramName ,
123- [string ] $Parameters
128+ [string ] $Parameters ,
129+ [Parameter (Mandatory = $false )][System.Management.Automation.Runspaces.PSSession ] $Session
124130 )
125131 $scriptBlock = {
126132 param ($ProgramName , $Parameters , $WorkingDirectory )
@@ -129,7 +135,7 @@ function Start-ProcessHelper {
129135 }
130136 $argList = @ ($ProgramName , $Parameters , $script :WorkingDirectory )
131137 Write-Log " Starting process $ProgramName with arguments $Parameters "
132- Invoke-OnHostOrVM - ScriptBlock $scriptBlock - ArgumentList $argList
138+ Invoke-OnHostOrVM - ScriptBlock $scriptBlock - ArgumentList $argList - Session $Session
133139}
134140
135141function Stop-ProcessHelper {
@@ -354,6 +360,11 @@ function Invoke-ConnectRedirectTestHelper
354360 $ProgramName = " tcp_udp_listener.exe"
355361 Add-FirewallRule - RuleName " Redirect_Test" - ProgramName $ProgramName - LogFileName $LogFileName
356362
363+ if ($script :VMIsRemote ) {
364+ $Credential = New-Credential - Username $script :Admin - AdminPassword $script :AdminPassword
365+ $Session = New-PSSession - ComputerName $script :VMName - Credential $Credential
366+ }
367+
357368 if ($script :TestMode -eq " Regression" ) {
358369 # Previous versions of tcp_udp_listener did not suport the local_address parameter, use old parameter sets.
359370 $TcpServerParameters = " --protocol tcp --local-port $DestinationPort "
@@ -364,7 +375,7 @@ function Invoke-ConnectRedirectTestHelper
364375 $ParameterArray = @ ($TcpServerParameters , $TcpProxyParameters , $UdpServerParameters , $UdpProxyParameters )
365376 foreach ($parameter in $ParameterArray )
366377 {
367- Start-ProcessHelper - ProgramName $ProgramName - Parameters $parameter
378+ Start-ProcessHelper - ProgramName $ProgramName - Parameters $parameter - Session $Session
368379 }
369380 } else {
370381 # Build array of all IP addresses from all interfaces
@@ -381,7 +392,7 @@ function Invoke-ConnectRedirectTestHelper
381392 foreach ($IPAddress in $IPAddresses ) {
382393 foreach ($Protocol in $Protocols ) {
383394 foreach ($Port in $Ports ) {
384- Start-ProcessHelper - ProgramName $ProgramName - Parameters " --protocol $Protocol --local-port $Port --local-address $IPAddress "
395+ Start-ProcessHelper - ProgramName $ProgramName - Parameters " --protocol $Protocol --local-port $Port --local-address $IPAddress " - Session $Session
385396 }
386397 }
387398 }
@@ -416,6 +427,11 @@ function Invoke-ConnectRedirectTestHelper
416427 Invoke-OnHostOrVM - ScriptBlock $scriptBlock - ArgumentList $argList
417428
418429 Stop-ProcessHelper - ProgramName $ProgramName
430+
431+ if ($null -ne $Session )
432+ {
433+ Remove-PSSession - Session $Session - ErrorAction SilentlyContinue
434+ }
419435}
420436
421437function Stop-eBPFComponents {
0 commit comments