MQTT C#

Basically all you need can be found here. Tested and it worked with mosquitto

https://github.com/chkr1011/MQTTnet/wiki/Client

here is working sample to send message:

var client = new MqttFactory().CreateMqttClient();
var options = new MqttClientOptionsBuilder()
.WithClientId($"HomeMessenger1")
.WithTcpServer("192.168.1.10", 1883)
.WithCredentials("bud", "%spencer%")
//.WithTls()
.WithCleanSession()
.Build();

 System.Threading.CancellationToken cancellationToken;
        await client.ConnectAsync(options, cancellationToken);

        var message = new MqttApplicationMessageBuilder()
            .WithTopic("Home/gate")
            .WithPayload("H")
            .WithExactlyOnceQoS()
            .WithRetainFlag()
            .Build();
        await client.PublishAsync(message, cancellationToken);    

develop Notepad++ plugins (in c#).

Notepad++ is awesome and you should be too, by developing plugins for it. To do so:
1) Go to notepad++ website, resource section (at least it was called it so back in 2020).
2) Find section about developing plugins, and options for other languages, select your language of choice.
3) Find “visual studio project template” zip archive, download it and copy it under your documents/Visual Studio 20xx/Project Templates
4) Open VS, create new project and search for “Notepad++” project template, it will create you nice “Hello plugin” sample.
Note: It might not showup. If everything fails, just search for example projects on same plugin page and use it as a template for your project.

useful sample to cover most needs

IntPtr currentScint = PluginBase.GetCurrentScintilla();
ScintillaGateway scintillaGateway = new ScintillaGateway(currentScint);

int numLines = scintillaGateway.GetLineCount(); 
StringBuilder endResult = new StringBuilder();
for (int lineIndex = 0; lineIndex < numLines; lineIndex += 1)
{                   
   string sourceData = scintillaGateway.GetLine(lineIndex);
   endResult.AppendLine(DoMyTransform(sourceData));
}
scintillaGateway.SetText(endResult.ToString());

Actions with retries

private static void DoWithRetries(Action action, 
                                  int tryCount = 3, 
                                  string addMessage = "")
{
    bool success = false;
    int tryIndex = 0;

    do
    {
        try
        {
            action();
            success = true;
        }
        catch (Exception ex)
        {
            tryIndex++;

            // any sort of logging
            Console.WriteLine($"Operation [{addMessage}] failed. Attempt nr. [{tryIndex}]. Exception: [{ex.Message}]");

            if (tryIndex >= tryCount)
                throw;
        }
    } while (!success);
}

Usage example:

DoWithRetries(() =>
{
    if (rng.Next(0, 3) > 0) // 33%
    {
        throw new Exception("random exception");
    }
    Console.WriteLine("Operation finished successfully");
}, 5, "Random Exception Thrower");

Result:

Operation [Random Exception Thrower] failed. Attempt nr. [1]. Exception: [random exception]
Operation [Random Exception Thrower] failed. Attempt nr. [2]. Exception: [random exception]
Operation finished successfully

Posting with Windows Auth

Problem:
Asp.net core controller method:

[HttpPost]
[Route(“/api/startwf”)]
public async void StartWorkflow([FromBody] WFInitiationDataModel model)

Use powershell to test it:

$data = @{
    "sysName" = "LTPS"        
}

$payload = ConvertTo-Json $data

$url = "http://localhost:5000/api/startwf"
Write-Host "POST:$url"


$response = $payload | Invoke-WebRequest -uri $url -Method Post -UseDefaultCredentials -ContentType "application/json" -TimeoutSec 21600

PowerShell SQL Select => Excel

This function runs an SQL query and displays results in a new Excel sheet:

function SQLtoXLS {
    param([String]$sql, [String]$server, [String]$db)

    $tbl = Invoke-Sqlcmd -ServerInstance $server -Database $db -Query $sql

    $xl = New-Object -comobject Excel.Application
    $xl.Visible = $False
    $xl.DisplayAlerts = $False

    $wb = $xl.WorkBooks.Add()
    $ws = $wb.Worksheets.Item(1)
    $ws.Name = "SELECT results"

    $r = 0

    if($tbl.Count -gt 0)
    {
        $rowCount = $tbl.Count
        $colCount = $tbl[0].ItemArray.Count

        $grid = New-Object 'string[,]' ($rowCount + 1), $colCount

        $c = 0
        foreach($col in $tbl[0].Table.Columns)
        {
            $grid[$r, $c] = $col.ColumnName
            $c++
        }
        $r++

        foreach($row in $tbl)
        {
            $c = 0
            foreach($i in $row.ItemArray)
            {
                $grid[$r, $c] = [String]$i
                $c++
            }
            $r++
        }

        $ws.Range($ws.Cells.Item(1,1), $ws.Cells.Item($rowCount + 1, $colCount)).Value = $grid
        $ws.Range($ws.Cells.Item(1,1), $ws.Cells.Item(1, $colCount)).Font.FontStyle = "Bold"
    }
    else
    {
        $ws.Cells.Item(1, 1).Value2 = "No results"
    }

    $ws.Columns.AutoFit() | Out-Null
    $ws.Rows.AutoFit() | Out-Null
    $xl.Visible = $True
}

Usage example:

SQLtoXLS -sql "SELECT * FROM sql_table" -server "localhost" -db "my_db"