I know there is a function similar to this one using Internet Explorer (sorry I’m not an IE fan), besides that this function auto scrolls the log and IE does not 😀

Note: The space between the double quotes in:

$frmLog = " " $dgvLog = " " And in the function in line:

if($script:frmLog -eq " ") Are because of this blog addon you can remove those spaces.

#Initialize the variables before calling the function for the first time. $frmLog = " " $dgvLog = " " #The first time you will call the function a form will be created #You can give it title,width and height. write-log "this is the first line" -Title "My New Form Title" Start-Sleep 1 write-log "this is line 2" Start-Sleep 1 write-log "this is line 3" Start-Sleep 1 write-log "this is line 4" Start-Sleep 1 write-log "this is line 5" #To release the form I didn't find an other way. write-log -Done $true

function write-log{ param( [string]$Str, [string]$Title = "Script Log", [int]$sizeX = 300, [int]$sizeY = 200, [boolean]$Done = $false ) if($script:frmLog -eq " ") { [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") $script:frmLog = New-Object System.Windows.Forms.Form $script:frmLog.Text = $Title $script:frmLog.Size = New-Object System.Drawing.Size($sizeX,$sizeY) $script:frmLog.StartPosition = "CenterScreen" $script:dgvLog = New-Object System.Windows.Forms.DataGridView $script:dgvLog.ColumnHeadersVisible = $false $script:dgvLog.RowHeadersVisible = $false $script:dgvLog.AllowUserToAddRows = $false $script:dgvLog.Dock="fill" $col1 = New-Object System.Windows.Forms.DataGridViewTextBoxColumn $col1.AutoSizeMode = "fill" $script:dgvLog.columns.add($col1) $script:frmLog.Controls.Add($script:dgvLog) $script:frmLog.Visible = $true $script:frmLog.Topmost = $True $script:frmLog.Add_Shown({$script:frmLog.Activate()}) } if($Done) { #To release the form I didn't find an other way $script:frmLog.Visible = $false [void] $script:frmLog.ShowDialog() } else { $script:dgvLog.rows.Add($str) $script:dgvLog.FirstDisplayedScrollingRowIndex = $script:dgvLog.RowCount - 1 } }

Share this: Twitter

Facebook

Like this: Like Loading... Related

Tagged: Start-Sleep, write-log