• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

Cannot overwrite variable PID

  • Thread starter =?Utf-8?B?RG91Zw==?=
  • Start date
?

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

#1
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 variable:pid | gm) to see if I
could figure out why.

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

My Computer

?

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

#2
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 variable:pid | gm) to see if I
> could figure out why.
>
> How can I tell/get a list of read-only/constant variables?
>
>
 

My Computer

A

Adam Milazzo

#3
"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 Computer

A

Adam Milazzo

#4
"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 Computer

J

Jeffrey Snover

#5
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 Computer

?

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

#6
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 Computer

X

XShell

#7
>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 Computer

A

Adam Milazzo

#8
"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 Computer

A

Adam Milazzo

#9
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 Computer

Users Who Are Viewing This Thread (Users: 1, Guests: 0)