How to unset the readonly option on a psvariable

by oising 18. April 2007 01:07

I had an interesting problem today, I wanted to temporarily set a powershell variable to "readonly." So, I tried the obvious, and expected it to fail (it did):

PS E:\projects\powershell> $p = 7                                                                                       
PS E:\projects\powershell> (gi variable:p).Options = "readonly"                                                         
PS E:\projects\powershell> (gi variable:p).Options = "none"                                                             
Exception setting "Options": "Cannot overwrite variable p because it is read-only or constant."                         
At line:1 char:17                                                                                                       
+ (gi variable:p).O <<<< ptions = "none"                                                                                

Of course it seems pretty obvious in hindsight that this would fail, but why do they provide both "const" AND "readonly" choices for variables? well, after some spelunking with the excellent Reflector (my first choice these days, I find it easier than navigating around the msdn behemoth), I found the answer:

internal void SetOptions(ScopedItemOptions newOptions, bool force)
{
    using (IDisposable disposable = tracer.TraceMethod(newOptions))
    {
        if (this.IsConstant || (!force && this.IsReadOnly))
        {
            SessionStateUnauthorizedAccessException exceptionRecord = new SessionStateUnauthorizedAccessException(this.name, SessionStateCategory.Variable, "VariableNotWritable");
            tracer.TraceException(exceptionRecord);
            throw exceptionRecord;
        }

E.g, if the variable is not a constant, you can force the change:

PS E:\projects\powershell> set-item variable:p $p -force                                                                
PS E:\projects\powershell> (gi variable:p).Options                                                                      
None                                                                                                                    

This resets the "readonly" bit.

Tags:

.NET | Monad | PowerShell

Add comment



  Country flag
biuquote
  • Comment
  • Preview
Loading


About the author

Oisin Grehan lives in Montreal, Canada and builds all sorts of crap for all sorts of people.

Month List

Page List