Office 365

PowerShell – the mighty tool from Microsoft

27 października, 2021 2
Podziel się:

PowerShell is a cross-platform tool, that is able to perform many administrative operations and changes in the OS (to be clear not only for OS). PowerShell is not only a tool, it is a solution for administrators to perform scripts, configure many things across f.e. domain or even whole tenant of your company. Bulk operations were never easier thanks to that solution. Managing users, creating and configuring mailboxes or even create Conditional Access Policies.

Fig.1 PowerShell Icon - PowerShell – the mighty tool from Microsoft

Fig. 1 PowerShell icon

A brief history of PowerShell

PowerShell was first introduced in 2006 for Windows XP SP2, Windows 2003 SP1, and Windows Vista while remaining as an optional feature for Windows Server 2008. Below the first version of PowerShell.

As you can see it really not differs much from the present version presented below:

Fig.3 PS Version 1024x472 - PowerShell – the mighty tool from Microsoft

Fig. 3 PS Version

Despite difference (on the first glance) in the visual aspect, which is related mainly to the running OS and it’s settings, the magic hides behind in the core of the Shell. Since the first release of PowerShell there were many features added such as script debugging, block comments (useful for script writers), eventing and scheduling jobs for Windows Task Scheduler.

A short list of the pros and cons of using PowerShell

Below is a short list of pros and cons of using the PowerShell to manage mailboxes, Exchange properties and other Microsoft Services manageable by PowerShell and etc.

Pros:

  • Much more knowledge about what is done to specified object (Azure Object, Mailbox).
  • Object management easily modified (mostly) using specified commands.
  • Much more info regarding specified object (many properties not seen in GUI).
  • Bulk operations using *.csv files (for example a list of users or other objects to be modified using the same attribute).
  • You can use variables just like any programming language.

Cons:

  • Some of managed attributes require other modules to be installed (not a con, but you still need to install the module).
  • You need to know what you are doing! – most of the commands are easy to use and you can predict what will happen, but some of them can cause disaster to your tenant.

Example of PowerShell usage for the administrators with a little backstory of our app

During my first weeks in the company I learned a lot about Azure and its capabilities. While I was lurking in the processes and the functions I reckon that some of the tasks can be performed using simple PowerShell commands instead of *click* *click* via GUI (Graphic User Interface). With my colleague we came up with an idea. How about creating our own “app” for some of the operations? Since the idea and start of the realization there were about 20 minutes. I started gathering info about needed modules, what is needed to be installed and how to make this all real thing.

Implementation of the idea in a few steps

All of the next operations are needed to be run as administrator.

  • First thing to do is to get the needed modules, but to get them we need to have proper package providers.
    To start we need to install the “NuGet” using: Install-PackageProvider -Name NuGet -Force:
Fig.4 Package installation 1024x472 - PowerShell – the mighty tool from Microsoft

Fig. 4 Package installation

  • When it is installed we proceed to find the exact module that is needed to be installed in order to get our work done. For our app we needed something that will do some tasks regarding Exchange Online. To find a specified module we needed to look for it in repositories. The easiest way is to find it’s name using Find-Module command.
Fig.5 Find module command 1024x472 - PowerShell – the mighty tool from Microsoft

Fig. 5 Find-module command

  • The next step is the installation of desired module. In our case it is ExchangeOnlineManagement. To install simply use the command: Install-Module -Name ExchangeOnlineManagement -Repository *YourRepositoryName*

You need to specify from which repository you are downloading the module otherwise you will receive an error.

  • After the module is installed to make sure, you can run the Get-InstalledModule to list all of the modules installed for Your PowerShell, where You should see now the module name installed. To check you can also try running ‚Connect-ExchangeOnline’ which after triggering should prompt you for your creditentials to log in to your tenant Exchange.

    Fig. 6 Connect to Exchange Online 1024x473 - PowerShell – the mighty tool from Microsoft

    Fig. 6 Connect to Exchange Online

  • Successful login will simply line up for another PS commands to be written. From now on you will be able to do (almost) whatever you want inside tenant Exchange Online server.

A disclaimer before proceeding

Many of the commands you can use have an additional modifier for the command which is ‚-WhatIf’. When used at the end of the command you will see an output for the command and show what will be outcome of performing such a command. On example below you can see the proper work of the modifier.

Fig.7 What if modifier 1024x476 - PowerShell – the mighty tool from Microsoft

Fig. 7 What-if modifier

Creation of one application

After installing everything I was ready to work further. Simple scripts were not enough for me as I still had to copy paste everything and run the scripts again. Me and my colleague came up to an idea to summarize everything into one single smooth app, which would allow us to go do the repetitive tasks even faster.

The info regarding ‚our GUI’ was google and other sites where we gathered info how to create it, implement an icon, text on top of the app window and etc. Then all we had to do was to implement commands into the task boxes and test everything. Below you can see the final result of the work.

Fig.8 IOC Blocker app - PowerShell – the mighty tool from Microsoft

Fig. 8 IOC Blocker app

In the picture you can see the possibilities of our app. It can be edited to work under your own tenant. The working principle is the text box in which you paste the URL (without http:// or https://) and then click on ‚Block URL’. It will perform a simple command assigned under the button. When field is empty a popup window comes up (info for us that the field cannot be empty) and it will disconnect the session, so we will have to log in again (just for security purposes). The correct performance of command will also be shown as popup but with information that task has been completed successfully.

Fig.9 Error message - PowerShell – the mighty tool from Microsoft

Fig. 9 Error message

Below there is a little section from the whole script where the button comes to live by defining its parameters and the command itself to be performed.Fig. 10 A little section from the whole script - PowerShell – the mighty tool from Microsoft

Conclusion at the end

As you can see this is not a rocket science and a simple working app can be done in PowerShell to improve the work of an administrator for prosaic cases like blocking an URL, domain and even purging spam/phish mails from users mailboxes.

The app can be edited to do more than that! By adding a few text boxes you can improve its functionality by for example adding a search query for content search from Security&Compliance feature from Microsoft. Then you could do all of the tasks regarding spam/phish tasks.

This is just a little example of what PowerShell is capable of and administrators who are more familiar with it could do more difficult and complex cases by simply running a few lines of code, when doing it by GUI would take up to few hours of ‚clicking’. In addition to that Azure has its own CLI in which you can run PowerShell commands so this little “helping hand” available for many years is not an exclusive for On-Premises environment.

Kategorie: Office 365
Krzysztof Cieplechowicz
Autor: Krzysztof Cieplechowicz
2nd Line Support Specialist. An enthusiast of getting to know the aspects of applications and services he deals with on a daily basis. He uses his knowledge and experience to accomplish goals as fast as possible, while maintaining accuracy and effectiveness. As part of his daily duties, he acts as part of L2 Support, where various solutions and technologies present their challenges - puzzles to be solved. In his free time, he enjoys cycling, visiting interesting places and finally - constantly getting his know-how in terms of hardware novelties.

    Imię i nazwisko (wymagane)

    Adres email (wymagane)

    Temat

    Treść wiadomości

    komentarze(2)

    avatar
    Adam
    27 października 2021 Odpowiedz

    Super artykuł o potężnym narzędziu! Brawo Krzysztof, tak trzymaj :)

    avatar
    Szymon
    27 października 2021 Odpowiedz

    Dobra robota, masz potencjał i powinieneś go rozwijać.
    Zasadniczym minusem takich tooli jest to, że duże grono "operatorów" po 3 latach pracy dostając pytanie jak zablokować domenę odpowie, że kliknąć w toolu Krzysztofa i Tomka... ;)

    Zostaw komentarz