System.Management.Automation.PSCredential & The Dispose Method

Background

Over the last few days, I ended up stealing so much code and I could not know what is what.

Again, trying to solve a bigger problem made my stealing a lot more scattered.

Understanding

Yet, as the dusk settles, one likely has to go back and understand and may be see what to keep and what to let go off.

System.Management.Automation.PSCredential

One of the things I wanted to do is house keeping.

That is, look through each function, identify the objects instantiated through the new-object invocation and give the resource back to the system through calling the Dispose Method.

System.Management.Automation.PSCredential

new-object

Code


# Instantiate new object System.Management.Automation.PSCredential
# from username and password
$credential = new-object System.Management.Automation.PSCredential ($username, $password);

Method – Dispose

Code


#If credential is instantiated, please dispose of it
if ($credential)
{
    $credential.Dispose();
}

Output

Output – Image

methodDisposeMissing.201900928.0350AM

Output – Text


Method invocation failed because [System.Management.Automation.PSCredential] does not contain a method named 'Dispose'.

Explanation

No dispose method.

Get Members ( gm )

Code


#If credential is instantiated, get member
if ($credential)
{
    $credential | gm;
}

Output

Output – Image

methodGetMember.01.201900928.0357AM

Output – Text


TypeName: System.Management.Automation.PSCredential

Name                 MemberType Definition
----                 ---------- ----------
Equals               Method     bool Equals(System.Object obj)
GetHashCode          Method     int GetHashCode()
GetNetworkCredential Method     System.Net.NetworkCredential GetNetworkCredential()
GetObjectData        Method     void GetObjectData(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context), void...
GetType              Method     type GetType()
ToString             Method     string ToString()
Password             Property   securestring Password {get;}
UserName             Property   string UserName {get;}

Explanation

Reviewed the list of members ( Methods and Properties ) and it is obvious that Nah, dispose is not included.

Summary

I have a lot more reading to do along the lines of releasing objects whose class does not expose the Dispose object.

Unfortunately, Powershell is a dynamic language and as one does not run the code through a compilation process, some things do not jump out, till when one runs the code.

Or when one goes back, and reviews his or her code.

 

Listening

Listening to D. Nice :-

D-Nice – Call Me D-Nice
Link

 

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s