Session variables not working: "Content writer cannot be retrieved"

Trying to pass down variables per session to endpoints, just for simple use across endpoints.

Relevant code:

$Session:FolderContentsPath = 'C:\temp'
$Session:FolderContentsTableId = 'FolderContentsTable'
New-UDEndpointInitialization -Variable 'FolderContentsPath','FolderContentsTableId'
New-UDCard -Id 'FolderContentsCard' -Title "Contents of" -Endpoint {
    New-UDInput -Title 'Path:' -Endpoint {
        param($Path)
        $Session:FolderContentsPath = $Path
        Sync-UDElement -Id $Session:FolderContentsTableId
    }
    New-UDTable -Id $Session:FolderContentsTableId -Headers "Mode","LastWriteTime","Length","Name" -Endpoint {
        Get-ChildItem $Session:FolderContentsPath | Out-UDTableData -Property "Mode","LastWriteTime","Length","Name"
    }    
}

Gives the error:

New-UDDashboard : Exception calling "Invoke" with "0" argument(s): "Content writer cannot be retrieved for the 'Session' provider for the path 'FolderContentsPath'. Object reference not

set to an instance of an object."

Only relevant line from UD log is:

09:37:35 [Debug] SessionDriveVariableProvider GetContentWriter - FolderContentsPath, then the log ends.

PSVersion:

Name                           Value
----                           -----
PSVersion                      5.1.18362.145
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.18362.145
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

What do I need to change to get session variables working?

@GSGBen

Welcome to this forum.

A few days there was a thread that came to my mind when I was reading your question.
I cañnot find it anymore but the problem could be that the $Session provider does not exist in the code you are referring to. That means that you are init a session variable in a context where no session (user) exists.

As far as I know, but never used, a session variable can only be used (and set) in a page.
Otherwise you have to use a cache variable ($Cache).

Hope this will help …

@GSGBen

Another thing that came to my mind is that you are trying to init a $Session variable with New-UDEndpointInitialization -Variable

I dont know your full code but this is not required (or maybe possible).

See https://docs.universaldashboard.io/endpoints/endpoint-initialization

Hi @GSGBen,

As mr @augustin.ziegler mentioned, you cannot include a $session variable in the new-udendpointinitialization.

$cache and $session variables are magical and will be avaliable to all of your endpoints without initzialization. $session will not be avaliable to non-user context endpoints though.

Read this:

https://docs.universaldashboard.io/endpoints/custom-variable-scopes

1 Like

Thanks all!

I recently had a similar issue when using $Session variables in a -Content code block.

I know this has already been solved, but for anyone reading this who’s having the same error and the above answers weren’t the cause, try using an -Endpoint parameter instead of -Content. Alternatively, you can wrap the sections of your code that use Session variables in small endpoints to provide better resource utilization.