1

Powershell V3+:
I'm writing a module providing various Cmdlets suiteable for interactive use. What I try to achive is, that variables (objects) are getting destructed without having the user to use remove-variable.

Example:

$object = open-object -someparam
$object # returns something
close-object -reference $object
$object # is not defined anymore - similiar to remove-variable

I could not manage to get the above working, alternative approaches like the below have also failed.

$x = New-Object -TypeName PSCustomObject
$x | Add-Member -type NoteProperty -Name Justsomedata -Value Nomeaning
$x | Add-Member -type ScriptMethod -Name killme -Value { $this = $null }
$x
$x.killme()
$x # :-(

Any suggestions? I want to prevent that there are variables in the users runspace that are not useable anymore.

4
  • Under normal circumstances, aren't the objects more-or-less properly de-allocated when the variable goes out of scope? Commented Aug 4, 2017 at 12:36
  • They would, yes, the problem is if you are interactively working with the cmdlets they won't go out of scope until the session gets closed. Commented Aug 4, 2017 at 12:41
  • 2
    Interactively, I don't believe this can be done, and I'm not sure I can conceive of a situation where I'd want it to work, rather than simply throwing an error if I used a variable that has a no-longer-valid value. Commented Aug 4, 2017 at 12:45
  • It would be "partially" possible if I would use a hashtable. It would always be passed by reference and I could delete all keys so in the end the object would be empty. Problem is, that I'm working with objects that come out of a DLL assembly so I do not have full control over it. However using ETS might introduce a "validity" flag, that might be a workaround if noone has a solution for this. Commented Aug 4, 2017 at 12:54

1 Answer 1

1

It would be better to change the state of the object in a way that makes it clear it should not longer be used. For instance, you could have a property named something like IsValid or IsOpen and then you could throw an exception if they tried to use it. It's better to avoid actually removing variables for the user in case they want to reference the object even if it is no longer valid.

But, if you really want to do it, you could try this:

$references = Get-Variable | Where-Object { $this -eq $_.Value}
$references | ForEach-Object { Remove-Variable $_.Name }
Sign up to request clarification or add additional context in comments.

1 Comment

Okay got it. Remove-Variable did the job when I added -scope global. I totally understand your point - in this case references to these "dead" objects would cause more issues than missing references. The idea with the property however is worth to consider, I'll think about it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.