Posts

Showing posts with the label C#

Monitoring Hosts and Domains for RBL Listing Using Azure - Part 2: Deployment

Image
In the previous post of the series ( here ), we went through the design of a solution to help monitor host and domain listing in RBLs. In this article, we'll go through the process of deploying and configuring the required resources on Azure. To deploy the solution to your Azure subscription, you have to perform two tasks: Deploy the Azure resources, that is the Storage Account and a Function App Deploy the Azure Function App application code To deploy the Azure resources, you have to submit an ARM deployment task using the ARM template file saved in the repository ( here ). There are two ways to create a deployment using this file: Deploy to Azure button   The main repository page contains a button that opens the Azure portal and prompts for parameters for the deployment: Leaving the default values will result in a deployment in the same location as the resource group and randomized resource names. ARM File Deployment The other way to deploy an ARM file is to use the Azure Powers...

Monitoring Hosts and Domains for RBL Listing Using Azure - Part 1: Design

Image
The thing that prompted the publishing of this series of posts is a recent case of a customer with a large mail platform. The Mail Transfer Agents handling the internet mail flow for such a platform are usually susceptible to being flagged as malicious on RBLs, causing issues with mail flow. The solution described in these posts helps monitor the status on the RBLs and trigger alerts in service management systems, using modern cloud application development techniques. But wait, what is an RBL? A Real Time Block list is a service that keeps track of the domain and/or the IP address of the hosts reported to be sending SPAM or malicius messages. RBLs are actual DNS zones where IP addresses or domain names are represented by A records. Let's take a list, say rbl.example.net, for example. To check if the host with IP 100.101.102.103 is listed we have to query the DNS for the host 103.102.101.100.rbl.example.net. If there is no such host known (the responce is NXDOMAIN) the host ...

Building a PowerShell cmdlet using C# - Part 4: Packaging the Cmdlet

Image
This is the fourth and last article of the series on how to build a PowerShell cmdlet. On the previous articles we created a simple cmdlet, troubleshooted the code and took a quick tour on the output streams. Although you can import the dll built by Visual Studio directly as a module, this is not the best approach, since you won't be able to publish it, provide help for your cmdlets and many other features provided by PowerShell modules. To create a module we'll start by creating a folder with the same name of the module. This is the folder that is going to hold all the module files. We'll call this module DemoCmdlet, since this is the name of the Visual Studio project and the dll produced. All modules require a module manifest so let's create one with the New-ModuleManifest cmdlet: PS C:\DemoCmdlet> New-ModuleManifest -Path DemoCmdlet.psd1 PS C:\DemoCmdlet> ls     Directory: C:\Users\cpolydorou\OneDrive\tmp\Blog\DemoCmdlet Mode   LastWriteTime L...

Building a PowerShell cmdlet using C# - Part 3: Using the Output Streams

Image
Now that we've seen how to setup the code base for a cmdlet and how to debug it, let's take a look on how it will interact with the world via the various output streams. Verbose Output - WriteVerbose() One of the most useful functions of every cmdlet. Provides detailed information about the execution of the command. Debug Output - WriteDebug() The latest versions of PowerShell give the ability to debug to the end users. The -Debug  parameter along with the WriteDebug  method, pause the execution at specific points so that the end user can debug it. Warning Output - WriteWarning() It goes without saying that if any issues arise during the execution of the cmdlet, those should be reported to the user. For minor issues that do not have an impact on the result of the command, use the WriteWarning method. Errors - WriteError() However, when there are issues that do have an impact on the result, we have to inform the user about them. Things get a litt...

Building a PowerShell cmdlet using C# - Part 2: Debugging

Image
On the previous article of the series, we created a Visual Studio project for a cmdlet and added the base code. This article is going to be about debugging the code, an ability that will greatly reduce the time taken to troubleshoot. Right click your project and select "Properties". On the properties tab, click "Debug" to go to debug settings. Here we are gong to select the "Start external program" option and browse to the powershell executable located at "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe". This way, when we start the debugger, a PowerShell window will start. We also want our module to be loaded to that PowerShell host so fill the following in the "Command line arguments" box: -noexit -command "&{ import-module .\DemoCmdlet.dll -verbose}" Replace the name of the dll with the one created for your project. You can find it by browsing to the bin/Debug folder in your project. When you h...

Building a PowerShell cmdlet using C# - Part 1: Basic Code and Requirements

Image
As PowerShell evolved, it gave administrators more are more options when creating functions. The latest addition in PowerShell 5 was classes which along with the ability to use .NET classes allows us to build functions for even the most complicated tasks. The use of classes and .NET objects however is much easier in C# and the fact that the code is faster and strongly typed makes C# a better choice when building complicated functions. In this article, we are going to create a cmdlet using Visual Studio. Let's get started! Fire up Visual Studio and create a Class Library project: When the project is created, it will contain a class file that is going to hold the code for our cmdlet. Before starting to write the code, we have to add a package that will provide all the necessary tools to create the cmdlet. Right click the solution in the Solution Explorer and select "Manage NuGet Packages for Solution". When the package manager tab has opened, click "...

How To Write to the Event Log in C#

I've been doing a lot of C# developing for Microsoft Exchange lately and I would like to share with you a few lines of C# that I've found to be very useful in event handling. I prefer using the windows event logs and not text files for logging since they will not grow out of control and are very easy to search and handle. The function below will create an event in the specified log, under the specified source with the provided information: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 private static void WriteEvent (String LogName, String EventSource, String EventBody, int EventId, EventLogEntryType EventType) { try { using (EventLog eventLog = new EventLog(LogName)) { if (!EventLog.SourceExists(EventSource)) EventLog.CreateEventSource(EventSource, LogName); eventLog.Source = EventSource; eventLog.W...

Case insensitive String.Contains for C#

So, you want to use the String.contains method but you want it to be case insensitive. You can use the following test instead: string s = "MYSTRING"; if( s.IndexOf("string", 0, StringComparison.CurrentCultureIgnoreCase) != -1 ) {     // String contains "string" } else {    // String does not contain "string" } We use "-1" because "0" can be an index