Pode
Pode copied to clipboard
Can't call functions from some scriptblocks?
trafficstars
When I try to call a function in Register-PodeEvent I get an error. If I move the body of the function into the -ScriptBlock part of Register-PodeEvent it works as expected.
Here is the error I'm receiving:
Date: 2023-12-01 21:55:10
Level: Error
ThreadId: 0
Server: chagwood
Category: ObjectNotFound: (Write-DynamicRoutes:String) [Invoke-PodeScriptBlock], CommandNotFoundException
Message: The term 'Write-DynamicRoutes' is not recognized as the name of a cmdlet, function, script 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.
StackTrace: at <ScriptBlock>, C:\Users\Administrator\Desktop\WebServiceAPI\start-server.ps1: line 37
at Invoke-PodeScriptBlock, C:\Program Files\WindowsPowerShell\Modules\Pode\2.9.0\Public\Utilities.ps1: line 530
at Invoke-PodeEvent, C:\Program Files\WindowsPowerShell\Modules\Pode\2.9.0\Private\Events.ps1: line 22
at Start-PodeInternalServer, C:\Program Files\WindowsPowerShell\Modules\Pode\2.9.0\Private\Server.ps1: line 39
at Start-PodeServer, C:\Program Files\WindowsPowerShell\Modules\Pode\2.9.0\Public\Core.ps1: line 179
at <ScriptBlock>, C:\Users\Administrator\Desktop\WebServiceAPI\start-server.ps1: line 31
at <ScriptBlock>, <No file>: line 1
at Invoke-PodePackageScript, C:\Program Files\WindowsPowerShell\Modules\Pode\2.9.0\Private\Setup.ps1: line 12
at Pode, C:\Program Files\WindowsPowerShell\Modules\Pode\2.9.0\Public\Core.ps1: line 515
at <ScriptBlock>, <No file>: line 1
Here is my code:
Import-Module -Name Pode -MaximumVersion 2.99.99
$global:scriptSubfolderName = "scripts"
function Write-DefaultRoute {
Write-PodeJsonResponse -Value @{ 'value' = 'Welcome to the WebServices API 1.0' }
}
function Write-ScriptRoutes {
if(-not(Test-Path -Path ".\$($global:scriptSubfolderName)")) {
throw "Could not locate scripts subfolder"
} else {
$fullPath = Resolve-Path ".\$($global:scriptSubfolderName)"
Write-Verbose "The scripts folder [$($fullPath.Path)] appears to exist. Continuing..."
}
$scriptFiles = Get-ChildItem -Path ".\$($global:scriptSubfolderName)" -Filter "*.ps1" -File
if($scriptFiles.Count -eq 0){
Write-Verbose "No .ps1 files exist in [$($global:scriptSubfolderName)]. No dynamic routes created."
} else {
$scriptFiles | ForEach-Object {
Write-Verbose "Creating route [$($_.Name.ToLower().Replace('.ps1',''))] from [$($_.FullName)]"
}
}
}
Start-PodeServer -ScriptBlock {
New-PodeLoggingMethod -Terminal | Enable-PodeErrorLogging -Levels Error, Warning, Informational, Verbose
Add-PodeEndpoint -Address localhost -Port 8080 -Protocol Http
Register-PodeEvent -Type Start -Name 'CreateScriptRoutes' -ScriptBlock {Write-ScriptRoutes}
Add-PodeRoute -Method Get -Path '/' -ScriptBlock {Write-DefaultRoute}
}
Hi @chagwood,
If add the global: flag to the functions it should work: function global:Write-ScriptRoutes {}.
I think I can make Pode do this automatically by adding the following into Pode's Import-PodeFunctionsIntoRunspaceState function:
New-Item -Path function: -Name "global:$($func.Name)" -Value $func.Group[-1].Definition -Force
I tested it briefly just now, and seems to do the trick, so I'll look at adding it in.