MegaBites-AI/Windows-powershell
0372
1# Copyright (c) Microsoft Corporation.2# Licensed under the MIT License.3 4function Start-PwshProcess5{6 param (7 [string] $FilePath = $null8 )9 10 if ($FilePath -eq $null -or $FilePath -eq "") {11 $FilePath = Join-Path -Path $PSHOME -ChildPath 'pwsh'12 }13 14 $proc = Start-Process -FilePath $FilePath -WindowStyle Hidden -PassThru -ErrorAction Stop15 16 return $proc.Id17}18 19Describe 'NamedPipe Custom Remote Connection Tests' -Tags 'Feature','RequireAdminOnWindows' {20 21 BeforeAll {22 if (!$IsWindows) {23 return24 }25 Import-Module -Name Microsoft.PowerShell.NamedPipeConnection -ErrorAction Stop26 27 $script:PwshProcId = Start-PwshProcess28 $script:session = $null29 }30 31 AfterAll {32 if (!$IsWindows) {33 return34 }35 if ($null -ne $script:session)36 {37 Remove-PSSession -Session $script:session38 }39 40 if ($script:PwshProcId -gt 0)41 {42 Stop-Process -Id $script:PwshProcId43 }44 }45 46 It 'Verifies that New-NamedPipeSession succeeds in connectiong to Pwsh process' -Skip:(!$IsWindows) {47 $script:session = New-NamedPipeSession -ProcessId $script:PwshProcId -ConnectingTimeout 10 -Name CustomNPConnection -ErrorAction Stop48 49 # Verify created PSSession50 $script:session.State | Should -BeExactly 'Opened'51 $script:session.Availability | Should -BeExactly 'Available'52 $script:session.Name | Should -BeExactly 'CustomNPConnection'53 $script:session.Transport | Should -BeExactly 'PSNPTest'54 $script:session.ComputerName | Should -BeExactly "LocalMachine:$($script:PwshProcId)"55 }56 57 It 'Verifies timeout error when trying to connect to pwsh process with current connection' -Skip:(!$IsWindows) {58 $brokenSession = New-NamedPipeSession -ProcessId $script:PwshProcId -ConnectingTimeout 2 -Name CustomNPConnection -ErrorAction Stop59 60 # Verify expected broken session61 $brokenSession.State | Should -BeExactly 'Broken'62 $brokenSession.Availability | Should -BeExactly 'None'63 $brokenSession.Runspace.RunspaceStateInfo.Reason.InnerException.GetType().Name | Should -BeExactly 'TimeoutException'64 65 $brokenSession | Remove-PSSession66 }67}68 