Over the last few years I’ve been using Powershell for carrying out some remote administration tasks and I’ve loved it. I furthered my own knowledge and use of it when certain tools started to support it’s use and tools like App-V became re-developed, build on top of Powershell, it’s an integral part of several products now. Well, recently I started developing a tool using C# and Powershell (Which I hope to make available on this site in future).
I kept hitting this error:
Exception calling “InvokeMethod” with “4” argument(s): “Exception calling “InvokeMember” with “5” argument(s): “Type mismatch. (Exception from HRESULT: 0x80020005 (DISP_E_TYPEMISMATCH))””
Which has been covered in a few great blog posts like this one:
https://p0w3rsh3ll.wordpress.com/2012/01/10/working-with-the-windowsinstaller-installer-object/
Every post I found, although it had worked great for others did not work for me. I kind of got a little ahead of myself, I coded over 300 lines of Powershell before testing it altogether…I assumed since I had one example which worked, it would work for the next 300 lines. It did not, I got the error and every fix I found online, would not work for me.
What was unique for me, was the fact in my code, I had a lot of IF statements which were conditionally either doing an INSERT to create a new row in my database, it did not already exist or an UPDATE if it already existed. What I found on closer inspection was that my IF statements were a bit messed up and my code was actually trying to INSERT rows which already existed which caused my error. By troubleshooting, I fixed the conditions for my IF statements and got it to work just fine. So, if you are getting this error and you are using SQL, it’s possible your code is attempting to INSERT over an existing record.