$session:myvar not working after update to 2.8.0

after update to version 2.8.0 i do get an error using $session variables …
did something change? Or do I have a ps version problem?

Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt.
In P:\PowerShell\mini_dashboard.ps1:6 Zeichen:2

  • $session:myvar=10
  •  + CategoryInfo          : InvalidOperation: (:) [], NullReferenceException
     + FullyQualifiedErrorId : GetContentWriterProviderException
    
    
    

Get-Module

ModuleType Version Name ExportedCommands


Script 1.0.0.0 ISE {Get-IseSnippet, Import-IseSnippet, New-IseSnippet}
Manifest 3.1.0.0 Microsoft.PowerShell.Management {Add-Computer, Add-Content, Checkpoint-Computer, Clear-Content…}
Manifest 3.0.0.0 Microsoft.PowerShell.Security {ConvertFrom-SecureString, ConvertTo-SecureString, Get-Acl, Get-AuthenticodeSignature…}
Manifest 3.1.0.0 Microsoft.PowerShell.Utility {Add-Member, Add-Type, Clear-Variable, Compare-Object…}
Manifest 3.0.0.0 Microsoft.WSMan.Management {Connect-WSMan, Disable-WSManCredSSP, Disconnect-WSMan, Enable-WSManCredSSP…}
Script 2.8.0 UniversalDashboard {Add-UDElement, Clear-UDCache, Clear-UDElement, ConvertTo-JsonEx…}
Script 2.3.2 UniversalDashboard {Add-UDElement, Clear-UDElement, ConvertTo-JsonEx, Disable-UDLogging…}

Get-Module

ModuleType Version Name ExportedCommands


Script 1.0.0.0 ISE {Get-IseSnippet, Import-IseSnippet, New-IseSnippet}
Manifest 3.1.0.0 Microsoft.PowerShell.Management {Add-Computer, Add-Content, Checkpoint-Computer, Clear-Content…}
Manifest 3.0.0.0 Microsoft.PowerShell.Security {ConvertFrom-SecureString, ConvertTo-SecureString, Get-Acl, Get-AuthenticodeSignature…}
Manifest 3.1.0.0 Microsoft.PowerShell.Utility {Add-Member, Add-Type, Clear-Variable, Compare-Object…}
Manifest 3.0.0.0 Microsoft.WSMan.Management {Connect-WSMan, Disable-WSManCredSSP, Disconnect-WSMan, Enable-WSManCredSSP…}
Script 2.8.0 UniversalDashboard {Add-UDElement, Clear-UDCache, Clear-UDElement, ConvertTo-JsonEx…}
Script 2.3.2 UniversalDashboard {Add-UDElement, Clear-UDElement, ConvertTo-JsonEx, Disable-UDLogging…}

$PSVersionTable

Name Value


PSVersion 5.1.14393.3383
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
BuildVersion 10.0.14393.3383
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1

Can you provide a little bit more of your script? Are you calling $Session:MyVar outside of your dashboard code?

yes … in fact after loading module i do define functions (eg for opening and reading from db).
Inside this functions i do define session variables …

funny thing … starting script dashboard i to get al lot of errors … but function seems to be there.
just testing my applications … looks like powershell doesn´t know $session when starting script …

very easy samle; pls. to not care about the script function …

Import-Module UniversalDashboard

$session:myvar=10

$global:mypage = New-UDPage -ID mypage -Name “mypage” -Endpoint {

  # Input usualy from Database ...
   $dbs=@()

   $bla=1
   while ( $bla -lt 50) {

      $didadum="$bla$bla"

      $myrow = @{
        name   = $bla
        number = $bla
      }                                 
      $dbs=$dbs+$myrow
      $bla+=1
   }

   

   $session:mymemory=0

  # Card
  New-UDCard  -Content {
           

     # Select
     New-UDSelect -ID myselect -MultiSelect -Label ‘select me’ -Option {
        $session:mymemory=0
        foreach ( $value in $dbs ) {
           New-UDSelectOption -Name $value.name -Value $value.number # -Selected
        } 
     } -OnChange {
          $State = $EventData
          Show-UDToast -Message "State: >$State<" -Duration 7000
     } 
              
     # Button
     New-UDButton -Icon ticket -Text "Button" -OnClick {                                                              

        $user_aendern_Datenbank_clearing_eaps=@()                  
        $user_aendern_Datenbank_clearing_eaps=$null

        $helfer=get-UDElement -id "myselect"            
        $helfer=$helfer.Attributes['value']              
        Show-UDToast -Message "Helfer: >$helfer<" -Duration 7000
        
        if ( $helfer -eq $null ) {
           Show-UDToast -Message "nix ausgewählt ...<" -Duration 7000
        }
                                    
        $helfer=$helfer.Replace('"','')
        $helfer=$helfer.Replace('[','')
        $helfer=$helfer.Replace(']','')
        $helfer=$helfer.split(',')                  
        $myselectdbs=$session:dbs
                                                  
        foreach ($wert in $helfer ) {                                           
           $user_aendern_Datenbank_clearing_eaps+=$myselectdbs[$wert].name    
           $myvar=$myselectdbs[$wert].name
           Show-UDToast -Message "Auswahl: >$wert<" -Duration 7000
        }                                                                               
        
     }
  }

}

$FunctionInit=New-UDEndpointInitialization -Variable @(“session:myvar”)

$Dashboard = New-UDDashboard -Title “Multiselect” -Page $global:mypage -EndpointInitialization $FunctionInit
Get-UDDashboard | Stop-UDDashboard
Start-UDDashboard -Dashboard $Dashboard -Port 10000 -AllowHttpForLogin

this was okay in version 2.3.2

The $Session: scope relies on a user having a session and won’t work outside of a dashboard endpoint. If it worked in 2.3.2, it was luck as that is not how it was designed so I wouldn’t rely on that working in 2.8.

You can just set a variable like: $myvar = 10 outside the dashboard and you won’t even have to use EndpointInitialization in 2.8 since it will bring that variable in automatically. Then within your dashboard just use $myvar. The way you are using $session:dbs and $session:mymemory should work.

not like it was designed … but working … :slight_smile: also in 2.8. if you ignore the errors starting the script …
I check if I can solve it in a differend way …

p.s.: I am calling my functions from inside a dashboard endpoint … functions are working …
session is there …

That sound be fine. The only one that won’t work is this:

Import-Module UniversalDashboard

$session:myvar=10

So you’re saying you are using the $Session scope in functions within your dashboard and those aren’t working? Are you seeing any errors in the log or in the dashboard?

So you’re saying you are using the $Session scope in functions within your dashboard and those aren’t working? -> they are working … But lot of errors starting script/Dashboard

Ah ok. Is it more errors than just this?

Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt.
In P:\PowerShell\mini_dashboard.ps1:6 Zeichen:2

$session:myvar=10
 + CategoryInfo          : InvalidOperation: (:) [], NullReferenceException
 + FullyQualifiedErrorId : GetContentWriterProviderException

sorry no more errors … sorry it is in german …

Ok. Cool. Thanks! Have you tried using just a regular variable there rather than a session one?

Hi Adman!

Thank you for your help; I think I found my problem … Problem is between keyboard and char …
What I do; And this is working!

I do define functions using $session and $cache variables to save the results for the endpoints.
Later I do call the functions from inside endpoint. This is working. No problem!

But at one point I called a function befor having a session (from outside endpoint)
this of cause is causing error messages …

Sorry for this
Everything looks fine
And again thank you for your help!
Fabian

1 Like