MegaBites-AI/Windows-powershell
0372
1# Copyright (c) Microsoft Corporation.2# Licensed under the MIT License.3Describe "InvokeOnRunspace method argument error handling" -tags "Feature" {4 5 BeforeAll {6 $command = [System.Management.Automation.PSCommand]::new()7 $localRunspace = $Host.Runspace8 }9 10 It "Null argument exception should be thrown for null PSCommand argument" {11 { [System.Management.Automation.HostUtilities]::InvokeOnRunspace($null, $localRunspace) } |12 Should -Throw -ErrorId "PSArgumentNullException"13 }14 15 It "Null argument exception should be thrown for null Runspace argument" {16 { [System.Management.Automation.HostUtilities]::InvokeOnRunspace($command, $null) } |17 Should -Throw -ErrorId "PSArgumentNullException"18 }19}20 21Describe "InvokeOnRunspace method as nested command" -tags "Feature" {22 23 It "Method should successfully invoke command as nested on busy runspace" {24 25 $command = [System.Management.Automation.PSCommand]::new()26 $command.AddScript('"Hello!"')27 $currentRunspace = $Host.Runspace28 29 $results = [System.Management.Automation.HostUtilities]::InvokeOnRunspace($command, $currentRunspace)30 31 $results[0] | Should -Be "Hello!"32 }33}34 35Describe "InvokeOnRunspace method on remote runspace" -tags "Feature","RequireAdminOnWindows" {36 37 BeforeAll {38 $skipTest = (Test-IsWinWow64) -or !$IsWindows39 40 if ($skipTest) {41 return42 }43 44 $script:remoteRunspace = New-RemoteRunspace45 }46 47 AfterAll {48 if ($script:remoteRunspace)49 {50 $script:remoteRunspace.Dispose();51 }52 }53 54 It "Method should successfully invoke command on remote runspace" -Skip:$skipTest {55 56 $command = [System.Management.Automation.PSCommand]::new()57 $command.AddScript('"Hello!"')58 59 $results = [System.Management.Automation.HostUtilities]::InvokeOnRunspace($command, $script:remoteRunspace)60 61 $results[0] | Should -Be "Hello!"62 }63}64 65Describe 'PromptForCredential' -Tags "CI" {66 BeforeAll {67 [System.Management.Automation.Internal.InternalTestHooks]::SetTestHook('NoPromptForPassword', $true)68 }69 70 AfterAll {71 [System.Management.Automation.Internal.InternalTestHooks]::SetTestHook('NoPromptForPassword', $false)72 }73 74 It 'Should accept no targetname' {75 $out = $Host.UI.PromptForCredential('caption','message','myUser',$null)76 $out.UserName | Should -BeExactly 'myUser'77 }78 79 It 'Should accept targetname as domain' {80 $out = $Host.UI.PromptForCredential('caption','message','myUser','myDomain')81 $out.UserName | Should -BeExactly 'myDomain\myUser'82 }83}84 85Describe 'PushRunspaceLocalFailure' -Tags 'CI' {86 It 'Should throw an exception when pushing a local runspace' {87 $runspace = [RunspaceFactory]::CreateRunspace()88 try {89 $runspace.Open()90 $exc = { $Host.PushRunspace($runspace) } | Should -Throw -PassThru91 $exc.Exception.InnerException | Should -BeOfType ([System.ArgumentException])92 [string]$exc | Should -BeLike "*PushRunspace can only push a remote runspace. (Parameter 'runspace')*"93 }94 finally {95 $runspace.Dispose()96 }97 }98}99 