Call function display to textbox

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Post Reply
chexmix
Posts: 4
Joined: Thu Nov 08, 2018 6:12 am

Call function display to textbox

Post by chexmix » Thu Nov 08, 2018 5:50 pm

I have a function that I call in various places to display information. It works fine in powershell console but I can't figure out how to display it in a textbox.

I call the function like so.

log -l "Trying to import Active Directory module"
log -l "Unable to import Active Directory module" -type "ERROR"

Here is the function code

Code: Select all

function Log {
    #Create a logging function that writes to console, and colour codes according to whether it's an Error or not
    Param($l, $type)
    $time = get-date -f HH:mm:ss
    if ($type -match "error"){
        write-host $time -f Gray -NoNewline ; write-host " ERROR: " -f Red -NoNewline ; write-host $l -f DarkYellow
    }
    elseif ($type -match "warn"){
        write-host $time -f Gray -NoNewline ; write-host " WARNING: " -f Magenta -NoNewline ; write-host $l -f Yellow
    }
    else {
        write-host $time -f Gray -NoNewline ; write-host " INFO: " -f Cyan -NoNewline ; write-host $l -f Green
    }
}

User avatar
jvierra
Posts: 12874
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Call function display to textbox

Post by jvierra » Thu Nov 08, 2018 6:03 pm

Write-Host cannot be used to display anything in a form. Change all write Host output to the following.

$textbox1.Lines += < your message text >

Here is an example:

Code: Select all

function Log {
    Param($l, $type = 'INFO')

     $msg = '[{0:HH:MM:SS}][{1}]{2}' -f [datetime]::Now,$type,$l
    $textbox1.Lines += $msg
    Write-Host $msg # for verification when running in PowerShell Studio
}

Post Reply