PowerShell and M2MQTT - powershell

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)

Related

how do I remove-Printer that contains wildcard characters through Powershell?

I am attempting to remove printers with the Remove-printer command, but keep running into a WildcardPatternException error. The printer name contains [].
This is the error that I am getting when running the command.
Exception calling "FilterByProperty" with "4" argument(s): "The specified wildcard character pattern is not valid:
[Follow_Me_Colour_Duplex [ServerName](Mobility)]"
At line:1153 char:9
+ $__cmdletization_queryBuilder.FilterByProperty('Name', $__cmd ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : WildcardPatternException
Despite the server name being in the printer name, it is a local printer.
Is there a way to ignore the wildcard characters in this string? Or is there another way to get the location / name without the characters being in there?
Thanks in advance.

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)

Cannot call overloaded methods of .NET API in 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?

Getting error while exporting azure db using powershell

Followed Export SQL database example using link :
https://learn.microsoft.com/en-us/azure/sql-database/sql-database-export-powershell#export-sql-database-example
Getting below error:
New-AzureRmSqlDatabaseExport : ResourceNotFound: The Resource 'Microsoft.Sql/servers/XXX.database.windows.net/databases/[DBNAME]' under resource group 'Default-SQL-SoutheastAsia' was
not found.
At [FilePath]\sample.ps1:24 char:18
+ $exportRequest = New-AzureRmSqlDatabaseExport –ResourceGroupName $ResourceGroupN ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : CloseError: (:) [New-AzureRmSqlDatabaseExport], CloudException
+ FullyQualifiedErrorId : Microsoft.Azure.Commands.Sql.ImportExport.Cmdlet.NewAzureSqlDatabaseExport
Get-AzureRmSqlDatabaseImportExportStatus : Cannot bind argument to parameter 'OperationStatusLink' because it is null.
At [FilePath]\sample.ps1:30 char:63
+ Get-AzureRmSqlDatabaseImportExportStatus -OperationStatusLink $exportRequest.Ope ...
+ CategoryInfo : InvalidData: (:) [Get-AzureRmSqlDatabaseImportExportStatus], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.Azure.Commands.Sql.ImportExport.Cmdlet.GetAzureSqlDatabaseImportExportStatus
Any help?
I had this same issue today, actually. I think if you change your server name to not use the fully-qualified name, that might do it. So, just use "xxxxx."
At least that's what worked for me.
You need to verify that your Automation account has latest modules imported and are up to date (AzureRM.Automation – AzureRM.Profile – AzureRM.Sql) atleast to Version: 2.5.0. If modules show a different version, i.e. 1.0.3 then:
navigate to assets in the automation account, select modules and click on
Update Azure Modules
.
Wait for the modules to get updated it normally takes a few minutes

"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