mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-18 02:13:00 +08:00
Add new PowerShell project
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
using System.Management.Automation;
|
||||
|
||||
namespace MQTTnet.PowerShell.Cmdlets;
|
||||
|
||||
[Cmdlet(VerbsCommunications.Connect, "MqttSession")]
|
||||
[OutputType(typeof(MqttClientConnectResult))]
|
||||
public class ConnectMqttSessionCmdlet : PSCmdlet
|
||||
{
|
||||
[Parameter]
|
||||
public SwitchParameter CleanSession { get; set; } = true;
|
||||
|
||||
[Parameter]
|
||||
public string? ClientId { get; set; } = Guid.NewGuid().ToString();
|
||||
|
||||
[Parameter(Mandatory = true)]
|
||||
public required new string Host { get; set; }
|
||||
|
||||
[Parameter]
|
||||
public string? Password { get; set; }
|
||||
|
||||
[Parameter]
|
||||
public int Port { get; set; } = 1883;
|
||||
|
||||
[Parameter(Mandatory = true, ValueFromPipeline = true)]
|
||||
public required MqttSession Session { get; set; }
|
||||
|
||||
[Parameter]
|
||||
public string? Username { get; set; }
|
||||
|
||||
[Parameter]
|
||||
public SwitchParameter UseTls { get; set; }
|
||||
|
||||
protected override void ProcessRecord()
|
||||
{
|
||||
var clientOptionsBuilder = new MqttClientOptionsBuilder();
|
||||
clientOptionsBuilder.WithTcpServer(Host, Port);
|
||||
|
||||
if (Username != null)
|
||||
{
|
||||
clientOptionsBuilder.WithCredentials(Username, Password);
|
||||
}
|
||||
|
||||
var response = Session.Client.ConnectAsync(clientOptionsBuilder.Build()).GetAwaiter().GetResult();
|
||||
|
||||
WriteObject(response);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user