Posts

Showing posts from 2021

Data Structures in Powershell

Image
A good algorithm is not always the solution to a given problem. In most cases, a mechanism to save the data used by the algorithm is required, in order not only to organise the data but also optimise its execution. Such mechanisms are called Data Structures and you can think of them as small databases. Data Structures not only hold data, but also expose methods to access and update it. My DataStructures module contains the most frequently used data structures, implemented for Powershell objects in C#. At the time this post is written, the module contains the stack and queue structures, but more are on the way. Stack The idea behind the stack data structure is to save objects in order to be processed later in the execution of an algorithm. The below figure shows the way stacks work: As we insert items in the stack, the items already in it are pushed to the bottom. When removing an item, the item to be removed is the most recent one, hence the alternate naming LIFO - Last In First Out...

Solving Bin Packing with Powershell

Image
Welcome to another post regarding optimization algorithms in Powershell. Today we're going to examine the Bin Packing problem and a way to solve it using Powershell. Bin Packing is the problem of separating a set of items into subsets, in a way that their size does not exceed a specific value and the number of sets is the minimum possible. As you can imagine, there are multiple applications of such an algorithm in everyday life, such as in logistics (e.g. containers on ships), storage, and even job scheduling. More information on the Bin Packing problem is also available on Wikipedia .   To make things easier for people not familiar with programming languages like C# and Python, I've decided to convert my C# code to a cmdlet that will access Powershell objects and will separate them into groups. The cmdlet is called Get-BinPacking and is available as part of my AdvancedAlgorithms module published on PowershellGallery here , starting from version 1.2.0.0. There are two ways to...

Solving Knapsack with Powershell

Image
One of the most common tasks when designing the migration from a Microsoft Exchange organisation to Exchange Online, is to separate the mailboxes into batches. I recently had to solve a quiz like that where I had to select the right mailboxes to migrate on each day, given the restriction imposed by the network bandwidth available. The way I finally approached it - and trigger for this post - was to apply the knapsack algorithm in order to make the best selection for each migration batch. There are of course many other parameters that you should take into account in cases like that, such as the role of the user and any permission delegations. A short description of the knapsack problem, for those that have not heard it before. We are given a collection of items that each one has a specific weight and value and a knapsack that has specific weight capacity. The goal is to identify the optimal selection of items to put in the knapsack that will not only keep its total weight under the maxi...

Passing information between Azure DevOps Pipelines using Powershell

Image
In this article, we are going to examine one of the possible ways to transfer information from an Azure DevOps build pipeline to a release pipeline. The goal is to pass variables with information created during the execution of the build pipeline to the release pipeline. To accomplish this, we are going to use Powershell to create a build artifact that will contain all the variables needed, and then we'll consume it from the release pipeline. Starting with the build pipeline, we'll be using the classic bicep deployment that has outputs. This is the information that we would like to have available in the release pipeline. Think of a WebApp that is created by the build pipeline and we need its resource group and name in order to publish our code to it. First, let's go through the steps of the build pipeline. We have three steps in total, the azure deployment, the conversion of its outputs to variables, and finally the creation of the artifact. There is nothing sophisticated a...

How to deploy Bicep templates using Azure DevOps Pipelines

Image
In this article, I'm going to walk you through submitting Azure Deployments using Bicep files from Azure DevOps. Unfortunately, up until now, there is no official task for this need, so we are going to use Azure CLI instead. We'll start with creating a new pipeline, so open your project in Azure DevOps, switch to the Pipelines view, and hit the new pipeline button. We're going to use the classic edition for this demo, so go for the Use the classic editor option at the bottom. The first step of a build pipeline is to select the source that contains your code. For this, I've created a Git repository in the same project that contains a bicep file and looks like this: By default, the source repository is going to be set to Azure DevOps, which is fine for our case: Moving on to the next step, we're presented with some template options for the pipeline. We'll start with an empty pipeline that we'll build ourselves so click the Empty Job option. The next screen i...

Manage Azure AppService Files using Powershell

Image
For the last couple of weeks, I've been working on a project that is focused on Azure App Services and how certain applications can be hosted on them. Part of the project was to examine the performance of the storage space that each plan offers and whether it can accommodate the needs of each application. If you deploy an App Service that is using code instead of containers, you'll have to upload the files to the App Service's storage yourself. This can be achieved in many ways, including DevOps pipelines, development tools such as Visual Studio and FTP. In my case, I was handed a testing version of the website code that was based on PHP and a large number of files used by the site, including CSS and JS and a lot of image files that I had to upload to the AppService. In order to provide the required information regarding the endpoints to connect to and with what credentials, Azure provides a file called "Publish Profile". There are three profiles included in the p...

Monitoring Hosts and Domains for RBL Listing Using Azure - Part 3: Management

Image
Following my last post about monitoring your domains and hosts for RBL listing, we are going to continue with the PowerShell module to manage the solution and a script to query the status of hosts and domains. Since the solution is accessible via HTTP, it couldn't be simpler to manage using PowerShell. But first, let's take a look at the files in the module. The module is comprised of three files, the module manifest (.psd1), the script file (.psm1), and a configuration file (.json). The manifest contains the module parameters and configuration, the script file the code of the functions, and the configuration file a couple of settings such as the URL of the function app and the key to use. We'll talk about key management later in this article. The module contains the following functions: List related Get-AzureRBLList Add-AzureRBLList Remove-AzureRBLList Host related Get-AzureRBLHost Add-AzureRBLHost Remove-AzureRBLHost Check-AzureRBLHost Domain related Get-AzureR...

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...