1
+ # requires -Module PowerShellLogging , ThreadJob
2
+ param ($Count = 4 )
3
+
4
+ Describe " Working when called in a remote runspace" - Tag " Remoting" {
5
+
6
+ $Path = " TestDrive:\log{0}.txt"
7
+ $Path = (Join-Path (Convert-Path (Split-Path $Path )) (Split-Path $Path - Leaf))
8
+
9
+ BeforeAll {
10
+ $script :PowerShell = $ (
11
+ foreach ($index in 1 .. $Count ) {
12
+ $LocalHost = [System.Management.Automation.Runspaces.WSManConnectionInfo ]@ {ComputerName = " ." ; EnableNetworkAccess = $true }
13
+ $Runspace = [runspacefactory ]::CreateRunspace($LocalHost )
14
+ $Runspace.Open ()
15
+ $PowerShell = [PowerShell ]::Create()
16
+ $PowerShell.Runspace = $Runspace
17
+ $PowerShell
18
+ }
19
+ )
20
+ }
21
+
22
+ AfterAll {
23
+ foreach ($PS in $script :PowerShell ) {
24
+ $PS.Runspace.Dispose ()
25
+ $PS.Dispose ()
26
+ }
27
+ }
28
+
29
+ $TestScript = "
30
+ Import-Module PowerShellLogging
31
+ `$ Logging = Enable-LogFile -Path '${Path} '
32
+ Write-Host 'This is a host test from attempt {0}'
33
+ '${Path} '
34
+ Start-Sleep 2
35
+ Write-Verbose 'This is a verbose test from attempt {0}' -Verbose
36
+ Disable-LogFile `$ Logging
37
+ "
38
+
39
+ It " Should not crash when used" {
40
+ $script :Results = & {
41
+ $i = 0
42
+ foreach ($PS in $script :PowerShell ) {
43
+ $i += 1
44
+ Start-ThreadJob { param ($PS , $Script ) $PS.AddScript ($Script ).Invoke() } - ArgumentList $PS , ($TestScript -f $i )
45
+ }
46
+ } | Wait-Job | Receive-Job
47
+ }
48
+
49
+ It " Should not interfere with output" {
50
+ $script :Results.Count | Should - Be $script :PowerShell.Count
51
+ $i = 0
52
+ foreach ($resultPath in $script :Results ) {
53
+ $i += 1
54
+ $resultPath | Should - Be ($Path -f $i )
55
+ }
56
+ }
57
+
58
+ It " Should not cause Enable-LogFile to fail" {
59
+ foreach ($PS in $script :PowerShell ) {
60
+ $PS.Streams.Error.InvocationInfo.MyCommand.Name | Should -Not - Contain " Enable-LogFile"
61
+ }
62
+ }
63
+
64
+ It " Should not cause Write-Host to fail" {
65
+ foreach ($PS in $script :PowerShell ) {
66
+ $PS.Streams.Error.InvocationInfo.MyCommand.Name | Should -Not - Contain " Write-Host"
67
+ }
68
+ }
69
+
70
+ It " Should not cause Write-Verbose to fail" {
71
+ foreach ($PS in $script :PowerShell ) {
72
+ $PS.Streams.Error.InvocationInfo.MyCommand.Name | Should -Not - Contain " Write-Verbose"
73
+ }
74
+ }
75
+
76
+ It " Should not cause Disable-LogFile to fail" {
77
+ foreach ($PS in $script :PowerShell ) {
78
+ $PS.Streams.Error.InvocationInfo.MyCommand.Name | Should -Not - Contain " Disable-LogFile"
79
+ }
80
+ }
81
+
82
+ It " Should not cause any errors" {
83
+ foreach ($PS in $script :PowerShell ) {
84
+ $PS.Streams.Error.Count | Should - Be 0
85
+ }
86
+ }
87
+
88
+ Write-Warning " Expecting $ ( $script :Results.Count ) log files!"
89
+
90
+ It " Should create the log file" {
91
+ # this is enough to prove the logging works
92
+ $i = 0
93
+ foreach ($PS in $script :PowerShell ) {
94
+ $i += 1
95
+ ($Path -f $i ) | Should - Exist
96
+ }
97
+ }
98
+
99
+ $i = 0
100
+ foreach ($PS in $script :PowerShell ) {
101
+ $i += 1
102
+ It " Should log host output to $ ( $Path -f $i ) " - Skip:(! (Test-Path ($Path -f $i ))) {
103
+ (Get-Content ($Path -f $i )) -match " This is a host test from attempt $i $" | Should -Not - BeNullOrEmpty
104
+ }
105
+ }
106
+
107
+ $i = 0
108
+ foreach ($PS in $script :PowerShell ) {
109
+ $i += 1
110
+ It " Should log host output to $ ( $Path -f $i ) " - Skip:(! (Test-Path ($Path -f $i ))) {
111
+ (Get-Content ($Path -f $i )) -match " This is a verbose test from attempt $i $" | Should -Not - BeNullOrEmpty
112
+ }
113
+ }
114
+ }
0 commit comments