$Task1_Config = ...; # , . function Task1_CheckRequirements() {} # , . function Task1_CanExecute($project) {} # . function Task1_Execute($project, $context) {}
function Task1() { $result = New-Object -Typename PSObject -Property ` @{ "name" = "Task1" "config" = ... } Add-Member -InputObject $result -MemberType ScriptMethod -Name CheckRequirements -Value ` { } Add-Member -InputObject $result -MemberType ScriptMethod -Name CanExecute -Value ` { Param($project) } Add-Member -InputObject $result -MemberType ScriptMethod -Name Execute -Value ` { Param($project, $context) } return $result }
Unable to process remote command data. Error message: WSMan provider lead process did not return the correct answer. Vendor in leading process may behave incorrectly.
The WSMan has not been accepted. A provider may be improperly.
$ErrorActionPreference = "Stop" $cred = New-Object System.Management.Automation.PsCredential(...) function runLocal($sb, $cnt) { Write-Host "Local $cnt" Invoke-Command -ScriptBlock $sb -ArgumentList @($cnt) } function runRemote($sb, $cnt) { Write-Host "Remote $cnt" $s = New-PSSession "." -credential $cred try { Invoke-Command -Session $s -ScriptBlock $sb -ArgumentList @($cnt) } finally { Remove-PSSession -Session $s } }
$scriptBlock1 = { Param($cnt) function test($cnt) { if($cnt -ne 0) { test $($cnt - 1) return } Write-Host " Call depth: $($(Get-PSCallStack).Count)" } test $cnt } runLocal $scriptBlock1 3000 runRemote $scriptBlock1 150 runRemote $scriptBlock1 160 ---------- Local 3000 Call depth: 3004 Remote 150 Call depth: 152 Remote 160 The script failed due to call depth overflow.
$scriptBlock2 = { Param($cnt) function test() { $result = New-Object -Typename PSObject -Property @{ } Add-Member -InputObject $result -MemberType ScriptMethod -Name Execute -Value ` { Param($cnt) if($cnt -ne 0) { $this.Execute($cnt - 1) return } Write-Host " Call depth: $($(Get-PSCallStack).Count)" } return $result } $obj = test $obj.Execute($cnt) } runLocal $scriptBlock2 3000 runRemote $scriptBlock2 130 runRemote $scriptBlock2 135 ---------- Local 3000 Call depth: 3004 Remote 130 Call depth: 132 Remote 135 Processing data for a remote command failed with the following error message: The WSMan provider host process did not return a proper response.
$scriptBlock3 = { Param($cnt) function test() { $result = New-Object -Typename PSObject -Property @{ } Add-Member -InputObject $result -MemberType ScriptMethod -Name Execute -Value ` { Param($cnt) if($cnt -ne 0) { $this.Execute($cnt - 1) return } Write-Host " Call depth: $($(Get-PSCallStack).Count)" throw "error" } return $result } try { $obj = test $obj.Execute($cnt) } catch { Write-Host " Exception catched" } } runLocal $scriptBlock3 130 runRemote $scriptBlock3 5 runRemote $scriptBlock3 6 ---------- Local 130 Call depth: 134 Exception catched Remote 5 Call depth: 7 Exception catched Remote 6 Call depth: 8 The script failed due to call depth overflow.
$scriptBlock4 = { Param($cnt) function test($cnt) { if($cnt -ne 0) { test $($cnt - 1) return } Write-Host " Call depth: $($(Get-PSCallStack).Count)" throw "error" } try { test $cnt } catch { Write-Host " Exception catched" } } runLocal $scriptBlock4 2000 runRemote $scriptBlock4 150 ---------- Local 2000 Call depth: 2004 Exception catched Remote 150 Call depth: 152 Exception catched
$scriptBlock5 = { Param($cnt) Class test { Execute($cnt) { if($cnt -ne 0) { $this.Execute($cnt - 1) return } Write-Host " Call depth: $($(Get-PSCallStack).Count)" throw "error" } } try { $t = [test]::new() $t.Execute($cnt) } catch { Write-Host "Exception catched" } } runLocal $scriptBlock5 130 runRemote $scriptBlock5 7 runRemote $scriptBlock5 8 ---------- Local 130 Call depth: 134 Exception catched Remote 7 Call depth: 9 Exception catched Remote 8 Call depth: 10 The script failed due to call depth overflow.
$scriptBlock6 = { Param($cnt) function Call($self, $scriptName, [parameter(ValueFromRemainingArguments = $true)] $args) { $args2 = @($self) + $args Invoke-Command -ScriptBlock $self.$scriptName -ArgumentList $args2 } function test() { $result = @{ } $result.Execute = { Param($self, $cnt) if($cnt -ne 0) { Call $self Execute $($cnt - 1) return } Write-Host " Call depth: $($(Get-PSCallStack).Count)" throw "error" } return $result } try { $obj = test Call $obj Execute $cnt } catch { Write-Host "Exception catched" } } runLocal $scriptBlock6 1000 runRemote $scriptBlock6 55 runRemote $scriptBlock6 60 ---------- runLocal $scriptBlock6 1000 runRemote $scriptBlock6 55 runRemote $scriptBlock6 60 Local 1000 Call depth: 2005 Exception catched Remote 55 Call depth: 113 Exception catched Remote 60 Exception catched
locally | via winRM | |
Function | > 3000 | ~ 150 |
Object methods | > 3000 | ~ 130 |
Object methods with try-catch | ~ 130 | five |
Function with try-catch | > 2000 | ~ 150 |
Class method (ps5) with try-catch | ~ 130 | 7 |
Hash + script block with try-catch | > 1000 | ~ 55 |
Source: https://habr.com/ru/post/320970/
All Articles