Tabs using external ps1 files

Hey, so I am wondering if I can use seperate files to host the content of PU commands that will make up a tab.

When I tried this:

New-UDTab -Text ‘Item Three’ -Content {(Get-Content “C:\XXXX UD\Testing\Pages\test1.ps1”)}

test1.ps1 contains
New-UDHeading -Text “Update clients password” -Size 1

It fails. any ideas? It would just make the seperate coding simpler to handle rather than all in one file.

Thanks

It just displays the code text rather than running it as code.

Funny I was just working on doing the same exact thing (and having my own issues with figuring out $PSScriptroot on v3 dashboards)

For your issue, you’re gonna want to do this:
New-UDTab -Text ‘Item Three’ -Content { . “C:\XXXX UD\Testing\Pages\test1.ps1” }
(Edit: Removed errant “Get-Content” from my example)

That does not seem to work either

New-UDTab -Text ‘Item Three’ -Content {. get-content “C:\XXXX UD\Testing\test1.ps1”} (I moved its location)

Still just renders the text not actually running the code:

image

$UDScriptRoot = $PSScriptRoot

I had to use this code to allow loading of items dynamically as pages:

$Pages = @()
$Pages += . “$PSScriptRoot\pages\home.ps1”
$pagestoload = Get-ChildItem “$PSScriptRoot\pages” -Recurse -Exclude “home.ps1” -filter *.ps1
foreach ($pagetoload in $pagestoload)
{
$Pages += . $pagetoload.fullname
}
New-UDDashboard -Pages $Pages -Title “XXXX”

If that helps

Works great for the menu bar, but I would rather load tabs I think for what I need. but would like to seperate the code yet its just now working.

maybe dont need the get-content.

Yup that fixed it…

New-UDTab -Text ‘Item Three’ -Content {. “C:\XXXX UD\Testing\test1.ps1”}

Oops, yep. I originally posted it without the get-content but decided to copy your folder structure in and I screwed it up!

No worries. Got there in the end and thats what is important :smiley: thanks for helping out

If you happen to run into the same issue i had trying to use relative paths for folder:

This was answered on another thread, but for $PSScriptroot you’ll have to set this into another variable in your dashboard ps1 prior to calling new-uddashboard.
for example, first line on my dashboard.ps1:
$UDRoot = $PSscriptroot

Once you have this, you can dot source your files as you have done:

New-UDTabs -Tabs {
    New-UDTab -Text 'Item One' -Content {. "$UDRoot\overwatch\tab1.ps1"}
    New-UDTab -Text 'Item Two' -Content { New-UDTypography -Text 'Item Two' -Variant 'h2' }
    New-UDTab -Text 'Item Three' -Content { New-UDTypography -Text 'Item Three' -Variant 'h2' }
}

I did this the other day too. This isn’t quite as easy as just dot-sourcing the script file haha haha (thanks @CaelFrost) - but it worked for me…

So if you save your raw code in a .ps1 file like udcardcontent.ps1, with maybe the whole file just something like this…

New-UDCard -Title "Welcome to this UDCard" -Content {
    New-UDElement -Tag p -Content {
	    New-UDTypography -Text "Testing this out." -Paragraph
	    New-UDHtml -Markup "<BR>"
	    New-UDButton -Variant outlined -Text "click me" -OnClick { Invoke-UDRedirect -Url "https://forums.ironmansoftware.com/t/tabs-using-external-ps1-files/3530" }
    }
}

Then in your dashboard/page .ps1 file you can pull in that code by…

New-UDPage -Name "Home" -Content {
    $sbTest = Get-Command 'C:\temp\udcardcontent.ps1' | Select-Object -ExpandProperty ScriptBlock 
    New-UDGrid -Item -Content {
        $sbTest.Invoke()
    }
}

or of course, simplifying it…

New-UDPage -Name "Home" -Content {    
    New-UDGrid -Item -Content {
        (Get-Command 'C:\temp\udcardcontent.ps1' | Select-Object -ExpandProperty ScriptBlock ).Invoke()
    }
}

but @CaelFrost’s way is much simpler :smiley:

New-UDPage -Name "Home" -Content {   
    New-UDGrid -Item -Content {
        . 'C:\temp\udcardcontent.ps1'
    }
} 

Maybe you could set up a cache variable first from an initialisation page, which makes it available to all your pages…

$cache:sbTest = Get-Command 'C:\temp\udcardcontent.ps1' | Select-Object -ExpandProperty ScriptBlock 

Now on any page, when you want to insert that code, just add…

New-UDPage -Name "Home1" -Content {    
    New-UDGrid -Item -Content {
        $cache:sbTest.Invoke()
    }
}

New-UDPage -Name "Home2" -Content {    
    New-UDGrid -Item -Content {
        $cache:sbTest.Invoke()
    }
}