Getting Started with Functions

Noob question: Just getting started with Universal Dashboard and I’m getting stuck on converting one of my simple scripts to run in UD. Mainly I’m not sure how to use functions and pass information to the UD elements. I found this forum post that explained this, but I’m unable to get this working. Simplified example of the script:

function New-Password {
    return "ExamplePassword"
    }

$newPasswordEndInit = New-UDEndpointInitialization -Function "New-Password"

$Dashboard = New-UDDashboard -Title "Generate Password" -Content {
    New-UDCard -Title 'Generate Password' -Content {
        New-UDButton -Text "Generate Password" -OnClick { 
            Show-UDToast -Message $getPassword = New-Password
        } #-Endpoint {"New-Password"}
        New-UDParagraph -Text 'Woud like to output here.'
    }

} -EndpointInitialization $newPasswordEndInit

Start-UDDashboard -Dashboard $Dashboard -Port 10001 -AutoReload

If I uncommend the -Endpoint {“New-Password”}, then the whole UDCard fails to load. When I click the button it tells me "Cannot bind argument to parameter ‘Message’ because it is null. I know I’m probably way off on calling that function there. Ultimately I would like to click the button and have a generated password append paragraph below the button and you could click again to generate a second or third one. I didn’t see anything in the documentation to push text into another element kind of like Javascript’s get element by id.

Try this:

$getPassword = New-Password
Show-UDToast -Message $getPassword
1 Like

Thanks for the really fast reply. That fixed it. I was sure I tried that before but must not have.

For a follow up question, how would I have the OnClick event of that button send a string to the UDParagraph below it?

You can use Set-UDElement and New-UDElement like this.

  New-UDButton -Text "Generate Password" -OnClick { 
             $getPassword = New-Password
              Set-UDElement -Id 'output' -Content { $getPassword }
       }
       New-UDElement -Id 'output' -Tag 'p'
2 Likes

Thanks again.

For anyone else who finds this, here’s the final example code.

function New-Password {
return "ExamplePassword"
}

$newPasswordEndInit = New-UDEndpointInitialization -Function "New-Password"

$Dashboard = New-UDDashboard -Title "Generate Password" -Content {
    New-UDCard -Title 'Generate Password' -Content {
        New-UDButton -Text "Generate Password" -OnClick { 
            $getPassword = New-Password
            Set-UDElement -Id 'output' -Content { $getPassword }
        }
        New-UDElement -Id 'output' -Tag 'p'
    }
} -EndpointInitialization $newPasswordEndInit

Start-UDDashboard -Dashboard $Dashboard -Port 10001 -AutoReload