Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在powershell中创建只读成员?_Powershell_Cmdlets - Fatal编程技术网

如何在powershell中创建只读成员?

如何在powershell中创建只读成员?,powershell,cmdlets,Powershell,Cmdlets,在Powershell中使用Add Membercmdlet时,如何使成员为只读 基本上,我想将成员添加到具有只读属性的系统.Diagnostic.Process。如下所示: $p = new-object System.Diagnostics.Process $p | Add-member -Name thisisreadonly -membertype scriptproperty -value { 6} $p.thisisreadonly #gives 6 $p.thisisrea

在Powershell中使用
Add Member
cmdlet时,如何使成员为只读

基本上,我想将成员添加到具有只读属性的
系统.Diagnostic.Process

如下所示:

 $p = new-object System.Diagnostics.Process
 $p | Add-member -Name thisisreadonly -membertype scriptproperty -value { 6}
 $p.thisisreadonly #gives 6
 $p.thisisreadonly = 5 #error- Set accessor for property "thisisreadonly" is unavailable.

因此,基本上您创建了一个ScriptProperty,其中有一个getter,但没有setter。

值得一提的是,add member的-secondvalue参数用于提供“setter”。同样值得一提的是
-MemberType
ScriptProperty
而不是
-ScriptMethod
,一次疏忽,在几分钟内至少丢失了一个略读答案。