Cannot call overloaded methods of .NET API in PowerShell - powershell

I have a 3rd party API that I want to use in a PowerShell script. I can instantiate the objects and access properties and non-overloaded methods, but every call to an overloaded method fails with Cannot find an overload for "<method>" and the argument count: "<count>".
The API works fine when called from C#.
Example (here $doc0 contains an instance of an object from the API and Value is the method I want to call):
PS C:\> $doc0.Value.OverloadDefinitions
System.Object IPSFNetDataItem.Value(int fieldIndex)
System.Object IPSFNetDataItem.Value(string field)
PS C:\> $doc0.FieldName(0) #Non-overloaded methods are ok.
ID
PS C:\> $doc0.Value([int]0) #overloaded methods fail
Cannot find an overload for "value" and the argument count: "1".
At line:1 char:1
+ $doc0.Value([int]0) #overloaded methods fail
+ ~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodException
+ FullyQualifiedErrorId : MethodCountCouldNotFindBest
PS C:\> $doc0.Value([string]"why?") #overloaded methods fail
Cannot find an overload for "value" and the argument count: "1".
At line:1 char:1
+ $doc0.Value([string]"why?") #overloaded methods fail
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodException
+ FullyQualifiedErrorId : MethodCountCouldNotFindBest
I have looked at other similar questions (e.g. here and here) but these solutions do not work in this case - there is surely no room for ambiguity in this very simple case and as the output from OverloadDefinitions shows, I am not trying to do anything that is not supported but the API.
I assume that PowerShell isn't usually this bad at resolving method calls; any ideas why this might be failing?

Related

PowerShell and M2MQTT

I'm trying to create a simple MQTT client in PowerShell to receive some messages from a broker. This broker is using a non standard port.
To do that, I followed the instructions I found at https://jackgruber.github.io/2019-06-05-ps-mqtt/
When using the example's code it's working fine but when I'm trying to specify a port like
$MqttClient = [uPLibrary.Networking.M2Mqtt.MqttClient]("mqtt-broker.net",41383)
it throws the error:
Cannot convert the "System.Object[]" value of type "System.Object[]" to type "uPLibrary.Networking.M2Mqtt.MqttClient".
At line:1 char:1
+ $MqttClient = [uPLibrary.Networking.M2Mqtt.MqttClient]("ext.mqtt.drya ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [], RuntimeException
+ FullyQualifiedErrorId : ConvertToFinalInvalidCastException
Any ideas?
I suspect you'd need to construct the object, not just cast it.
Try:
[upLibrary.Networking.m2mqtt.mqttclient]::new("mqtt-broker.net",41383)

In Powershell: ToLower() works, in VSC/Powershell not?

I have win10 Pro and Powershell 5.1
On the other hand VSC(1.54.1) with the powershell extention (ms-vscode-powershell, v2021.2.2).
The command
$day = $day.ToLower()
on the Powershell prompt works fine
But in VSC it says:
+ $dayName = $dayName.ToLower()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
MethodNotFound???
Any help?
Thanks
$dayName.ToString().ToLower() should do what you expect here (according to your comment).
.ToLower() is a string method and you are trying to use it on System.DayOfWeek, which doesn't have that method.
In order to keep track of your variables and just what they are, running $myVariable.GetType().FullName can be very handy - I use it all the time.
In your example, running
$day.GetType().FullName
$dayName.GetType().FullName
would probably result in something like
PS C:\> $day.GetType().FullName
System.String
PS C:\> $dayName.GetType().FullName
System.DayOfWeek

Powershell draw visio container

I need to use powershell to draw visio, but I dont know how powershell can draw containers.
This is the article
https://powershellstation.com/2016/04/18/powershell-and-visio-part-6-containers/
that I have referenced and followed, but it seems $master has not been declared somewhere so it is failing.
I wonder if anyone has any instructions for this problem.
This is my code
$Visio=New-Object -ComObject Visio.Application  
$doc=$Visio.Documents.Add(‘’)  
$Page=$Visio.ActivePage  
$stencilPath=$Visio.GetBuiltInStencilFile(2,0)
$stencil=$Visio.Documents.OpenEx($stencilPath,64)
$page=$Visio.ActivePage
$container=$page.Drop($master,5,5)
$rec=$page.DrawRectangle(2,3,5,6)
$container.ContainerProperties.AddMember($rec,1) 
This is the error
You cannot call a method on a null-valued expression.
At line:7 char:1
+ $container.ContainerProperties.AddMember($rec,1)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
I appreciate everyone's help.
This is the code to draw container
$stencilPath=$viso.GetBuiltInStencilFile(2,0)
$stencil=$viso.Documents.OpenEx($stencilPath,64)
$Master=$stencil.Masters('Classic')
$Page.Drop($Master,11)

Unable to do type verification on a runspace

I am using Pester to do some unit testing on a module I am creating and I'm having a bit of trouble with one of my functions that is supposed to return a runspace object. When I create a runspace object and do a .GetType() I see the name listed as LocalRunspace with a fullname of System.Management.Automation.Runspaces.LocalRunspace however when I run$Runspace -is [LocalRunspace] or $Runspace -is [System.Management.Automation.Runspaces.LocalRunspace] I get the error message below
Unable to find type [System.Management.Automation.Runspaces.LocalRunspace]. Make sure that the assembly that contains this type is loaded.
At line:1 char:1
+ $ps.Runspace -is [System.Management.Automation.Runspaces.LocalRunspace]
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Manageme...s.LocalRunspace:TypeName) [], RuntimeException
+ FullyQualifiedErrorId : TypeNotFound
Just trying to figure out what type I should be referencing in order to create the proper validation check. I know I could fall back on doing comparison to the results of the .GetType() but I'd really rather use the normal Pester convention of Should BeOfType

"Method Not found" error when calling GetElementsbyClassName

I created a PowerShell automation script and I gave it to my friend when he run it, it said
Method invocation failed because [mshtml.HTMLBodyClass] does not contain a method
named 'getElementsByClassName'.
At C:\Users\עמית\Documents\asaf.ps1:22 char:3
+ $a=$docs.body.getElementsByClassName("FadeOut-Scroll")
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
why is that happened?
We both have IE11, .net 4.5, Visual studio, but I have that function and he not.
And it looks like here in his computer the IE console have the function:
but PowerShell does not:
How to update PowerShell?
Found after alot of time:
the missing want microsoft core xml