Here is the full transcript of the MSH session (except for commands 1
and 2):
<begin transcript>
PS C:\Documents and Settings\Andrew Watt> get-history
Id CommandLine
-- -----------
3 $MaximumHistoryCount = 1 ; clear-host
PS C:\Documents and Settings\Andrew Watt> function clear-host{
>> $MaximumHistoryCount = 1
>> clear-host
>> }
>>
PS C:\Documents and Settings\Andrew Watt> clear-host
The script failed due to call depth overflow. The call depth reached
101 and the maximum is 100.
PS C:\Documents and Settings\Andrew Watt>
<end transcript>
I have also attached a screen shot of a repro. On that occasion the
only previous command issued after opening a new PowerShell console
was $MaximumHistoryCount (result was 64).
Does anyone understand what is going on here?
Thanks
Andrew Watt MVP
I have since tried it on a Windows Server 2003 machine. Same result.
Also, on both machines after getting this error message the PowerShell
console responds to a cls or clear-host with the same message. I can
no longer clear the console's display!
Andrew Watt MVP
I questioned it,and Jouko answered me:
> How can I reference the cmdlet "set-location" in my script ?
Prefix the cmdlet name with name of the snap-in:
Microsoft.PowerShell.Management\Set-Location
You can determine the snap-in name with:
(Get-Command Set-Location).PSSnapIn.Name
####
So I suggest you use
Microsoft.PowerShell.Management\clear-host
in your function.
--
greeting
applepwc
In any case the function would have needed to be
function clear-history{
$global:MaximumHistoryCount = 1
clear-host
}
to work as I intended.
Andrew Watt MVP
I made a big mistake:clear-host is function,not a cmdlet.
So I suggest another method to solve your problem:
1,rename original clear-host function to clear-host2.
Rename-Item function:\clear-host clear-host2
2,invoke clear-host2 in your new clear-host function.
--
greeting
applepwc
PS (1) > function dir { "Foobar" }
Now I call get-command
PS (2) > get-command dir
CommandType Name Definition
----------- ---- ----------
Function dir "Foobar"
Alias dir Get-ChildItem
As you can see the token "dir" maps to two possible commands. If I just type
"dir", then I'll get the alias because aliases are resolved before
functions.
PS (3) > dir C:\CONFIG.SYS
Directory: Microsoft.PowerShell.Core\FileSystem::C:\
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 11/17/2004 3:32 AM 0 CONFIG.SYS
So how do I get the function? Well, the CommandInfo object for the function
is the first one returned from get-command command:
PS (4) > (get-command dir)[0]
CommandType Name Definition
----------- ---- ----------
Function dir "Foobar"
Now I call that function using &:
PS (5) > &(get-command dir)[0]
Foobar
This works with any type of command. Let's try it with sc:
PS (6) > get-command sc
CommandType Name Definition
----------- ---- ----------
Alias sc Set-Content
Application sc.exe C:\WINDOWS\system32\...
ExternalScript sc.ps1 C:\Documents and Set...
There are three commands that map to the token "sc". We want the one at
index 1: the exe:
PS (7) > (get-command sc)[1]
CommandType Name Definition
----------- ---- ----------
Application sc.exe C:\WINDOWS\system32\...
Now invoke that command, querying for the plugplay service...
PS (8) > &(get-command sc)[1] query plugplay
SERVICE_NAME: plugplay
TYPE : 20 WIN32_SHARE_PROCESS
STATE : 4 RUNNING
(NOT_STOPPABLE,NOT_PAUSABLE,ACCEP
TS_SHUTDOWN)
WIN32_EXIT_CODE : 0 (0x0)
SERVICE_EXIT_CODE : 0 (0x0)
CHECKPOINT : 0x0
WAIT_HINT : 0x0
PS (9) >
Since CommandInfo objects are just objects, we can store them into a
variable to use later:
PS (10) > &$sc query plugplay
SERVICE_NAME: plugplay
TYPE : 20 WIN32_SHARE_PROCESS
STATE : 4 RUNNING
(NOT_STOPPABLE,NOT_PAUSABLE,ACCEP
TS_SHUTDOWN)
WIN32_EXIT_CODE : 0 (0x0)
SERVICE_EXIT_CODE : 0 (0x0)
CHECKPOINT : 0x0
WAIT_HINT : 0x0
When invoked in this way, no lookup is done, the command is directly
executed. If you're a UNIX shell user, this roughly corresponds to the idea
of a "tracked alias" except that we don't currently allow you to bind a
CommandInfo object to an alias (yet). Next release perhaps :-)
-bruce
--
Bruce Payette [MSFT]
Windows PowerShell Technical Lead
Microsoft Corporation
This posting is provided "AS IS" with no warranties, and confers no rights.
"applepwc" <appl...@msn.com> wrote in message
news:8906342F-84AE-4E6F...@microsoft.com...