Wednesday, 17 May 2017

C# Windows Service Debug

Hi all, here an easy example to make a debug on C# Windows Service ,
Implementation of Code founded on:

https://www.codeproject.com/Articles/14353/Creating-a-Basic-Windows-Service-in-C

With this useful example you create a Single *.exe file , but if you start the debugger
you can't debug the code with Visual Studio, Here is how to do:

First, make all step writed on CodeProject(in future reference I call this project "CodeProjectWS"), after you need to do this:

Right Clik on Current Solution -> Add -> New Project
On Project Type select Windows Consolle Application
select a name for new project, I choose :"LwebCodeWinDbg" -> Ok
Now in New Project "LwebCodeWinDbg" right-click the project name and go to:
[Add]->[Class]. Name the class "LwebCodeWindowsService.cs" and then hit OK.
Open class "LwebCodeWindowsService" and put this code:


namespace LwebCodeWinDbg
{
    public class LwebCodeWindowsService
    {
        public static void WindowsServiceStart()
        {
            System.IO.File.WriteAllText("c:\\LwebCodeTetsService.txt", DateTime.Now.ToString());
        }
    }
}

   
Now in New Project "LwebCodeWinDbg" open Program.cs and paste this code over Main Function:

namespace LwebCodeWinDbg
{
    class Program
    {
        static void Main(string[] args)
        {
            //put debug breakpoint on this istruction:
            LwebCodeWindowsService.WindowsServiceStart();
        }
    }
}


Now Open "CodeProjectWS" , right Clik on "Reference"-> Add Reference
in left pane select: "Solution"    -> Browse... and browse to "LwebCodeWinDbg" Project and then hit OK.
Open WindowsService.cs
and paste this code on OnStart Event:


protected override void OnStart(string[] args)
        {
            base.OnStart(args);
            LwebCodeWinDbg.LwebCodeWindowsService.WindowsServiceStart();

        }

with this istruction you can call the same function from Windows Service and Windows Consolle Application.
To debug the Windows service right click on "LwebCodeWinDbg" -> Set as Startup Project
F5 to start debug.

Hope it helps

Tuesday, 18 April 2017

Javascript read JSON file and loop through records(optional with JQuery)

Hi all, here a very fast example on how to read json data from file in Javascript,
here the content of *.htm file:


<!DOCTYPE html>
<html>
<head>
    <title>LWebCode Javascript Read JSON from file</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
    <script type="text/javascript" src="mydata.json"></script>
    <script>
        function ReadData()
        {
            var dataLstAz = JSON.parse(jsLstData);
            alert(jsLstData.length);
          
            for (var key=0, size=jsLstData.length; key<size; key++)
            {
                var obj = jsLstData[key];
                liout = obj.Name + ", " + obj.Age + "<br/>";
                $("#Output").append(liout);
            }
        }
      
        $( document ).ready(function() {
            ReadData();
        });
    </script>
</head>
<body>
    <div id="Output">
    </div>
</body>
</html>


Here the content of "mydata.json" file,
put this file in same folder of *.htm file, or change path in *.htm file:


jsLstData = '[{"Name":"MyName1","Age":"20"}, {"Name":"MyName2","Age":"22"}]';
//End of file "mydata.json"


Now simply open *.htm file in browser and you'll get this output:

MyName1 20
MyName2 22


That's it, hope it helps

Friday, 31 March 2017

PowerShell Certificate Authority List, Loop, Filter Trought CA Certificate Authority

PowerShell Loop Trought CA Certificate Authority,
Hi all, here a quick and easy PowerShell script to list and filter all details of Installed Certificate Authority, if needed filter on line(s) given from certlst.txt file

function LWebCode_CALoop
{
    $MyDir = Split-Path -Path $MyInvocation.ScriptName
    $foutput = $MyDir + "\certlst.txt"
    Get-ChildItem -Recurse Cert: > $foutput
    $reader = [System.IO.File]::OpenText($foutput)
    while($null -ne ($line = $reader.ReadLine()))
    {

        echo $line
        /* optional filter on line(s)
        if($line -Match "Thumbprint")
        {
            echo $line
        }

        */
    }
}

LWebCode_CALoop


Do inside if block your preferred stuff

Saturday, 18 March 2017

Send Mass Mail C#, PowerShell, Visual Basic, ASP, PHP, Curl, Node, Ruby, Python, Java, Go

Hi all, here some example and links to send Mass mail,
All this site can works with most common language: C#, PowerShell, Visual Basic, ASP, PHP, Curl, Node, Ruby, Python, Java, Go,
have lot of report and statistics graph, offering high level of personalization, and allow you to create your custom Marketing campaign


http://www.mailjet.com
Free Account has:
Month Max Limit: 6000
Day Max Limit: 200
Developement Support: SMTP , API


http://www.mailgun.com
Free Account has:
Month Max Limit: 10000
Day Max Limit: 400
Developement Support: SMTP , API


http://mailchimp.com
Free Account has:
Month Max Limit: 12000
Day Max Limit: ?
Developement Support: API

Monday, 6 March 2017

PowerShell Encryption and Decryption, PS V 2.0 or Higher, Easy

Hi All, here's an easy example on how to encrypt or decrypt a string
this way IS NOT SURE because Key value is inside the *.ps1 file,
Another user can easily retrive your data.
This is only to understand basic of encryption in PowerShell



<#
LWEBCODE 
EASY POWERSHELL 2.0 or higher Encryption and Decryption
#>
$global:byteArr = New-Object Byte[] 32
$global:byteArr = [Byte[]](100, 101, 100, 101, 100, 101, 100, 101, 100, 101, 100, 101, 100, 101, 100, 101, 100, 101, 100, 101, 100, 101, 100, 101, 100, 101, 100, 101, 100, 101, 100, 101)

function encrypt($content)
{
    $Password = $content | ConvertTo-SecureString -AsPlainText -Force
    $Password | ConvertFrom-SecureString -key $global:byteArr
    Write-Output "$Password"
    Write-Output "ENC END"
}

function decrypt($content)
{
    $User = "MyUserName"
    $MyCredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, ($content | ConvertTo-SecureString -Key $global:byteArr)
    $plainText = $MyCredential.GetNetworkCredential().Password

    Write-Output "Plain text: $plainText"
    Write-Output "DEC END"
}

encrypt("lwebcode is the best ;D ")

decrypt("76492d1116743f0423413b16050a5345MgB8AEoATwB4ADYAegBQAHcAaABhAGMATQBxAEwAWAA4AEI
ARgA2AE4ARwBlAFEAPQA9AHwAOQBjAGUAOABkAGQAYgAxADgAOABlAGMAMwA3AGEAYwA3AGQAMgA5AD
YAMgBmADQAZAA3ADIAYgAyADYANABiAGMAYQBjADIAMgA3AGEAYwA4AGQAYQBkAGUAOAAwADgANwAyA
DMANAAwADkAZQBhADcANgA5ADUANABhAGEAZgAwAGEAYwA3ADIANAA2ADcANQA3AGIAYQA4ADIAYgAx
ADUAOAAyADIANwBjADgAYgAxADgANAAyAGMAOAA5ADUAMQA0AGYAMQAxADgAOQAwADQAOQBiADQAZQA
yADMANwAyADgAZgBjAGMAOABkADkAYwAxAGMAMQBhADgANAA4AA==")

Monday, 20 February 2017

PowerShell Send Email with Gmail, PS V. 2.0 or Higher

Hi All, here a free script to sending email with gmail and powershell v 2.0 or higher:
If you get error on sending like:

Exception calling "Send" with "1" argument(s): "The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at"...

You MUST enable less secure App access on your gmail's account
https://myaccount.google.com/security

<#
LWEBCODE Mail Sender
On your gmail account enable less secure App access

https://myaccount.google.com/security

In code above replace GMAILUSER with your gmail's user:
lwebcode123@gmail.com=>lwebcode123
#>
function sendmail
{
    $smtpClient = new-object system.net.mail.smtpClient
    $smtpClient.Host = "smtp.gmail.com"
    $smtpClient.Port = 587
    $smtpClient.EnableSsl = $true
    $SMTPClient.Credentials = New-Object System.Net.NetworkCredential("GMAILUSER", "password");
      
    $emailfrom = "lwebcode123@gmail.com"
    $emailto = "lwebcode123@gmail.com"
    $subject = "test"
    $body = "this is body"

    $emailMessage = New-Object System.Net.Mail.MailMessage
    $emailMessage.From = $EmailFrom
    $emailMessage.To.Add($EmailTo)
    $emailMessage.Subject = $Subject
    $emailMessage.Body = $Body
    $emailMessage.Attachments.Add("C:\lwebcode\attachement.txt")
    $SMTPClient.Send($emailMessage)
}
sendmail

Thursday, 16 February 2017

Overlapped I/O operation is in progress, fix Windows Installer error

Hi all, here how you can try to fix Windows Installer error:
Overlapped I/O operation is in progress
On Microsoft SQL Server Setup Installing have Overlapped error

Try to follow these steps:

1    Click Start, click Run, type regedit in the Open box, and then click OK.
2    Locate and then click the following subkey in the registry:
     HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows\Installer
3    On the Edit menu, point to New, and then click DWORD Value.
     Type SecureRepairPolicy for the name of the DWORD, and then press Enter.
4    Right-click SecureRepairPolicy, and then click Modify.
5    In the Value data box, type 2, and then click OK.
6    Locate and then click the following subkey in the registry:
     HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows\Installer
7    On the Edit menu, point to New, and then click Key.
8    Type SecureRepairWhitelist for the name of the key, and then press Enter.
9    Double-click the SecureRepairWhitelist key to open it.
10   On the Edit menu, point to New, and then click String Value. Create String   Values that contain the product codes (including braces {})
     of the products that have to be added to the Safe Recipients list.

     The NAME of the String Value is the "product code" and the VALUE can be left blank. To obtain the product code for other MSIs, open the MSI by using the ORCA tool that is available in Windows SDK.
   
     LWEBCODE NOTES:
     If you can't access to "product code" with
ORCA, you can try this on another machine wich have already the program installed
     in this example we have trouble installing MSSQL Server, so find in another machine
     HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\MSSQL.1\Setup
     key:Product Code => you can copy the value
   
11     Retry to install
   
     If install doesn't work in step N° 5 try to set 1 for SecureRepairPolicy value

   
Reference:

https://support.microsoft.com/en-us/help/2918614/ms14-049-description-of-the-security-update-for-windows-installer-service-august-12,-2014

https://blogs.msdn.microsoft.com/vsnetsetup/2014/10/27/error-997-overlapped-io-operation-is-in-progress-kb2918614-breaks-windows-installer-service/


http://www.sqlservergeeks.com/sql-server-finding-out-sql-server-product-code-from-windows-registry/