{"id":10961,"date":"2006-04-25T12:17:43","date_gmt":"2006-04-25T12:17:43","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/powershell\/2006\/04\/25\/mshobject-and-methods-that-take-object\/"},"modified":"2019-02-18T13:25:08","modified_gmt":"2019-02-18T20:25:08","slug":"mshobject-and-methods-that-take-object","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/powershell\/mshobject-and-methods-that-take-object\/","title":{"rendered":"MshObject and Methods that take object"},"content":{"rendered":"<p class=\"MsoNormal\"><font face=\"Arial\" size=\"2\"><span>Every once in a while people ( including me) run into trouble calling methods that take object. &nbsp;Why?&nbsp; Well, as I told you earlier objects in MSH are wrapped by an invisible MshObject. &nbsp;As it turns out that this invisible object is not always so invisible. &nbsp;Take a look at the following:<\/span><\/font><\/p>\n<p class=\"MsoNormal\"><font face=\"Arial\" size=\"2\"><span>&nbsp;<\/span><\/font><\/p>\n<p class=\"MsoNormal\"><font face=\"Courier\" size=\"1\"><span>MSH&gt;$key = get-item .<\/span><\/font><\/p>\n<p class=\"MsoNormal\"><font face=\"Courier\" size=\"1\"><span>MSH&gt;$hash = @{}<\/span><\/font><\/p>\n<p class=\"MsoNormal\"><font face=\"Courier\" size=\"1\"><span>MSH&gt;$hash[$key] = &#8216;foo&#8217;<\/span><\/font><\/p>\n<p class=\"MsoNormal\"><font face=\"Courier\" size=\"1\"><span>MSH&gt;$hash[$key]<\/span><\/font><\/p>\n<p class=\"MsoNormal\"><font face=\"Courier\" size=\"1\"><span>foo<\/span><\/font><\/p>\n<p class=\"MsoNormal\"><font face=\"Courier\" size=\"1\"><span>MSH&gt;$hash.ContainsKey($key)<\/span><\/font><\/p>\n<p class=\"MsoNormal\"><font face=\"Courier\" size=\"1\"><span>False<\/span><\/font><\/p>\n<p class=\"MsoNormal\"><font face=\"Courier\" size=\"1\"><span>MSH&gt;<\/span><\/font><\/p>\n<p class=\"MsoNormal\"><font face=\"Arial\" size=\"2\"><span>&nbsp;<\/span><\/font><\/p>\n<p class=\"MsoNormal\"><font face=\"Arial\" size=\"2\"><span>Weird huh?&nbsp; On the one hand you can use the key to access the object.&nbsp; On the other hand it tells you that the hashtable doesn\u2019t really have that key. &nbsp;What\u2019s going on?&nbsp; Well, if you take a look at the method ContainsKey&nbsp; (Here\u2019s a neat trick on how to do it):<\/span><\/font><\/p>\n<p class=\"MsoNormal\"><font face=\"Arial\" size=\"2\"><span>&nbsp;<\/span><\/font><\/p>\n<p class=\"MsoNormal\"><font face=\"Courier\" size=\"1\"><span>MSH&gt;$hash.ContainsKey<\/span><\/font><\/p>\n<p class=\"MsoNormal\"><font face=\"Courier\" size=\"1\"><span>&nbsp;<\/span><\/font><\/p>\n<p class=\"MsoNormal\"><font face=\"Courier\" size=\"1\"><span>&nbsp;<\/span><\/font><\/p>\n<p class=\"MsoNormal\"><font face=\"Courier\" size=\"1\"><span>MemberType&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : Method<\/span><\/font><\/p>\n<p class=\"MsoNormal\"><font face=\"Courier\" size=\"1\"><span>OverloadDefinitions : {System.Boolean ContainsKey(Object key)}<\/span><\/font><\/p>\n<p class=\"MsoNormal\"><font face=\"Courier\" size=\"1\"><span>TypeOfValue&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : System.Management.Automation.MshMethod<\/span><\/font><\/p>\n<p class=\"MsoNormal\"><font face=\"Courier\" size=\"1\"><span>Value&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;: System.Boolean ContainsKey(Object key)<\/span><\/font><\/p>\n<p class=\"MsoNormal\"><font face=\"Courier\" size=\"1\"><span>Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : ContainsKey<\/span><\/font><\/p>\n<p class=\"MsoNormal\"><font face=\"Courier\" size=\"1\"><span>IsInstance&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : True<\/span><\/font><\/p>\n<p class=\"MsoNormal\"><font face=\"Arial\" size=\"2\"><span>&nbsp;<\/span><\/font><\/p>\n<p class=\"MsoNormal\"><font face=\"Arial\" size=\"2\"><span>&nbsp;<\/span><\/font><\/p>\n<p class=\"MsoNormal\"><font face=\"Arial\" size=\"2\"><span>You can see that the method takes object.&nbsp; Because it takes object MSH doesn\u2019t know whether you actually meant to pass the method the MshObject or the object contained by the MshObject. &nbsp;So it goes ahead and makes the decision of passing the MshObject. &nbsp;However, that wasn\u2019t the key used earlier.&nbsp; The key that was used was the actual BaseObject. &nbsp;What to do? What to do?&nbsp; Well, if you ever run into this situation here are two things you can do. &nbsp;The first is to use the MshObject property I showed you earlier and access the BaseObject property:<\/span><\/font><\/p>\n<p class=\"MsoNormal\"><font face=\"Arial\" size=\"2\"><span>&nbsp;<\/span><\/font><\/p>\n<p class=\"MsoNormal\"><font face=\"Courier\" size=\"1\"><span>MSH&gt;$hash.ContainsKey($key.MshObject.BaseObject)<\/span><\/font><\/p>\n<p class=\"MsoNormal\"><font face=\"Courier\" size=\"1\"><span>True<\/span><\/font><\/p>\n<p class=\"MsoNormal\"><font face=\"Arial\" size=\"2\"><span>&nbsp;<\/span><\/font><\/p>\n<p class=\"MsoNormal\"><font face=\"Arial\" size=\"2\"><span>The second is to cast the object to the actual type that you want.<\/span><\/font><\/p>\n<p class=\"MsoNormal\"><font face=\"Arial\" size=\"2\"><span>&nbsp;<\/span><\/font><\/p>\n<p class=\"MsoNormal\"><font face=\"Courier\" size=\"1\"><span>MSH&gt;$hash.ContainsKey([IO.DirectoryInfo]$key)<\/span><\/font><\/p>\n<p class=\"MsoNormal\"><font face=\"Courier\" size=\"1\"><span>True<\/span><\/font><\/p>\n<p class=\"MsoNormal\"><font face=\"Arial\" size=\"2\"><span>&nbsp;<\/span><\/font><\/p>\n<p class=\"MsoNormal\"><font face=\"Arial\" size=\"2\"><span>&nbsp;<\/span><\/font><\/p>\n<p class=\"MsoNormal\"><font face=\"Arial\" size=\"2\"><span>Try to keep this in mind when working with methods that take object.<\/span><\/font><\/p>\n<p class=\"MsoNormal\"><font face=\"Arial\" size=\"2\"><span>&nbsp;<\/span><\/font><\/p>\n<p class=\"MsoNormal\"><font face=\"Arial\" size=\"2\"><span>&#8211; Marcel<\/span><\/font><\/p>\n<p>[<i>Edit: Monad has now been renamed to Windows PowerShell.  This script or discussion may require slight adjustments before it applies directly to newer builds.<\/i>]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Every once in a while people ( including me) run into trouble calling methods that take object. &nbsp;Why?&nbsp; Well, as I told you earlier objects in MSH are wrapped by an invisible MshObject. &nbsp;As it turns out that this invisible object is not always so invisible. &nbsp;Take a look at the following: &nbsp; MSH&gt;$key = [&hellip;]<\/p>\n","protected":false},"author":600,"featured_media":13641,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-10961","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell"],"acf":[],"blog_post_summary":"<p>Every once in a while people ( including me) run into trouble calling methods that take object. &nbsp;Why?&nbsp; Well, as I told you earlier objects in MSH are wrapped by an invisible MshObject. &nbsp;As it turns out that this invisible object is not always so invisible. &nbsp;Take a look at the following: &nbsp; MSH&gt;$key = [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/10961","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/users\/600"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/comments?post=10961"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/10961\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media\/13641"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media?parent=10961"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/categories?post=10961"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/tags?post=10961"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}