Windows Vista Forums

Cannot overwrite variable PID

  1. #1


    =?Utf-8?B?RG91Zw==?= Guest

    Cannot overwrite variable PID

    Here is a snippet. I hit this accidently.

    function test ($PID) { $o = "" | select PID; $o.PID = $PID; $o }

    PS C:\> test "hello"
    Cannot overwrite variable PID because it is read-only or constant.
    At line:1 char:1
    + t <<<< est "hello"

    I tried a get-member on the variable (dir variableid | gm) to see if I
    could figure out why.

    How can I tell/get a list of read-only/constant variables?



      My System SpecsSystem Spec

  2.   


  3. #2


    =?Utf-8?B?ZHJlZXNjaGtpbmQ=?= Guest

    RE: Cannot overwrite variable PID

    The problem is that the name of your function parameter $PID is already used
    by PowerShell as an automatic variable containing the current process ID. I
    ran into the same problem when trying to name my function parameter "$host"
    some days ago.

    To get all variables and their options run this:
    PS> dir variable: | select name, options, description | format-table -auto

    To get only those variables that are constant run this:
    PS> dir variable: | where {$_.options -like "*constant*"}

    To get your function working change the name of your parameter:
    function test ($myPID) { $o = "" | select PID; $o.PID = $myPID; $o }

    --
    greetings
    dreeschkind

    "Doug" wrote:

    > Here is a snippet. I hit this accidently.
    >
    > function test ($PID) { $o = "" | select PID; $o.PID = $PID; $o }
    >
    > PS C:\> test "hello"
    > Cannot overwrite variable PID because it is read-only or constant.
    > At line:1 char:1
    > + t <<<< est "hello"
    >
    > I tried a get-member on the variable (dir variableid | gm) to see if I
    > could figure out why.
    >
    > How can I tell/get a list of read-only/constant variables?
    >
    >


      My System SpecsSystem Spec

  4. #3


    Adam Milazzo Guest

    Re: Cannot overwrite variable PID

    "Doug" <Doug@discussions.microsoft.com> wrote in message
    news:6C673D5D-62F5-4CC3-A7E6-E60C3D20E394@microsoft.com...
    > Here is a snippet. I hit this accidently.
    >
    > function test ($PID) { $o = "" | select PID; $o.PID = $PID; $o }
    >
    > PS C:\> test "hello"
    > Cannot overwrite variable PID because it is read-only or constant.
    > At line:1 char:1
    > + t <<<< est "hello"

    Actually, I would consider this a bug... or a poor design decision.

    It also repros with: function test($PID) { }

    I'd think that parameter names would be scoped locally to the function and
    shadow global variables with the same name. Then if you needed you could
    reference the global function with the scope specifier -- $global:PID

    > How can I tell/get a list of read-only/constant variables?

    I would use:

    gv | ? { $_.Options -match 'Constant|ReadOnly' }



      My System SpecsSystem Spec

  5. #4


    Adam Milazzo Guest

    Re: Cannot overwrite variable PID

    "Adam Milazzo" <adamm@san.rr.com> wrote in message
    news:OEHRWM22GHA.4484@TK2MSFTNGP02.phx.gbl...
    > "Doug" <Doug@discussions.microsoft.com> wrote in message
    > news:6C673D5D-62F5-4CC3-A7E6-E60C3D20E394@microsoft.com...
    >> Here is a snippet. I hit this accidently.
    >>
    >> function test ($PID) { $o = "" | select PID; $o.PID = $PID; $o }
    >>
    >> PS C:\> test "hello"
    >> Cannot overwrite variable PID because it is read-only or constant.
    >> At line:1 char:1
    >> + t <<<< est "hello"

    > Actually, I would consider this a bug... or a poor design decision.
    >
    > It also repros with: function test($PID) { }
    >
    > I'd think that parameter names would be scoped locally to the function and
    > shadow global variables with the same name. Then if you needed you could
    > reference the global function with the scope specifier -- $global:PID


    After further investigation, this seems to be caused not by the fact that
    it's readonly or constant, but by the fact that the $PID variable has the
    AllScope option. Any variable with the AllScope option can't be used as a
    parameter name.

    I still think this AllScope thing is not such a good idea...



      My System SpecsSystem Spec

  6. #5


    Jeffrey Snover Guest

    Re: Cannot overwrite variable PID

    This is the list of our Constant AllScope variables:
    Name Options
    ---- -------
    Host Constant, AllScope
    ExecutionContext Constant, AllScope
    true Constant, AllScope
    ShellId Constant, AllScope
    false Constant, AllScope
    PSHOME Constant, AllScope
    PID Constant, AllScope

    jps

      My System SpecsSystem Spec

  7. #6


    =?Utf-8?B?ZHJlZXNjaGtpbmQ=?= Guest

    Re: Cannot overwrite variable PID

    I think it would make sense if some of those 'special' constants got the
    prefix 'PS' like for instance $PSHOME.
    This would reduce the probability to run into the problem that you can't
    name your function parameters 'PID' or 'Host', because it is unlikely that
    somebody names his parameters with that prefix.

    This is how it should look like:

    Name Options
    ---- -------
    PSHost Constant, AllScope
    PSExecutionContext Constant, AllScope
    true Constant, AllScope
    PSShellId Constant, AllScope
    false Constant, AllScope
    PSHOME Constant, AllScope
    PSPID Constant, AllScope


    The constants true and false should not have this prefix of course.
    Btw: Why is $null not Constant, AllScope?

    --
    greetings
    dreeschkind

    "Jeffrey Snover" wrote:

    > This is the list of our Constant AllScope variables:
    > Name Options
    > ---- -------
    > Host Constant, AllScope
    > ExecutionContext Constant, AllScope
    > true Constant, AllScope
    > ShellId Constant, AllScope
    > false Constant, AllScope
    > PSHOME Constant, AllScope
    > PID Constant, AllScope
    >
    > jps


      My System SpecsSystem Spec

  8. #7


    XShell Guest

    Re: Cannot overwrite variable PID

    >I think it would make sense if some of those 'special' constants got the
    > prefix 'PS' like for instance $PSHOME.


    I second your suggestion and if you submit this on connect I would
    definitely vote on this to get it in RC2.

    When the provider,snap-in and drive can get renamed to PSSnapIn , PSProvider
    and PSDrive then there should be room to rename these variables too before
    RTM.




      My System SpecsSystem Spec

  9. #8


    Adam Milazzo Guest

    Re: Cannot overwrite variable PID

    "dreeschkind" <dreeschkind@discussions.microsoft.com> wrote in message
    news:364481A6-D58A-40CD-8D38-83BE57509701@microsoft.com...
    >I think it would make sense if some of those 'special' constants got the
    > prefix 'PS' like for instance $PSHOME.


    Except that $PSHOME is rather confusing, then. It seems like it would be the
    PS installation path rather than the user's home directory.



      My System SpecsSystem Spec

  10. #9


    Adam Milazzo Guest

    Re: Cannot overwrite variable PID

    Jeffrey Snover wrote:
    > This is the list of our Constant AllScope variables:
    > Name Options
    > ---- -------
    > Host Constant, AllScope
    > ExecutionContext Constant, AllScope
    > true Constant, AllScope
    > ShellId Constant, AllScope
    > false Constant, AllScope
    > PSHOME Constant, AllScope
    > PID Constant, AllScope


    I'll note that no AllScope variables can be used as parameter names (not
    just the constant/readonly ones).

    PS> new-variable -Option AllScope -Name foo
    PS> function test($foo) { $foo }
    PS> test 5
    The AllScope option cannot be removed from the variable 'foo'.
    At line:1 char:1
    + t <<<< est 5

      My System SpecsSystem Spec


Cannot overwrite variable PID
Similar Threads
Thread Forum
cannot overwrite since upgrading to SP1 Vista file management
How to overwrite files Vista General
Copy/Overwrite? Vista General
How can I ensure that a variable is a built-in powershell variable? PowerShell
File overwrite Vista General