We help IT Professionals succeed at work.

Can You Do In PowerShell Something Like Freeze Pane/Header In Excel?

IBeSmitty
IBeSmitty asked
on
Hello!

In Excel you can do something like Freeze Header/Pane where you can scroll through the worksheet while a header row at the top stays put.  That way you always have header information at the top while you scroll through the data.  Make sense?  Is there a way to do that in PowerShell?

I've included code below for what I'm calling a Monitoring Shell.  Basically I want software testers where I work to be able to give an ad-hoc PowerShell command as a parameter to this script, and have this monitor the output of the given command.  I have a header row printing every refresh cycle, but I've been testing with short output type of commands.  If there is a lot of data, then the header will scroll out of view.  This is where I'd like to do the Excel-like Freeze Pane/Header.

I thought about processing each line of output of the command in say a for each item type loop and calculating visible lines, etc., but I'm not sure I can do that when dealing with unknown commands and pipelines.  I'd rather not restrict what kind of command or pipeline the user can provide to this script, at least as much as possible.

Does anyone have any suggestions or ideas?  Thanks in advance!  :)  Also if anyone has any ideas for improving this script, I'd be interested in hearing them (feature-wise or syntax).  I'm just learning PowerShell so please be gentle if there is better syntax for things than what I have here.  :)  Also, if you want to use this script in your own work, that's fine just please give me credit in a comment or something.  I do plan on releasing/publishing this somewhere, just have not figured out where yet.

Jason

# ####################################################################
# PowerShell
# 12 June 2010
# Smith, Jason Alan
# Monitoring Shell
# ####################################################################
Param (	[string]$MonitorCommand = (Read-Host "Command To Monitor"),
		$RefreshSeconds = (Read-Host "Refresh (Seconds)"),
		$StopMonitoringHours = (Read-Host "Hours To Stop Monitoring")
		)

Function DisplayMonitorHeaderInfo
	{
		$HeaderBorderChar = "="
		$HeaderBorder = $HeaderBorderChar * 139		#PowerShell Console Size: 140 Wide x 35 High
		
		Write-Host $HeaderBorder -ForegroundColor Yellow
		Write-Host "Monitoring Started: " $MonitoringStart "`tMonitoring Will Stop: " $StopDateTime "`tIt Is Now: " $Today -ForegroundColor Yellow
		Write-Host "Monitoring Command: " $MonitorCommand -ForegroundColor Yellow
		Write-Host "Refresh Interval (Seconds): " $RefreshSeconds -ForegroundColor Yellow
		Write-Host $HeaderBorder -ForegroundColor Yellow
	}
		
If ($MonitorCommand -eq "")
	{Write-Host "No Command Indicated."}
If ($RefreshSeconds -eq "")
	{$RefreshSeconds = 5}
If ($StopMonitoringHours -eq "")
	{$StopMonitoringHours = 1}
Else
	{
		# Indicated Command Exists,
		# Start Monitoring the Command
		
		$Today = Get-Date
		$MonitoringStart = $Today
		$StopDateTime = $Today.AddHours($StopMonitoringHours)
		
		Clear-Host
		DisplayMonitorHeaderInfo
		
		while ($true)
		{
			# Endless Loop Will Execute Command to Monitor, Sleep,
			# and Repeat; User Must CONTROL+C to Stop Monitoring and/or
			# Specify Date/Time to Stop Monitoring
			
			Clear-Host
			DisplayMonitorHeaderInfo
			
			Invoke-Expression $MonitorCommand
			Start-Sleep $RefreshSeconds
			
			$Today = Get-Date
			If ($Today -gt $StopDateTime)
				{Break}
		}
	}

Open in new window

Comment
Watch Question

My suggestion :

The simplest and most straight forward way is to repeat printing of your header lines every n rows.
Commented:
After rereading your question, I have some more ideals for you.

I would suggest displaying your data in a windows form. That way you can set the header info and allow the data to be displayed in a scrollable component.

If you need help on creating a form for powershell I suggest Primal Forms http://www.primaltools.com/downloads/communitytools/