Live terminal with output from the script

Product: PowerShell Universal
Version: 3.6.3

Hi,

We have been using a simple PSU dashboard for a while now, that lets the helpdesk restore deleted users in Active Directory. The code itself is working fine, but we are missing some “live logs”.
This is the code:

New-UDDashboard -Title 'Restore AD User' -Content {
    New-UDDynamic -Id 'table' -Content {
        New-UDButton -Text 'Refresh' -OnClick {
            Sync-UDElement -Id 'table'
        } -Icon (New-UDIcon -Icon 'Sync') -Color info
        $Data = Get-ADObject -IncludeDeletedObjects -Filter * -Properties displayname, whencreated,whenchanged | Where-Object {$_.ObjectClass -eq "User" -and $_.Deleted -eq $true} | Select-Object Name,whenchanged,whencreated,ObjectGuid,DisplayName
        New-UDTable -Id 'SamAccountSelection' -Data $Data -ShowPagination -PageSize 10 -ShowExport -ShowSort -ShowSearch -ShowSelection
        New-UDButton -Text "Restore User" -OnClick {
            $Value = Get-UDElement -Id "SamAccountSelection"
            $DeletedUserName = $Value.SelectedRows.Name
            $DeletedGuid = $Value.SelectedRows.ObjectGuid
            $DeletedDisplayName = $Value.SelectedRows.DisplayName
            Show-UDToast -Message "Restoring $($DeletedUserName)" -Duration 3000
            
            ##### START - This is the section we want to be shown
            #Checking if a new user with the same display name have been created, since the first one was deleted.
            Write-Output "Checking if $DeletedDisplayName already exists"
            $ExistingUser = Get-ADUser -Filter "DisplayName -eq '$($DeletedDisplayName)'"
            if ($ExistingUser) {
                Write-Output "An user with the displayname $DeletedDisplayName already exist"
                break
            }
            else {
                Write-Output "Confirmed that $DeletedDisplayName does NOT already exists"
            }
            Try {
                Write-Output "Restoring $DeletedUserName"
                $TempVar = Restore-ADObject -Identity $DeletedGuid -ErrorAction stop
                Write-Output "Restored $DeletedUserName"
            }
            catch {
                Show-UDToast -Message "Failed at restoring $($DeletedUserName) - $($_.Exception.Message)" -Duration 3000
            }
            ##### STOP - This is the section we want to be shown
        }
    } 

My goal is, that when the user clicks on the “Restore User” button, a “terminal” is shown at the bottom of the screen, that updates every second.
The output from write-output (or a PSU equivilant of that function) is then shown in the terminal.

How do i achieve this ?

1 Like