Can't use global variables like $Roles in dashboard main navigation

Product: PowerShell Universal
Version: 3.7.13

I have a dashboard with dashboard pages where “the main dashboard” just looks a bit like this:

$Navigation = @(
    New-UDListItem -Label "Home" -Icon (New-UDIcon -Icon 'Home') -OnClick {Invoke-UDRedirect -Url '/home'} # DONE
    New-UDListItem -Label "AD" -Icon (New-UDIcon -Icon 'user') -Children {
        New-UDListItem -Label "AD - Search" -Icon (New-UDIcon -Icon 'Search') -OnClick {Invoke-UDRedirect -Url '/AD-Search'} # DONE
        New-UDListItem -Label "AD - Create User with 365 Mailbox" -Icon (New-UDIcon -Icon 'EnvelopeOpen') -OnClick {Invoke-UDRedirect -Url '/AD-UserWithMail'} #Mis fun
    }
    New-UDListItem -Label "Printers" -Icon (New-UDIcon -Icon 'print') -OnClick {Invoke-UDRedirect -Url '/Printers'} # DONE
    if ($Roles -contains "Administrator") {
        New-UDListItem -Label "S-FTP" -Icon (New-UDIcon -Icon 'ArrowRightToFile') -OnClick {Invoke-UDRedirect -Url '/S-FTP'} # Mangler alt
    }
    New-UDListItem -Label "StraksBox" -Icon (New-UDIcon -Icon 'lightbulb') -OnClick {Invoke-UDRedirect -Url '/StraksBox'} # DONE
)

$Pages = @()
$Pages += Get-UDPage -Name 'Home'
$Pages += Get-UDPage -Name 'AD - Search'
$Pages += Get-UDPage -Name 'AD - Create User with 365 Mailbox'
$Pages += Get-UDPage -Name 'Printers'
$Pages += Get-UDPage -Name 'S-FTP'
$Pages += Get-UDPage -Name 'StraksBox'

New-UDDashboard -Title 'Tools' -Pages $Pages -Navigation $Navigation

I would like to make sure only Administrator’s was shown the S-FTP link in the navigation.
I know I can make the restriction directly on the page, wrapping the entire page in the same if statement, but it would be nice to also remove the link from the navigation.

I’ve tried with multiple things, and it looks like “the main page” doesn’t read any global variables, I can’t use $Public:something or $Secret:something either.
Seems like a bug IMO.

Navigation is static and generated only when the dashboard is starting. Use -LoadNavigation instead. It’s called whenever the page is loaded and includes all user\dashboard state.

$Navigation = {
    New-UDListItem -Label "Home" -Icon (New-UDIcon -Icon 'Home') -OnClick {Invoke-UDRedirect -Url '/home'} # DONE
    New-UDListItem -Label "AD" -Icon (New-UDIcon -Icon 'user') -Children {
        New-UDListItem -Label "AD - Search" -Icon (New-UDIcon -Icon 'Search') -OnClick {Invoke-UDRedirect -Url '/AD-Search'} # DONE
        New-UDListItem -Label "AD - Create User with 365 Mailbox" -Icon (New-UDIcon -Icon 'EnvelopeOpen') -OnClick {Invoke-UDRedirect -Url '/AD-UserWithMail'} #Mis fun
    }
    New-UDListItem -Label "Printers" -Icon (New-UDIcon -Icon 'print') -OnClick {Invoke-UDRedirect -Url '/Printers'} # DONE
    if ($Roles -contains "Administrator") {
        New-UDListItem -Label "S-FTP" -Icon (New-UDIcon -Icon 'ArrowRightToFile') -OnClick {Invoke-UDRedirect -Url '/S-FTP'} # Mangler alt
    }
    New-UDListItem -Label "StraksBox" -Icon (New-UDIcon -Icon 'lightbulb') -OnClick {Invoke-UDRedirect -Url '/StraksBox'} # DONE
}

New-UDDashboard -LoadNavigation $Navigation # ...

To be honest, this is confusing and trips a lot of people up but was done for backwards compat. We may change this to be only -Navigation and remove -LoadNavigation in v4. Then -Navigation could accept both an array of static values as well as a script block.

1 Like

It really didn’t like that

Full text:


Error with dashboard script

Cannot process argument transformation on parameter 'LoadNavigation'. Cannot convert the "System.Object[]" value of type "System.Object[]" to type "UniversalDashboard.Models.Endpoint".

at <ScriptBlock>, C:\ProgramData\UniversalAutomation\Repository\dashboards\Tools\Tools.ps1: line 33

at <ScriptBlock>, <No file>: line 1

System.Management.Automation.ParameterBindingArgumentTransformationException: Cannot process argument transformation on parameter 'LoadNavigation'. Cannot convert the "System.Object[]" value of type "System.Object[]" to type "UniversalDashboard.Models.Endpoint". ---> System.Management.Automation.ArgumentTransformationMetadataException: Cannot convert the "System.Object[]" value of type "System.Object[]" to type "UniversalDashboard.Models.Endpoint". ---> System.Management.Automation.PSInvalidCastException: Cannot convert the "System.Object[]" value of type "System.Object[]" to type "UniversalDashboard.Models.Endpoint".

   at System.Management.Automation.LanguagePrimitives.ThrowInvalidCastException(Object valueToConvert, Type resultType)

   at System.Management.Automation.LanguagePrimitives.ConvertNoConversion(Object valueToConvert, Type resultType, Boolean recurse, PSObject originalValueToConvert, IFormatProvider formatProvider, TypeTable backupTable)

   at System.Management.Automation.LanguagePrimitives.ConversionData`1.Invoke(Object valueToConvert, Type resultType, Boolean recurse, PSObject originalValueToConvert, IFormatProvider formatProvider, TypeTable backupTable)

   at System.Management.Automation.LanguagePrimitives.ConvertTo(Object valueToConvert, Type resultType, Boolean recursion, IFormatProvider formatProvider, TypeTable backupTypeTable)

   at System.Management.Automation.ArgumentTypeConverterAttribute.Transform(EngineIntrinsics engineIntrinsics, Object inputData, Boolean bindingParameters, Boolean bindingScriptCmdlet)

   --- End of inner exception stack trace ---

   at System.Management.Automation.ArgumentTypeConverterAttribute.Transform(EngineIntrinsics engineIntrinsics, Object inputData, Boolean bindingParameters, Boolean bindingScriptCmdlet)

   at System.Management.Automation.ParameterBinderBase.BindParameter(CommandParameterInternal parameter, CompiledCommandParameter parameterMetadata, ParameterBindingFlags flags)

   --- End of inner exception stack trace ---

   at System.Management.Automation.ParameterBinderBase.BindParameter(CommandParameterInternal parameter, CompiledCommandParameter parameterMetadata, ParameterBindingFlags flags)

   at System.Management.Automation.CmdletParameterBinderController.BindParameter(CommandParameterInternal argument, MergedCompiledCommandParameter parameter, ParameterBindingFlags flags)

   at System.Management.Automation.CmdletParameterBinderController.BindParameter(UInt32 parameterSets, CommandParameterInternal argument, MergedCompiledCommandParameter parameter, ParameterBindingFlags flags)

   at System.Management.Automation.CmdletParameterBinderController.BindParameters(UInt32 parameterSets, Collection`1 arguments)

   at System.Management.Automation.CmdletParameterBinderController.BindCommandLineParametersNoValidation(Collection`1 arguments)

   at System.Management.Automation.CmdletParameterBinderController.BindCommandLineParameters(Collection`1 arguments)

   at System.Management.Automation.CommandProcessor.BindCommandLineParameters()

   at System.Management.Automation.CommandProcessor.Prepare(IDictionary psDefaultParameterValues)

   at System.Management.Automation.CommandProcessorBase.DoPrepare(IDictionary psDefaultParameterValues)

   at System.Management.Automation.Internal.PipelineProcessor.Start(Boolean incomingStream)

   at System.Management.Automation.Internal.PipelineProcessor.SynchronousExecuteEnumerate(Object input)

--- End of stack trace from previous location where exception was thrown ---

   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()

   at System.Management.Automation.Internal.PipelineProcessor.SynchronousExecuteEnumerate(Object input)

   at System.Management.Automation.PipelineOps.InvokePipeline(Object input, Boolean ignoreInput, CommandParameterInternal[][] pipeElements, CommandBaseAst[] pipeElementAsts, CommandRedirection[][] commandRedirections, FunctionContext funcContext)

   at System.Management.Automation.Interpreter.ActionCallInstruction`6.Run(InterpretedFrame frame)

   at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)


Note that I changed your $Navigation definition from an array to a script block. It looks like you are still passing an array to -LoadNavigaition

Found it documented here:

1 Like

That’s true, didn’t notice that at first glance. It worked, once again thanks a lot!
Love how fast the support are in here :smiley:

1 Like