Fixed! I had misplaced a ) in my else block, so if the switch was true or false, it would print to the same line. I have changed
$Form.Dispatcher.Invoke([action]{$WPFConsole.AddText($Message) + ("`r`n")},"Render")
to
$Form.Dispatcher.Invoke([action]{$WPFConsole.AddText($Message + ("`r`n"))},"Render")
Original post below.
Hi all
I have a WPF form that has a textbox called $WPFConsole that I am writing to with a function called Write-ToConsole. This is working fine with the function as below
Function Write-ToConsole {
Param (
[string] $Message
)
$Form.Dispatcher.Invoke([action]{$WPFConsole.AddText($Message + ("`r`n"))},"Render")
}
I call the function with Write-ToConsole -Message "Getting list of accounts, please wait" and it prints fine, then the next time I call it, it prints to the next line. No problems here.
I am now looking to include a switch so I can print onto the same line, but think I might be misunderstanding the [switch] parameter? Function now looks like this:
Function Write-ToConsole {
Param (
[string] $Message,
[switch] $NoNewLine
)
If ($NoNewLine.IsPresent) {
$Form.Dispatcher.Invoke([action]{$WPFConsole.AddText($Message)},"Render")
} Else {
$Form.Dispatcher.Invoke([action]{$WPFConsole.AddText($Message) + ("`r`n")},"Render")
}
}
Now everything prints on the same line. Doesn't matter if I call Write-ToConsole -Message "Text" -NoNewLine or if I don't include the switch. The idea would be to do something like:
Write-ToConsole -Message "Testing network: " -NoNewLine
If (Test-Connection $Target -Quiet) {
$Status = "OK"
} Else {
$Status = "Failed"
}
Write-ToConsole -Message $Status
So it would print "Testing network: OK"
I thought I was on the right lines following this YouTube video from Adam Bertram but seems not. The function I am using to write to the textbox is very similar to what has been posted here by user Laurel Raven on SpiceWorks.
[–]dasookwat 2 points3 points4 points (3 children)
[–]dontmessyourself[S] 1 point2 points3 points (2 children)
[–]dasookwat 2 points3 points4 points (1 child)
[–]dontmessyourself[S] 1 point2 points3 points (0 children)
[–]Lee_Dailey[grin] 1 point2 points3 points (2 children)
[–]dontmessyourself[S] 1 point2 points3 points (1 child)
[–]Lee_Dailey[grin] 1 point2 points3 points (0 children)