Quick Hits: Writing to a Read-Only Property

综合技术 2016-06-28

This post is less on doing something useful and more on just proving that I can do something even though PowerShell is trying to tell me that I cannot do it. That little thing is updating a property on an object that is set as Read-Only.

My example to show you how you can do something like this is by looking at $PSVersionTable which as we all know gives us some great information about what version of PowerShell that we are using.


I am also using this as this was an example used in our previous user group meeting in which we were shown that the properties here could not be updated.

From this object, I am going to look more at the PSVersion property which of course is an object within this object.


Looking at the properties of this object, we can see that each of the properties are shown as Read-Only by only having the Get method available to use.

$PSVersionTable.PSVersion | Get-Member

Furthermore, I am greeted with a nice error stating that my attempts to modify this have been foolish and that I should just give up.

$PSVersionTable.PSVersion.Major = 10

Yea, I guess I could just throw in the towel and move onto something more useful like updating some bugs on a module, but I think that I can do better here! And by using some reflection magic, I can make this happen!

To do this, I need to look at the hidden fields for the PSVersion object.


Here we can see that the field that we need to look at updating is _Major. Now we need to figure out how we can set this.

$Field = $PSVersionTable.PSVersion.GetType().GetField('_Major','static,nonpublic,instance')
$Field | Get-Member

Looking at the methods, I see a SetValue that accepts a object and a value for the object. This seems pretty simple,right? Just call the method and update the value.


Perfect! We have now updated this “Read-Only” property to give us PowerShell V10! Now this will go away once we close and re-open PowerShell but it still a fun thing to do to show off how you can update these kinds of values.

责编内容by:Learn Powershell (源链)。感谢您的支持!


Windows Payload免杀方法实验 前言 最近的在学习内网渗透过程中,遇到的一个问题就是如何静静的在目标机器执行Payload。网上姿势很多,最先想到的是PowerSploit,后来看到余弦的猥琐流打法影响深刻,于是动手实践,有些能被360监测到,过程如下: 一、Payload msfvenom -p windows/me...
Dew Drop – November 6, 2017 (#2597) Top Links Running Blazor on Mono in the browser (Steve Sanderson) TWC9: 29 New HoloLens Markets, Behind Surface Pro LTE, Lots of Azure, C...
Find Local Xamarin and .NET Developer Events This ... Spring into action this April by finding a Xamarin or .NET user group hosting Xamarin (and related technology) events near you! Get the latest news o...
Working with “Big Data” in .NET For simplicity (and because I didn’t know any better at the time), Fiddler uses plain public byte[] array fields to represent the request and respons...
Hands-free Security Scanning in .NET Applications This was originally posted on my blog . I'm sure if you follow any news sources at all, you see countless hacks, breaches, and leaks from various ...