oop - Is it possible to use anything similar to $this to refer to the current object in Powershell? -


i'm attempting make powershell module work external api. have custom object looks this:

$subscriber = new-module -ascustomobject -scriptblock {     [string] $emailaddress=$null     [string] $name=$null     [bool]   $resubscribe=$false     [bool]   $restartsubscriptionbasedautoresponders=$false      export-modulemember -variable * -function * } 

now, i'd object able implement function outputs in json format. i'm aware normal way is: $subscriber | convertto-json instead, i'd object implement along these lines:

$subscriber = new-module -ascustomobject -scriptblock {     [string] $emailaddress=$null     [string] $name=$null     [bool]   $resubscribe=$false     [bool]   $restartsubscriptionbasedautoresponders=$false      function tojson {         $this | convertto-json     }      export-modulemember -variable * -function * } 

but problem there no $this variable nor have been able find information other way it. attempting misuse powershell such degree isn't supported @ all? or missing obvious? i'm open critique , advice :)

the variables define @ module scope (eg. $emailaddress) can accessed1 inside functions defined in module.

however convertto-json converts single object pipeline.

therefore better defining single object, , passing through:

$subscriber = new-module -ascustomobject -scriptblock {     $data = new-object psobject -property @{               emailaddress=$null;               name=$null;               resubscribe=$false;               restartsubscriptionbasedautoresponders=$false             }      function tojson {         $data | convertto-json     }      export-modulemember -variable * -function * } 

but wouldn't fix -ascustomobject, conversion single object done inside tojson:

$subscriber = new-module -ascustomobject -scriptblock {     [string] $emailaddress=$null     [string] $name=$null     [bool]   $resubscribe=$false     [bool]   $restartsubscriptionbasedautoresponders=$false      function tojson {         new-object psobject -property @{               emailaddress=$emailaddress;               name=$name;               resubscribe=$resubscribe;               restartsubscriptionbasedautoresponders=$restartsubscriptionbasedautoresponders             } | convertto-json     }      export-modulemember -variable * -function * } 

(and, far aware, there no single $this in case: there isn't .psm1 type modules.)


1 , updated if use script: scope prefix.


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -