Product: PowerShell Universal Version: 2026.1.5
Hi, I wanted to reach out to you again regarding a different issue. Is there a way in PowerShell Universal to adjust an existing target script from within an app, using predefined Forms variables (two UD-Selects and one textbox)? I would like to use this app as a basis to revise the parameters of the respective scripts. Example:
New-UDApp -Content {
New-UDCard -Title "Test-App" -Content {
New-UDDivider
New-UDElement -Tag 'br'
New-UDStack -Direction 'column' -Spacing 3 -Content {
New-UDForm -Content {
New-UDSelect -Id 'SelectArt' -Label 'Art Selection' -Option {
New-UDSelectOption -Name 'IT' -Value 'IT'
New-UDSelectOption -Name 'Sales' -Value 'Sales'
New-UDSelectOption -Name 'Manager' -Value 'Manager'
} -DefaultValue 'Alle' -FullWidth
New-UDSelect -Id 'SelectOU' -Label 'OU Selection' -Option {
$ADSystemOUs = Get-ADOrganizationalUnit -Filter * -SearchBase $Searchbase -SearchScope OneLevel -Properties | Sort-Object Name
foreach ($ou in $ADSystemOUs) {
New-UDSelectOption -Name $ou.Name -Value $ou.DistinguishedName
}
} -FullWidth -Multiple
New-UDTextbox -Id 'TxtAdGroup' -Label 'AD-Group' -Placeholder 'AD-Group' -FullWidth
New-UDElement -Tag 'br'
} -OnSubmit {
$Art = $EventData.SelectArt
$OU = $EventData.SelectOU
$Group = $EventData.TxtAdGruppe
} -ButtonVariant outlined -SubmitText "Submit"
} -FullWidth
}
}
I would like to use the above-mentioned code, as described, to save an existing script (and others) with the data selected in the app. I know that I can use Get-PSUScript to call the respective scripts, but so far I have not found a way to pass information to them.
Recommended Answer
I have found the solution to my problem. I need to reference the Script ID in set-psuscript in order for the editing to be carried out.
Here is the final code:
# Replace Area
$UpdatedContent = $Script.Content -replace '(\$Area\s*=\s*".*")', ('$Area= {0}' -f $newArea)
# Replace Target
$UpdatedContent = $UpdatedContent -replace '(\$Target\s*=\s*".*")', ('$Target= {0}' -f $newTarget)
Set-PSUScript -id $Script.id -Content $UpdatedContent -ComputerName $serverAddress -AppToken $appTokenCan’t you invoke the script with the paramaters you’ve selected?
universal-docs/cmdlets/Invoke-PSUScript.txt at v5 · ironmansoftware/universal-docs
-OnSubmit {
$Art = $EventData.SelectArt
$OU = $EventData.SelectOU
$Group = $EventData.TxtAdGruppe
Invoke-PSUScript -Name "Script to invoke" -Parameters @{
Art = $Art
OU = $OU
Group = $Group
}
}
If you’re looking to save the script with hard parameters, you can use placeholders;
For example, if you created the script below in a file named test.ps1
# Filename test.ps1 Write-Output “Hello ~HelloWhat~”
Then did something similar to this with your app
# App Content
…
New-UDSelect -Id ‘HelloWhat’ -Label ‘HelloWhat’ -Option {
New-UDSelectOption -Name ‘World’ -Value ‘World’
New-UDSelectOption -Name ‘Kitty’ -Value ‘Kitty’
New-UDSelectOption -Name ‘Sailor’ -Value ‘Saillor’
} -DefaultValue ‘Alle’ -FullWidth
…
(Get-PSUScript -Name test.ps1).Content -Replace “~HelloWhat~“, “$($EventData[0])” | Set-Content -Path NewFile.ps1
I’ve put that together off the top of my head and the syntax might not be quite right - but the method should work even though a bit of tweaking might be needed.
hey @AnonymousUser, thx for your reply. I ll take a try tomorrow and send you a feedback.
Hey there, the solution in my case would be set-psuscript. But, when i run the job, i ll get an error-message:
Set-PSUScript -Name $ScriptName -Content $UpdatedContent -AppToken $appToken -ComputerName $serverAddress
Status(StatusCode="Cancelled", Detail="No message returned from method.")
Thats the code I use
$ScriptContent = Get-PSUScript -Name $ScriptName -ComputerName $serverAddress -AppToken $appToken
$UpdatedContent = $ScriptContent.content.Replace('(\$Area\s*=\s*".*")','$Area = {0}' -f $newArea).Replace('(\$Target\s*=\s*".*")','$Target = {0}' -f $newTarget)
Set-PSUScript -Name $ScriptName -Content $UpdatedContent -AppToken $appToken -ComputerName $serverAddressI have found the solution to my problem. I need to reference the Script ID in set-psuscript in order for the editing to be carried out.
Here is the final code:
# Replace Area
$UpdatedContent = $Script.Content -replace '(\$Area\s*=\s*".*")', ('$Area= {0}' -f $newArea)
# Replace Target
$UpdatedContent = $UpdatedContent -replace '(\$Target\s*=\s*".*")', ('$Target= {0}' -f $newTarget)
Set-PSUScript -id $Script.id -Content $UpdatedContent -ComputerName $serverAddress -AppToken $appToken