Page 1 of 1

Call function display to textbox

Posted: Thu Nov 08, 2018 5:50 pm
by chexmix
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
    }
}

Re: Call function display to textbox

Posted: Thu Nov 08, 2018 6:03 pm
by jvierra
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
}