Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

I would like to see a more modern console (terminal) for PowerShel

3 views
Skip to first unread message

John Ericson

unread,
Jul 3, 2006, 9:21:02 AM7/3/06
to
I did a comprehensive blogging on this subject at the address:
http://www.john.se/blog/2006/07/01/searching-for-a-good-replacement-for-the-old-console-in-windows/

Where I mention the shortcomings of today's console in Windows and what I
expect from a modern console. For easy reading I'll quote the blog in its
entirety below, but for the hypertext version with links and formatting I
recommend the link above.

"Searching for a good replacement for the old console in Windows.

My early impressions of PowerShell.
Lately I’ve been playing around with Microsoft’s PowerShell, which I’ve seen
referred to as PoSH on some sites. While the shell looks like the next-gen
technology when it comes to shell and scripting, I’m really dismayed that
Microsoft hasn’t improved the archaic cmd.exe terminal (In Windows
terminology referred to as a “console”). In the windows world we have been
using the same terminal like since Windows NT4?

Great shell. Poor terminal.
The thing that bugs me the most with cmd.exe is that you can’t freely resize
it horizontally. It also looks out of place on Windows XP OS’s because of
some to me unknown reason it still uses the Window manager frame from Windows
2000 (Or what the hell! Probably even NT4). At least the frame is fixed in
Vista!

What I expect from a terminal today.
Perhaps I’m sounding picky in the ears of long-time Windows admins, but when
you have used terminals for a long time on Unix and Linux systems then going
back to cmd.exe really feels like time-travelling backwards. A good example
from the Linux world is gnome-terminal; fully resizable, good possibilities
for configuring the font, tabs, easy copy and paste.

Problems with PowerShell and rxvt and xterm for Windows.
I could use the shell in cmd.exe with the terminals rxvt or xterm from
cygwin (a Linux-like environment for Windows) to make for a nice terminal
experience. Unfortunately PowerShell doesn’t work with either rxvt or xterm
:-( , the prompt never appears and I have to close the terminal.

A better one in the future?
However, even MS employees admits that the current terminal it somewhat
outdated. Don’t get me wrong, I’m sure that PowerShell will make even the
most hardcore Unix admin jealous. I’m just finding it sad that their first
impression of PowerShell won’t be as great because of the inferior terminal.
Hopefully a better one will show up in the future!"

Others opinions, recommendation of alternative consoles, or feedback on this
issue is highly appreciated!

----------------
This post is a suggestion for Microsoft, and Microsoft responds to the
suggestions with the most votes. To vote for this suggestion, click the "I
Agree" button in the message pane. If you do not see the button, follow this
link to open the suggestion in the Microsoft Web-based Newsreader and then
click "I Agree" in the message pane.

http://www.microsoft.com/communities/newsgroups/list/en-us/default.aspx?mid=149f9d47-a0a3-43da-a334-5627879e32cc&dg=microsoft.public.windows.powershell

Jeroen Vandezande

unread,
Jul 3, 2006, 10:12:21 AM7/3/06
to
What I would like the most is command history...
In Unix when you log on again with a user and open a bash shell you can
press arrow up and have your last used command from last time... even if the
pc has been turned off...
I hate it when I close a command prompt and open a new one that I lost my
history....

Best Regards,

Jeroen Vandezande

"John Ericson" <JohnE...@discussions.microsoft.com> wrote in message
news:149F9D47-A0A3-43DA...@microsoft.com...

Sean McLeod

unread,
Jul 3, 2006, 10:20:52 AM7/3/06
to
See the following blog posting for a way to achieve command history across
sessions.

http://blogs.msdn.com/powershell/archive/2006/07/01/653194.aspx

Cheers

"Jeroen Vandezande" <NoS...@NoSpam.be> wrote in message
news:eyXEyqqn...@TK2MSFTNGP05.phx.gbl...

Jeffrey Snover [MSFT]

unread,
Jul 3, 2006, 10:39:10 AM7/3/06
to
> Perhaps I'm sounding picky in the ears of long-time Windows admins, but
> when
> you have used terminals for a long time on Unix and Linux systems then
> going
> back to cmd.exe really feels like time-travelling backwards.

You are not being picky. What you are asking for is perfectly reasonable.
We wanted to fix this for V1 but didn't have the bandwidth. Instead, we
exposed the interfaces necessary for a 3rd party to be able to deliver this
(Search for PowerShell Analyzer to see an example of what I mean).

Thanks for filing the feature request. Other people should vote on this so
that we know where to put it in our priorities.
--
Jeffrey Snover [MSFT]
Windows PowerShell Architect
Microsoft Corporation
This posting is provided "AS IS" with no warranties, no confers rights.
Visit the Windows PowerShell Team blog at:
http://blogs.msdn.com/PowerShell
Visit the Windows PowerShell ScriptCenter at:
http://www.microsoft.com/technet/scriptcenter/hubs/msh.mspx


Greg Borota

unread,
Jul 3, 2006, 11:02:40 AM7/3/06
to
There has to be a solution to have the command-history saved when exiting
the console.

I was thinking of doing my first cmdlet exercise by building a cmdlet that
lets you hook a function to the console close event. If I am succesful and
there is interest, I'll post the solution here.

Greg

"Sean McLeod" <seanm...@newsgroups.nospam> wrote in message
news:%23XpUivq...@TK2MSFTNGP03.phx.gbl...

Sean McLeod

unread,
Jul 3, 2006, 12:44:58 PM7/3/06
to
Yep, although I guess if there was a fairly easy way to catch the exit then
Jeffrey would've used it.

Cheers

"Greg Borota" <bor...@gmail.com> wrote in message
news:%23gN2$GrnGH...@TK2MSFTNGP03.phx.gbl...

Greg Borota

unread,
Jul 3, 2006, 1:07:03 PM7/3/06
to
You are most probably right.
I can capture the close event, but I am not sure you can run scriptblocks by
this time (I get missing runspace exception, etc.).

Greg

"Sean McLeod" <seanm...@newsgroups.nospam> wrote in message

news:eTfGEAsn...@TK2MSFTNGP05.phx.gbl...

Andrew Watt [MVP]

unread,
Jul 3, 2006, 4:21:57 PM7/3/06
to
On Mon, 3 Jul 2006 16:12:21 +0200, "Jeroen Vandezande"
<NoS...@NoSpam.be> wrote:

>What I would like the most is command history...
>In Unix when you log on again with a user and open a bash shell you can
>press arrow up and have your last used command from last time... even if the
>pc has been turned off...
>I hate it when I close a command prompt and open a new one that I lost my
>history....
>
>Best Regards,
>
>Jeroen Vandezande

Jeroen,

Are you assuming that a user will have only a single PowerShell window
open?

If multiple windows have been opened how would you want/expect a
command history (or histories) to work?

Andrew Watt MVP

Andrew Watt [MVP]

unread,
Jul 3, 2006, 4:30:31 PM7/3/06
to
On Mon, 3 Jul 2006 06:21:02 -0700, John Ericson
<JohnE...@discussions.microsoft.com> wrote:

>What I expect from a terminal today.
>Perhaps I’m sounding picky in the ears of long-time Windows admins, but when
>you have used terminals for a long time on Unix and Linux systems then going
>back to cmd.exe really feels like time-travelling backwards. A good example
>from the Linux world is gnome-terminal; fully resizable, good possibilities
>for configuring the font, tabs, easy copy and paste.

Easy copy and paste would get a vote from me.

John, why not post specific suggestions individually on Connect so we
can vote on the aspects that we feel are important?

Andrew Watt MVP

John Ericson

unread,
Jul 3, 2006, 7:19:01 PM7/3/06
to
"Andrew Watt [MVP]" wrote:

> John, why not post specific suggestions individually on Connect so we
> can vote on the aspects that we feel are important?

That's a good idea! I'll post some individual suggestions that I can think
of using "Console: [suggestion]" as subject lines, like "Console: Easy
horizontal resizing", "Console: Make it easier selecting text" and more.

Offtopic: Out of curiosity! What does MVP stand for?

John Ericson

unread,
Jul 3, 2006, 7:42:01 PM7/3/06
to
"Andrew Watt [MVP]" wrote:

> Are you assuming that a user will have only a single PowerShell window
> open?

My reply is not intended to be a reply for Jeroen Vandezande, but instead
for a guy coming from the Unix world.

History can work fine even if you have several consoles (PowerShell windows)
open at the same time.

> If multiple windows have been opened how would you want/expect a
> command history (or histories) to work?

I suggest bash as a good example of how history could work with multiply
windows. You can check it out by installing cygwin on a Windows box. bash
saves the history in the ~/.bash_history file on exit and loads it on start.
If you open two bash windows then both those windows will have the history,
but all the new commands inputted will only be available in the local history
in each window. When any window is closed its history is saved to
.bash_history and newly created bash windows will read it on startup.

So bash reads the history and then stores it locally and writes it back to
the .bash_history file on exit. No intercommunication of history exists
between bash sessions, the sessions are local.

Keith Hill [MVP]

unread,
Jul 3, 2006, 10:56:02 PM7/3/06
to
"John Ericson" <JohnE...@discussions.microsoft.com> wrote in message
news:9D98DBBA-5401-49F8...@microsoft.com...

> I suggest bash as a good example of how history could work with multiply
> windows. You can check it out by installing cygwin on a Windows box. bash
> saves the history in the ~/.bash_history file on exit and loads it on
> start.
> If you open two bash windows then both those windows will have the
> history,
> but all the new commands inputted will only be available in the local
> history
> in each window. When any window is closed its history is saved to
> .bash_history and newly created bash windows will read it on startup.
>
> So bash reads the history and then stores it locally and writes it back to
> the .bash_history file on exit. No intercommunication of history exists
> between bash sessions, the sessions are local.

In that case, doesn't each shell exit stomp the history of the shells that
exited before it? That is, if I have three shells running then the last one
exited is that one that gets its history persisted.

--
Keith

Keith Hill [MVP]

unread,
Jul 3, 2006, 10:58:26 PM7/3/06
to
"John Ericson" <JohnE...@discussions.microsoft.com> wrote in message
news:3F5AC907-D0C5-4BCD...@microsoft.com...

>
> Offtopic: Out of curiosity! What does MVP stand for?

Most Valuable Professional. It is awarded to folks who participate a lot in
a particular Microsoft community especially in the area of helping others by
answering questions on MS technologies.

--
Keith

Greg Borota

unread,
Jul 4, 2006, 2:13:56 AM7/4/06
to
I think I learnt some about PowerShell by trying to run scriptblocks from
inside the console signal handler (called when closing the console). As long
as that code is running in a different thread, it is simply not going to
happen no matter what. Under those circumstances, it must be pretty hard to
call a PoSH function to save history when user closes the console window.
The thing that was unexpected was to realize that TRAP can catch exceptions
thrown from inside that handler.

Greg

"Sean McLeod" <seanm...@newsgroups.nospam> wrote in message

news:eTfGEAsn...@TK2MSFTNGP05.phx.gbl...


> Yep, although I guess if there was a fairly easy way to catch the exit
> then Jeffrey would've used it.
>
> Cheers
>

> "Greg Borota" wrote in message

John Ericson

unread,
Jul 4, 2006, 3:20:01 AM7/4/06
to
"Keith Hill [MVP]" wrote:

Ah, right! I just tested what you said with two cygwin bash shells and no,
the second shell doesn't stomp the history of the first one. Although the
cygwin bash shells must be closed with CTRL+D or the commands "exit" or
"logout", this could be a Windows thing I'm not sure. My test step by step:

1) Start two shells (two windows).

2) In the first shell I input "command1" and in the second "command2".

3) I close the first shell and then the second.

4) When I open a new shell and scroll back in the history by pressing the up
arrow key, I first get "command2" and then "command1".

Greg Borota

unread,
Jul 4, 2006, 3:24:02 AM7/4/06
to
I meant to say TRAP CAN'T catch exceptions thrown from console signal
handler. If exception are thrown from a different thread they won't reach
your TRAP, it seems.

"Greg Borota" <bor...@newsgroups.microsoft> wrote in message
news:%23ExroGz...@TK2MSFTNGP02.phx.gbl...

Michael Bednarek

unread,
Jul 4, 2006, 5:27:37 AM7/4/06
to
On Mon, 3 Jul 2006 06:21:02 -0700, John Ericson
<JohnE...@discussions.microsoft.com> wrote in
microsoft.public.windows.server.scripting,
microsoft.public.windows.powershell:

[snip]

4NT (JPSoft) has provided a number of features that are lacking in PoSH,
like command and directory history between sessions, for a long time.

As for appearance, JPSoft has a public beta for a new product, Tabbed
Console Interface (TCI) which can be used to run any console application
(<http://www.jpsoft.com/download.htm>). I have no need for Tabbed
Consoles and have no experience with TCI, but many experienced console
users like it a lot.

(I have no interest in JPSoft, except that I couldn't do my job without
4NT.)

--
Michael Bednarek http://mbednarek.com/ "POST NO BILLS"

Andrew Watt [MVP]

unread,
Jul 4, 2006, 7:06:44 AM7/4/06
to
On Mon, 3 Jul 2006 16:19:01 -0700, John Ericson
<JohnE...@discussions.microsoft.com> wrote:

>"Andrew Watt [MVP]" wrote:
>
>> John, why not post specific suggestions individually on Connect so we
>> can vote on the aspects that we feel are important?
>
>That's a good idea! I'll post some individual suggestions that I can think
>of using "Console: [suggestion]" as subject lines, like "Console: Easy
>horizontal resizing", "Console: Make it easier selecting text" and more.

Ideally, give the link for each suggestion. Inertia means that doing
things that way will likely attract more votes! Human nature likes
inertia. :)

>Offtopic: Out of curiosity! What does MVP stand for?

MVP = Most Valuable Professional.

See https://mvp.support.microsoft.com/

Andrew Watt MVP

Andrew Watt [MVP]

unread,
Jul 4, 2006, 7:10:53 AM7/4/06
to
On Tue, 4 Jul 2006 00:20:01 -0700, John Ericson
<JohnE...@discussions.microsoft.com> wrote:

>Ah, right! I just tested what you said with two cygwin bash shells and no,
>the second shell doesn't stomp the history of the first one. Although the
>cygwin bash shells must be closed with CTRL+D or the commands "exit" or
>"logout", this could be a Windows thing I'm not sure. My test step by step:
>
>1) Start two shells (two windows).
>
>2) In the first shell I input "command1" and in the second "command2".
>
>3) I close the first shell and then the second.
>
>4) When I open a new shell and scroll back in the history by pressing the up
>arrow key, I first get "command2" and then "command1".

Thanks. That's interesting.

Andrew Watt MVP

dreeschkind

unread,
Jul 4, 2006, 8:10:02 PM7/4/06
to
It should be possible to implement that in PowerShell as well.
PowerShell's history objects of each console instance just need to be
serialized and written (appended) to a global history file on exit - just
like bash does it.
After you have closed both shells and start up a new one, the combined
history of both old shells could be restored from the global history file
again.

I think there should be a possibility to write a custom shell exit handler
just like we can do with the custom prompt function.

--
greetings
dreeschkind

Greg Borota

unread,
Jul 5, 2006, 11:34:55 AM7/5/06
to
I thought that too, but I am afraid that's very hard without access to
original sources.

My understanding is that console signal handler (invoked when closing the
console) is invoked on a different thread, and that causes problems. Also
currently there doesn't seem to be any exit hook available. It's kind of
surprising but that's the way things seem to be.

You could have a convoluted hack, where a dialog pops up when the user
closes the console window asking to save history, but I personally don't
like that. Would people like something like that? That should work.

Greg

P.S. If you want I could post the code I wrote to capture console closing

"dreeschkind" <drees...@discussions.microsoft.com> wrote in message
news:65F8BA7A-9510-4285...@microsoft.com...

Greg Borota

unread,
Jul 5, 2006, 11:48:30 AM7/5/06
to
In case you want to play with this thing, this code might help. It
implements a signal handler for the console (old days - signals, etc.).
Currently it doesn't let you close the console - could be a good reminder to
exit via "bye", not by closing the console, etc.

Let me know if you can come up with a solution. I personally gave up, I
don't think is quite possible. That's way I implemented the prompt solution.
I think this is a good example where PS current limitations come to bite
you.

****************************************************

$framework =
[System.Runtime.InteropServices.RuntimeEnvironment]::GetRuntimeDirectory()

# credits to Jeffrey Snover
function Compile-Csharp ([string] $code, [Array]$References) {

# Get an instance of the CSharp code provider
$cp = New-Object Microsoft.CSharp.CSharpCodeProvider

$refs = New-Object Collections.ArrayList
$refs.AddRange( @("${framework}System.dll",
"${PsHome}\System.Management.Automation.dll",
"${PsHome}\Microsoft.PowerShell.ConsoleHost.dll",
"${framework}System.Windows.Forms.dll",
"${framework}System.Data.dll",
"${framework}System.Drawing.dll",
"${framework}System.XML.dll"))
if ($References.Count -ge 1) {
$refs.AddRange($References)
}

# Build up a compiler params object...
$cpar = New-Object System.CodeDom.Compiler.CompilerParameters
$cpar.GenerateInMemory = $true
$cpar.GenerateExecutable = $false
$cpar.IncludeDebugInformation = $false
$cpar.CompilerOptions = "/target:library"
$cpar.ReferencedAssemblies.AddRange($refs)
$cr = $cp.CompileAssemblyFromSource($cpar, $code)

if ( $cr.Errors.Count) {
$codeLines = $code.Split("`n");
foreach ($ce in $cr.Errors) {
write-host "Error: $($codeLines[$($ce.Line - 1)])"
$ce | out-default
}
Throw "INVALID DATA: Errors encountered while compiling code"
}
}


$cmd_code = '
using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Management.Automation;

public class ConsoleHook
{
private static ScriptBlock scriptBlock = null;
public delegate Boolean HandlerRoutine(CtrlTypes CtrlType);
private static HandlerRoutine hr = new HandlerRoutine(consoleHandler);
[DllImport("Kernel32")]
private static extern Boolean SetConsoleCtrlHandler(HandlerRoutine
Handler, Boolean Add);

public enum CtrlTypes {
CTRL_C_EVENT = 0,
CTRL_BREAK_EVENT = 1,
CTRL_CLOSE_EVENT = 2,
CTRL_LOGOFF_EVENT = 5,
CTRL_SHUTDOWN_EVENT = 6
}

private static Boolean consoleHandler(ConsoleHook.CtrlTypes CtrlType) {
// Do something with scriptBlock, EventHandlers, throwing exception,
whatever if you can!
return true;
}

public static void SetExitHook(ScriptBlock code) {
scriptBlock = code;
SetConsoleCtrlHandler(hr, true);
}
}'

Compile-Csharp $cmd_code
[ConsoleHook]::SetExitHook({Get-History -Count 100 | Export-CSV
~\PowerShell\history.csv})


"dreeschkind" <drees...@discussions.microsoft.com> wrote in message
news:65F8BA7A-9510-4285...@microsoft.com...

Jonathan Eric Miller

unread,
Jul 5, 2006, 3:48:33 PM7/5/06
to
I agree with what you have said. I'd also like to see command-completion in
addition to directory/file name completion. i.e. it should search the path
and find executable names that match. Also, the directory/filename
completion shouldn't wipe out text that appears to the right of where the
cursor is positioned. i.e. if I hit up arrow to recall the following
previously entered command "copy c:\abc d:\def", I should be able to
position the cursor at abc and delete it and then enter g and hit tab and
have it expand to c:\ghi without wiping out the d:\def at the end. I can do
this kind of thing in bash on Linux.

Also, it would be nice if more.com worked like less does on Linux. i.e. you
can use the arrow keys to move up or down one line at a time. This goes for
all the PowerShell help commands too. The way it works now, it prompts you
and when you hit a key, the prompt is still there in the text. So, if you
are hitting enter to see one line at a time, you get the prompt for each
line. When you press a key it should erase the previous prompt.

Jon

"John Ericson" <JohnE...@discussions.microsoft.com> wrote in message
news:149F9D47-A0A3-43DA...@microsoft.com...

Jon Forrest

unread,
Jul 5, 2006, 4:53:08 PM7/5/06
to Jonathan Eric Miller
I might have been the first to raise this issue
3 or 4 months ago (in microsoft.public.windows.server.scripting).

There is a potentially large group of us who have no current
interest in PS for interactive use because of all the missing
features critical for interactive use that are present in
virtually all Unix shells. Not being unreasonable people,
we do understand how software is developed, so we're
waiting patiently for PS V2 to see if we're being heard.

Many people, including yours truly, have put forth fine
examples of the missing features so I won't waste
time or space by doing so again here.


--
Jon Forrest
for...@ce.berkeley.edu
Computer Resources Manager
Civil and Environmental Engineering Dept.
305 Davis Hall
Univ. of Calif., Berkeley
Berkeley, CA 94720-1710
510-642-0904

Jon Miller

unread,
Jul 6, 2006, 1:29:49 PM7/6/06
to
It might be nice to have intellisense for returned objects too. Not sure if
that's possible or not, I'm just learning the syntax of PS now...

"Jonathan Eric Miller" <jemi...@uchicago.edu> wrote in message
news:OG%23hKwGo...@TK2MSFTNGP03.phx.gbl...

Jon Miller

unread,
Jul 6, 2006, 1:39:22 PM7/6/06
to
There should also be an easy way to search within a help topic like you can
do with UNIX less. Better paging support in general...

"Jonathan Eric Miller" <jemi...@uchicago.edu> wrote in message
news:OG%23hKwGo...@TK2MSFTNGP03.phx.gbl...

Jon Miller

unread,
Jul 6, 2006, 2:41:12 PM7/6/06
to
I just noticed that it does have intellisense for object properties. I'll
shut up now...

"Jon Miller" <jemi...@uchicago.edu> wrote in message
news:OQaBVHSo...@TK2MSFTNGP05.phx.gbl...

/\/\o\/\/

unread,
Jul 6, 2006, 3:44:10 PM7/6/06
to
Jon Miller wrote:
> I just noticed that it does have intellisense for object properties. I'll
> shut up now...
>

Tab-completion that is ;-)
but yes it works on variables, and you can also customize it

I have an example here that does add some more tabhandling (multilevel
on variables, .NET types amongs others)

http://mow001.blogspot.com/2006/06/powershell-tab-completion-part-4.html

Greetings /\/\o\/\/

dreeschkind

unread,
Jul 7, 2006, 1:04:01 AM7/7/06
to
Thanks for your code. I can understand what you are trying to do but
unfortunately I'm quite new to C# so I think I can't help you alot. I'm not
sure, but maybe this discussion can help you:

http://www.microsoft.com/communities/newsgroups/list/en-us/default.aspx?dg=microsoft.public.windows.server.scripting&tid=11c42853-c8ba-4863-979f-21156aeae5fd&cat=en_us_bc3ff1ec-9c4a-407c-94fd-3fa4c3871b71&lang=en&cr=us&sloc=en-us&m=1&p=1

It seems to be possible to have a PsProvider run some code automatically
when the Runspace (console?) is closing. Maybe this is a solution.

However, I think you should post your basic approach again because the real
experts here might have missed it deep in this thread.

--
greetings
dreeschkind

Greg Borota

unread,
Jul 7, 2006, 11:58:38 AM7/7/06
to
From what I understand, the high guys there are basically just confirming
that there is no mechanism to hook in code at exit (and no intent for v1).
The "solution" provided to create a CmdletProvider has a too pungent hacking
smell to me. Plus you have to leave the scripting world and carry binary
ddl - snapins around, etc. For my own needs the prompt solution is perfect
for the time being.
Thanks for the link, it was useful,
Greg


"dreeschkind" <drees...@discussions.microsoft.com> wrote in message

news:7BA022F6-0B28-47B0...@microsoft.com...

dreeschkind

unread,
Jul 7, 2006, 5:14:02 PM7/7/06
to
"Greg Borota" wrote:

> The "solution" provided to create a CmdletProvider has a too pungent hacking
> smell to me.

Well, the usage of a console signal handler and inline C# code also smelt a
little like hacking to me... :^)

The reason why I don't like to have so much code in my prompt function is
that I noticed it becoming slower and slower the more stuff I put into it.
Besides, this also doesn't smell right because it is not the main task of the
prompt function to save your history etc.

I filed the idea of a custom shell exit handler as a suggestion on connect
so please vote if you are still interested:

https://connect.microsoft.com/feedback/ViewFeedback.aspx?FeedbackID=158962&SiteID=99

--
greetings
dreeschkind

Greg Borota

unread,
Jul 7, 2006, 6:30:46 PM7/7/06
to
That is hacking, I agree. The solution should be coming from the source,
agreed.
I can never get to voting anything, I am always kicked out. Do you have to
have a hotmail account to get there?

"dreeschkind" <drees...@discussions.microsoft.com> wrote in message

news:9F1777CB-D18E-46FD...@microsoft.com...

Greg Borota

unread,
Jul 8, 2006, 10:21:10 AM7/8/06
to
Dreesckind, this code might be getting us somewhere. Does it work on your
computer? On mine it does.
The current issues to tackle is that by the time we are getting into
Runspace.Closing event, the current Runspace is no longer available, so I
have to create a new clean one to run the code in. So no history, no current
directory, etc. preserved into the new one. Maybe some ideas here?
There would be a mixed solution now with the help of our friend the PROMPT,
using the prompt to save data into memory not on disc and saving it on disc
only when closing the runspace. But I am trying to find a more generic
solution where I could still get scriptblock to execute into the current
runspace (or a copy of it). Anyway here is the code, you may want to give it
a try (needs the Jeffrey Snover function to do CodeDOM). Thanks.

$hookCode = '
using System;
using System.Runtime.InteropServices;
using System.Management.Automation;
using System.Management.Automation.Runspaces;

public class ConsoleHook
{
private static string script = null;
private static Runspace runspace = null;
public delegate Boolean HandlerRoutine(CtrlTypes ctrlType);


private static HandlerRoutine hr = new HandlerRoutine(consoleHandler);
[DllImport("Kernel32")]
private static extern Boolean SetConsoleCtrlHandler(HandlerRoutine
Handler, Boolean Add);

public enum CtrlTypes {
CTRL_C_EVENT = 0,
CTRL_BREAK_EVENT = 1,
CTRL_CLOSE_EVENT = 2,
CTRL_LOGOFF_EVENT = 5,
CTRL_SHUTDOWN_EVENT = 6
}

private static Boolean consoleHandler(CtrlTypes ctrlType) {
if (ctrlType != CtrlTypes.CTRL_C_EVENT) {
try {
Runspace.DefaultRunspace.Close();
} catch (Exception exc) {
Console.WriteLine(exc);
}
}
return false;
}

private static void HandleEvent(object sender, RunspaceStateEventArgs e) {
if (e.RunspaceStateInfo.State == RunspaceState.Closing) {
try {
RunspaceInvoke ri = new
RunspaceInvoke(runspace.RunspaceConfiguration);
ri.Invoke(script);
} catch (Exception exc) {
Console.WriteLine(exc);
}
}
}

public static void SetExitHook(string block) {
try {
script = block;
runspace = Runspace.DefaultRunspace;
runspace.StateChanged += new
EventHandler<RunspaceStateEventArgs>(HandleEvent);
SetConsoleCtrlHandler(hr, true);
} catch (Exception exc) {
Console.WriteLine(exc);
}
}
}'

Compile-Csharp $hookCode
[ConsoleHook]::SetExitHook("(Get-Location).Path | Out-File psTEST.txt")

"dreeschkind" <drees...@discussions.microsoft.com> wrote in message

news:9F1777CB-D18E-46FD...@microsoft.com...

Jon Miller

unread,
Jul 10, 2006, 5:43:57 PM7/10/06
to
Another annoyance that I just noticed about the terminal window is that if
you run some other command within the terminal window, for example, ssh.exe
(from OpenSSH), then, the right-click context menu doesn't work anymore. So,
you can't use it for copy/paste operations. However, the menu at the top
does allow you to do it...

Greg Borota

unread,
Jul 11, 2006, 12:42:23 AM7/11/06
to
I would guess that has to do more with how your default mode of operation is
for ssh.exe (quick edit mode). On my system I never see any right-click menu
because I changed it to be more like Unix terminals where you
SELECT->Enter -> Copy to Clipboard and right-click pastes from clipboard. I
suspect your ssh.exe is configured to work like that.

(By the way there is a way in V1 to have command history saved, custom code
run, etc. when you close the console window, I'll post it one of these days,
probably tomorrow)

Greg

"Jon Miller" <> wrote in message
news:OarNY0Gp...@TK2MSFTNGP03.phx.gbl...

Alex K. Angelopoulos [MVP]

unread,
Jul 11, 2006, 9:52:31 AM7/11/06
to

"Greg Borota" <bor...@newsgroups.microsoft> wrote in message
news:eNC1qRKp...@TK2MSFTNGP04.phx.gbl...

> (By the way there is a way in V1 to have command history saved, custom
> code run, etc. when you close the console window, I'll post it one of
> these days, probably tomorrow)

Greg, you'd better not be having a duel at dawn tomorrow. ;)


Jon Miller

unread,
Jul 11, 2006, 3:19:21 PM7/11/06
to
ssh.exe is just a console mode application that runs within PowerShell or
cmd.exe. If I check the settings under the menu at the top, they are the
same as they are if I'm at the DOS prompt. Another thing that I noticed is
that the mouse wheel doesn't work for scrolling.

Jon

"Greg Borota" <bor...@newsgroups.microsoft> wrote in message
news:eNC1qRKp...@TK2MSFTNGP04.phx.gbl...

0 new messages