SnV
September 16, 2020, 2:20pm
1
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
SnV
September 16, 2020, 2:23pm
2
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)
SnV
September 16, 2020, 2:27pm
4
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:
SnV
September 16, 2020, 2:31pm
6
$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.
SnV
September 16, 2020, 2:33pm
7
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!
SnV
September 16, 2020, 2:39pm
9
No worries. Got there in the end and thats what is important thanks for helping out
If you happen to run into the same issue i had trying to use relative paths for folder:
Given a dashboard and a folder structure as follows:
c:\ProgramData\UniversalAutomation\Repository
\Repository\Overwatch\Pages
\Repository\Overwatch\Tabs
With Powershell Universal and V3 Dashboards I can’t seem to get a $PSScriptRoot.
The goal would be to do something like :
New-UDDashboard -Title “Overwatch” -Content {
New-UDTabs -Tabs {
New-UDTab -Text 'Item One' -Content {. (join-path $PSScriptRoot "\overwatch\tab1.ps1")}
New-UDTab -Text 'Item Two' -Content { New-UDTypograp…
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' }
}
pharnos
September 17, 2020, 7:37am
12
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
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()
}
}