Posts

Graph Data Structure for Powershell

Image
A Graph is a non-linear data structure consisting of nodes and edges. The nodes are sometimes also referred to as vertices and the edges are lines or arcs that connect any two nodes in the graph. Graphs are used to solve many real-life problems such as representing networks - like paths in a city or telephone network or circuit network. Graphs are also used in social networks like Linkedin and Facebook.   There are many times that I've come across problems in the IT industry that graphs could help solve. To demonstrate the use of graphs to process connections between items, we'll use Exchange server mailboxes as nodes and the permissions between them as edges. The mailboxes variable shown below is loaded with mailbox info for five users. The majority of the objects' properties have been removed to simplify the demo: We'll start with importing the DataStructures module that contains the Graph type and then create a graph object: There are no nodes in the graph so it is e

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