TELNET to test connection to POP3/IMAP

First, make sure TELNET is installed on your machine else you’ll get error. You can install TELNET through Add or Remove feature on Windows.
You can use TELNET via a command prompt to confirm.

For IMAP check port 143 and if using SSL then check port 993.

For POP3 check port 110 and if using SSL then check port 995.

Test POP3 connection:

  • Start a command prompt
  • Enter TELNET {webserver} port e.g. TELNET exchangesrvr01 995
  • Enter USER {login} e.g. USER testemail@mycomp.com
  • Enter PASS {password} e.g. PASS secret
  • Enter LIST to show the mailbox
  • Enter QUIT to Exit

At each stage you should receive an OK message. If there is an issue at any stage you will receive an error

Test IMAP connection:

  • Start a command prompt
  • Enter TELNET {webserver} port e.g. TELNET exchangesrvr01 143
  • Enter . LOGIN {login} {password} e.g. LOGIN testemail@mycomp.com secret
  • Enter . LIST “” “*” to show the mailbox
  • Press CTRL + ] and then QUIT to Exit

At each stage you should receive an OK message. If there is an issue at any stage you will receive an error.

Register and unregister COM component using regsvr32 command

You’ll need to open Command Prompt in Admin mode to make the changes especially when User Account Control (UAC) is enabled on your Windows 10 machine. CD to system32 for 64-bit registration and SysWoW64 for 32-bit registration.
Place the COM component in the folder from you want to register and also your executable is placed e.g. “C:\TFS\Code\bin\” in the example below.

To register use the below command:

regsvr32 "C:\TFS\Code\bin\MailObj.dll"

To unregister use the below command:

regsvr32 /u "C:\TFS\Code\bin\MailObj.dll"

You should get a prompt that the register/unregister command succeeded.
This command can be used to register/unregister DLL or OCX files.

Use Robocopy to copy files on Windows Server

Robocopy is a robust file copy command for the Windows command line. It allows users to copy files, directories, and even drives from one location to another. It can be automated with Task Scheduler to run at specific times.

Robocopy example to copy a file from Source to Destination:

Format:

Robocopy [Src] [Dest] [filename] [options]

Src: Can be local or UNC path

Dest: Can be local or UNC path

Code Sample batch file:

SET DTVARYEAR=%Date:~10,4%
SET DTVARMONTH=%Date:~4,2%
SET DTVARDATE=%Date:~7,2%


echo Sync Started

Robocopy C:\Data\TestFolder C:\Data\Logs\TestFolder\%DTVARYEAR%-%DTVARMONTH%-%DTVARDATE%_TestFolder testlogfile.log /FFT /Z /XA:H /LOG+:C:\Data\SchedTasks\LogsTest\%DTVARYEAR%-%DTVARMONTH%_AppLogs.txt 

echo  Log synced

The above code will copy the testlogfile.log from Source to Destination.

Options:
/FFT Assumes FAT file times (two-second precision).
/Z Copies files in restartable mode.
/XA:H exclude hidden file
/LOG+ log output to file and append.

Additional useful options:
/MIR Mirrors a directory tree (equivalent to /e plus /purge).
/XF exclude specified file
/XD exclude specified directory

Standard deviation using C# and Ruby

Standard Deviation definition states:

A quantity expressing by how much the members of a group differ from the mean value for the group.

The logic below calculates the Standard Deviation for the population of values. If the data is being considered a population on its own, we divide by the number of data points, say N. If the data is a sample from a larger population, we divide by one fewer than the number of data points in the sample, n-1. So you can change the formula accordingly.

Ruby code sample:

#sample = Array[1,2,3,4,5,6,7,8]
sample = Array[1,2,3,4,5]

def calc_standard_deviation(values)
    avg = values.sum {|x| x.to_f} / values.size.to_f
    Math.sqrt(values.sum {|x| (x.to_f - avg.to_f) ** 2} / values.size)
end

puts "Standard Deviation is: " + calc_standard_deviation(sample).to_s

Run the Ruby code to test as below in VS Code:

PS C:\code\Ruby test> ruby .\stdDev.rb
#Ruby Output: Standard Deviation is: 1.4142135623730951

C# code sample:

static void Main(string[] args)
{
	//double[] sample = new double[8] { 1, 2, 3, 4, 5, 6, 7, 8 };
	double[] sample = new double[5] { 1, 2, 3, 4, 5 };

	Console.WriteLine("Standard Deviation is: " + StdDev(sample));
}

public static double StdDev(IEnumerable<double> values)
{
	// Get the mean.
	double mean = values.Sum() / values.Count();

	// Get the sum of the squares of the differences
	// between the values and the mean.
	var squares_query =
					from double value in values
					select (value - mean) * (value - mean);
	double sum_of_squares = squares_query.Sum();

	return Math.Sqrt(sum_of_squares / values.Count());
}

C# Output: Standard Deviation is: 1.4142135623731

Encrypt web.config with RSA encryption

Encryption done locally on a Server using command line can be used only if the Application is hosted on the same machine. If the Application is hosted in a load-balanced environment, the encryption should be done in a way so that the private key can be imported on all the load-balanced machines.
The commands need to be run with Admin permissions on the machine.

Run the following commands in Command Prompt in the similar order with Admin permissions:

Traverse to the path as per .Net Framework-

cd C:\Windows\Microsoft.NET\Framework\v4.0.30319

Encrypting Config file without using RSA Provider

aspnet_regiis.exe -pef <section name>  <path to the config file>

-pef is the action to be performed for encryption.

e.g. aspnet_regiis.exe -pef “connectionStrings” “C:\Data\TestWebApp”

All the connection strings present in the above example will be encrypted.

Encrypting Config file using RSA Provider

Creating Key containers:

Aspnet_regiis.exe -pc “<name of Key container>” -exp

Asymmetric private keys should always be stored in a key container.

Add configProtectedData section in web.config to be encrypted:

<configProtectedData>
<providers>

<add name="MyRSAProvider"

type="System.Configuration.RsaProtectedConfigurationProvider, System.Configuration, Version=4.0.30319.0,

Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a,processorArchitecture=MSIL"

keyContainerName="MyRSAKey"

useMachineContainer="true" />

</providers>

</configProtectedData>

The above section can only be added after < configSections > else it’ll be removed automatically.

The PublicKeyToken above can be found for the System.Configuration dll using the sn utility:

sn -T "C:\Windows\Microsoft.NET\Framework\v4.0.30319\System.Configuration.dll"

The key container name and the Provider name used above can be used in the example commands below.

Granting Access to an RSA Key Container to the AppPool:

aspnet_regiis -pa “<name of Key container>” “NT AUTHORITY\NETWORK SERVICE” -full

Encrypt command using Provider:

aspnet_regiis.exe -pef <name of config section> <path to the Config file> -prov “<name of Provider>”

Exporting the Key Container in Order to be used on Other Machines:

aspnet_regiis -px “< name of Key container >” <Path for Keys XML file> -pri

Import Key Container on another machine:

aspnet_regiis -pi “< name of Key container>” <Path for Keys XML file>

Path for keys e.g. C:\rsakeys.xml. The xml file can be copied to the same path on the other machine.

Delete the Xml File from Your Server

ASP.Net will automatically decrypt the Connection String using the grant permission given above to the AppPool and hence you need to access the Connection String in the same way as you would do normally.

To decrypt the config section locally using aspnet_regiis.exe use the below command:

Decrypt command: aspnet_regiis.exe -pdf < name of config section> <Path to the Config file>

e.g. aspnet_regiis.exe -pdf “connectionStrings” “C:\Data\TestWebApp”

Accessing the Ubuntu vm created on Azure via vnc server on Mac

Currently I’ve setup the Ubuntu Server 18.04 LTS from the Azure marketplace and I’m trying to access it via VNC Server setup on the Linux machine. Also, you’ll need a vnc client like RealVNC or you can also use the screen-sharing client available on your Mac.

Login via SSH:

First you need to login to your Linux VM as a non-root user which you’ve created while setting up the VM. To spin up a new Linux VM, you can check out this post. You can use the Cloud shell to connect to your VM using the non-root username and password to your machine via SSH. Use the Connect menu of your VM and copy the SSH command to run in the Cloud shell.

ssh your_user_name@IP_Address

You just need to replace the your_user_name and IP_Address parts in the above command. Enter the password you’re prompted for to complete the Login as SSH.

Install the required packages:

We now need to install the required packages like Xfce desktop environment and VNC Server which are not bundled in the Ubuntu OS by default. Xfce is a free and open-source desktop environment for Unix and Unix like Operating Systems.

Update list of packages:
$ sudo apt update
Install Xfce Desktop environment and wait for the installation to complete:
$ sudo apt install xfce4 xfce4-goodies
Install the VNC Server:

$ sudo apt install tightvncserver

Complete the initial configuration and provide the setup password:
$ vncserver

Providing a view-only password is optional. You’ll get the below Output as the initial configuration completes:

Creating default startup script /home/your_user_name/.vnc/xstartup Starting applications specified in /home/your_user_name/.vnc/xstartup Log file is /home/your_user_name/.vnc/your_hostname:1.log

Configure VNC Server:

The VNC Server is by default configured on the port 5901 and display port :1. VNC can launch multiple instances on other ports like :2, :3 and so on.

Let’s first kill the current instance for further configuration that we require:

$ vncserver -kill :1

Output:

Killing Xtightvnc process ID <ID>

Backup the xstartup file before modifying:

$ mv ~/.vnc/xstartup ~/.vnc/xstartup.bak

Create a new xstartup file and open in editor:

$ nano ~/.vnc/xstartup

Add the following lines to your file in the nano editor and save it:

~/.vnc/xstartup
#!/bin/bash
xrdb $HOME/.Xresources
startxfce4 &

This is making certain settings to the graphical desktop like colours, themes and fonts. The last line is starting the Xfce desktop. Now, let’s convert the file to an executable and restart:

$ sudo chmod +x ~/.vnc/xstartup
$ vncserver

Now, let’s connect to the VNC Server from your Mac by creating a SSH tunnel and use Screen-sharing client to connect.

Run this command on your Mac terminal:

$ ssh -L 5901:127.0.0.1:5901 -C -N -l your_user_name your_server_ip

Do replace the your_user_name with your sudo non-root username and your_server_ip with the IP Address of your Linux VM. Provide the password when prompted for your username.

Now, open your screen sharing App available in the Finder Go Menu on your Mac that says “Connect to Server…”.

Click on Connect and provide your password when prompted again and you’ll see the Xfce Desktop running via Screen-sharing.

Good old dir and copy commands

This is a true troubleshooting story!

Today, one of the users of my application wanted to know if we can find out the original title of a ticket created in the Application. Although, we do not store any such logs for modifications, while on the verge of giving up, I remembered we archive the e-mails received by the Windows Service application that processes the e-mails and archives them at a location on the Server on which it is installed. The e-mail Subject line is stored as the ticket title. These tickets which are created by the Windows Service can be viewed in the Web Application.

Since the folder with the archiving e-mails have a lot of ’em, simply doing a Windows Search is a time killer! So I remembered my way through the good old DOS commands.

Since the .eml file has the ticket number, I used the dir command to search for the e-mail name and copy command to copy it to another location quickly.

Firstly cd to the location where the archived e-mails are stored on the Server:

So e.g. if the ticket number is 123456

dir *123456* /s

The /s searches for the File name inside the two wildcard characters asterisk “*” with the dir command.

Then if the file name is e.g. TICKET123456 – abc.eml then use copy command as follows within the same directory:

copy "TICKET123456 - abc.eml" c:\Temp

So, with the e-mail having the original Subject Line preserved, I could share the original ticket title.