jenkinsci / jenkinsci/durable-task-plugin
[JENKINS-67036] Jenkins Powershell wrapper breaks valid Powershell scripts
- Dominant language
- Java
- Stars
- 30
- Forks
- 101
- PR merge metrics
- No merged PRs in 30d
Description
The following script fails when run in Jenkins:
node("windows") {
powershell('''\
function Invoke-NativeCommand {
# Do something useful here
}function New-GitInvoker([string]$Path) {
return {
Invoke-NativeCommand git `-C $Path @args
}.GetNewClosure()
}
$git = New-GitInvoker 'testRepoPath'
& $git fetch `-p ssh://someUrl
'''.stripIndent()
)
}
The error is:
```
powershell.exe : Invoke-NativeCommand : The term 'Invoke-NativeCommand' is not recognized as the name of a cmdlet, function, script
At D:\Jenkins\workspace\@tmp\durable-38d496fa\powershellWrapper.ps1:3 char:1
+ & powershell -NoProfile -NonInteractive -ExecutionPolicy Bypass -Comm ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (Invoke-NativeCo...nction, script :String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At D:\Jenkins\workspace\@tmp\durable-38d496fa\powershellScript.ps1:7 char:3
+ Invoke-NativeCommand git `-C $Path @args
+ ~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Invoke-NativeCommand:String) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : CommandNotFoundException
```
When run directly using PowerShell, the same script runs to completion without error. Perhaps the wrapper messes up closures?
A workaround exists - simply write the powershell script to a file and run it directly using powershell in the "bat" pipeline command:
bat 'powershell -NoProfile -File myscript.ps1'
Jenkins 2.289.1
durable-task plugin version 1.37
Pipeline: Nodes and Processes 2.39
---
Originally reported by timpatt, imported from: Jenkins Powershell wrapper breaks valid Powershell scripts
Raw content of original issue
The following script fails when run in Jenkins:
node("windows") {
powershell('''\
function Invoke-NativeCommand {
# Do something useful here
}function New-GitInvoker([string]$Path) {
return {
Invoke-NativeCommand git `-C $Path @args
}.GetNewClosure()
}
$git = New-GitInvoker 'testRepoPath'
& $git fetch `-p ssh://someUrl
'''.stripIndent()
)
}The error is:
powershell.exe : Invoke-NativeCommand : The term 'Invoke-NativeCommand' is not recognized as the name of a cmdlet, function, scriptAt D:\Jenkins\workspace\<job_name>@tmp\durable-38d496fa\powershellWrapper.ps1:3 char:1
+ & powershell -NoProfile -NonInteractive -ExecutionPolicy Bypass -Comm ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (Invoke-NativeCo...nction, script :String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.At D:\Jenkins\workspace\<job_name>@tmp\durable-38d496fa\powershellScript.ps1:7 char:3
+ Invoke-NativeCommand git `-C $Path @args
+ ~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Invoke-NativeCommand:String) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : CommandNotFoundExceptionWhen run directly using PowerShell, the same script runs to completion without error. Perhaps the wrapper messes up closures?
A workaround exists - simply write the powershell script to a file and run it directly using powershell in the "bat" pipeline command:
bat 'powershell -NoProfile -File myscript.ps1'
Jenkins 2.289.1
durable-task plugin version 1.37
Pipeline: Nodes and Processes 2.39
Contributor guide
Assessment
This issue has not been assessed yet.