[PS] Script failed due to call depth overflow - Image1.jpg (0/1)

Discussion in 'Scripting' started by Andrew Watt [MVP], May 11, 2006.

  1. I received the error message in the post's title when exploring the
    $MaximumHistoryCount issue.

    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{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
     
    Andrew Watt [MVP], May 11, 2006
    #1
    1. Advertisements

  2. The original post related to a Windows XP SP2 machine.

    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
     
    Andrew Watt [MVP], May 11, 2006
    #2
    1. Advertisements

  3. I think you meant to call the function clear-history didn't you? You're
    calling clear-host from within a function with the same name, so it's
    inifinite recursion being terminated when you go too deep. cls is just an
    alias for clear-host, so it does the same thing.
     
    Michael McMullen, May 11, 2006
    #3
  4. Andrew Watt [MVP]

    applepwc Guest

    Last time,I want creat a set-location function with set-location cmdlet in it.
    It doesn't work as you seen.
    ###
    The script failed due to call depth overflow. The call depth reached
    101 and the maximum is 100.
    ###

    I questioned it,and Jouko answered me:

    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
     
    applepwc, May 11, 2006
    #4
  5. Thanks. You're right. It's *so* easy to see what you expect to be
    there (clear-history) rather than what was there (clear-host).

    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
     
    Andrew Watt [MVP], May 11, 2006
    #5
  6. Andrew Watt [MVP]

    applepwc Guest

    I'm sorry for my last post :
    "So I suggest you useMicrosoft.PowerShell.Management\clear-host in your
    function
    "is actually wrong!

    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
     
    applepwc, May 12, 2006
    #6
  7. One way to control which command gets executed is to use the result returned
    from get-command along with the & operator. For example, say I define a
    function "dir".

    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.
     
    Bruce Payette [MSFT], May 12, 2006
    #7
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.