MegaBites-AI/Windows-powershell
0308
1# Copyright (c) Microsoft Corporation.2# Licensed under the MIT License.3 4Describe "Get-PSHostProcessInfo tests" -Tag CI {5 BeforeAll {6 $si = [System.Diagnostics.ProcessStartInfo]::new()7 $si.FileName = "pwsh"8 $si.Arguments = "-noexit"9 $si.RedirectStandardInput = $true10 $si.RedirectStandardOutput = $true11 $si.RedirectStandardError = $true12 $pwsh = [System.Diagnostics.Process]::Start($si)13 14 if ($IsWindows) {15 $si.FileName = "powershell"16 $powershell = [System.Diagnostics.Process]::Start($si)17 }18 }19 20 AfterAll {21 $pwsh | Stop-Process22 23 if ($IsWindows) {24 $powershell | Stop-Process25 }26 }27 28 It "Should return own self" {29 (Get-PSHostProcessInfo).ProcessId | Should -Contain $PID30 }31 32 It "Should return own self window title" {33 $expected = (Get-Process -Id $PID).MainWindowTitle34 (Get-PSHostProcessInfo -Id $PID).MainWindowTitle | Should -BeExactly $expected35 }36 37 It "Should list info for other PowerShell hosted processes" {38 # Creation of the named pipe is async39 Wait-UntilTrue {40 Get-PSHostProcessInfo | Where-Object { $_.ProcessId -eq $pwsh.Id }41 } | Should -BeTrue42 $pshosts = Get-PSHostProcessInfo43 $pshosts.Count | Should -BeGreaterOrEqual 144 $pshosts.ProcessId | Should -Contain $pwsh.Id45 }46 47 It "Should list Windows PowerShell process" -Skip:(!$IsWindows) {48 # Creation of the named pipe is async49 Wait-UntilTrue {50 Get-PSHostProcessInfo | Where-Object { $_.ProcessId -eq $powershell.Id }51 } | Should -BeTrue52 $psProcess = Get-PSHostProcessInfo | Where-Object { $_.ProcessName -eq "powershell" }53 $psProcess.Count | Should -BeGreaterOrEqual 154 $psProcess.ProcessId | Should -Contain $powershell.id55 }56 57 It "Verifies named pipe filepath get method" {58 $pipeFilePath = (Get-PSHostProcessInfo -Id $pid).GetPipeNameFilePath()59 $pipeFilePath | Should -Exist60 }61 62 It "Verifies named pipe filepath is removed on process exit" {63 $aliveFile = Join-Path -Path $TestDrive -ChildPath 'AliveFileXXZZ.txt'64 "" | Out-File -FilePath $aliveFile65 $testfilePath = Join-Path -Path $TestDrive -ChildPath 'TestScriptXXZZ.ps1'66 @'67 param (68 [string] $LiveFilePath69 )70 71 $count = 072 while ((Test-Path -Path $LiveFilePath) -and ($count++ -lt 60))73 {74 Start-Sleep -Milliseconds 50075 }76 77 exit78'@ | Out-File -FilePath $testfilePath79 80 # Create PowerShell process to monitor.81 $psFileName = $IsWindows ? 'pwsh.exe' : 'pwsh'82 $psPath = Join-Path -Path $PSHOME -ChildPath $psFileName83 $psProc = Start-Process -FilePath $psPath -ArgumentList "-File $testfilePath -LiveFilePath $aliveFile" -PassThru84 Wait-UntilTrue -sb {85 (Get-PSHostProcessInfo -Id $psProc.Id) -ne $null86 } -TimeoutInMilliseconds 5000 -IntervalInMilliseconds 25087 88 # Verify named pipe file path.89 $psNamedPipePath = (Get-PSHostProcessInfo -Id $psProc.Id).GetPipeNameFilePath()90 $psNamedPipePath | Should -Exist91 92 # Signal PowerShell test process to exit normally.93 Remove-Item -Path $aliveFile -Force -ErrorAction Ignore94 Wait-UntilTrue -sb {95 (Test-Path -Path $psNamedPipePath) -eq $false96 } -TimeoutInMilliseconds 5000 -IntervalInMilliseconds 25097 98 # Verify named pipe file path is removed.99 $psNamedPipePath | Should -Not -Exist100 }101}102 