What I Learned About VSCode At The PowerShell Summitt

I am going to assume that if you are reading this article that you know what Visual Studio code is and have installed the PowerShell extension for it, so this article isn’t going to tell you how to do any of that. If you don’t know what VS Code is, it is a free, open-source code editor developed by Microsoft for Windows, Linux, and macOS that is completely customizable. You can read more about it on the official site and on the “Why Visual Studio Code?” page. I thought Visual Studio code was awesome before the I attended the PowerShell Summit, and I think it’s even more awesome now.

One last note before I get started. If you are reading this article it’s probably safe for me to assume that you use PowerShell and/or Visual Studio Code. If either of those things are true, you should seriously look into attending the PowerShell + DevOps Summit in 2018. This year was the 3rd year I have attended and every year it gets better. The summit is intentionally kept small, so tickets go fast. To get notified about the 2018 Summit you can sign up for a mailing list or you can follow the official Twitter account @PSHSummit.

Alright, let’s get down to business.

I knew that VS Code added PowerShell language support a while ago, what I didn’t know is that you could set the default language for VSCode to PowerShell. Here’s how you do it:

  1. Use Ctrl + Shift + P to bring up the Command Palette (you should get used to doing this).
  2. Type User Settings and click on Preferences: Open User Settings
  3. In the box on the right hand side type “files.defaultlanguage”: “powershell”
  4. Save it. Congratulations your default language for all new files is now PowerShell!

 

 

The next thing I learned is that VS Code can handle automatic code formatting for PowerShell.  Don’t believe me?  Here is some crappy unformatted code:

To have VS Code do automatic formatting you can either right click anywhere in the document and then select Format Document or you can highlight the code you want to format, right click, and select Format Selection.  Either way you end up with formatted text.

You can also completely customize your own code formatting if you don’t like the default settings.  Go into your User Settings again (just like you did to set your default language) and type “codeformat” by the time you type all of that you should see it display the available code formatting options:

Select any of those and then set the appropriate option.

Since there are squiggly lines in the code example above, now is a good time to mention the next thing that I learned.  When writing PowerShell in VS Code, behind the scenes it is using PSScriptAnalyzer to analyze the code and make recommendations.  There are the different ways to see the problems and recommendations in VS Code.  The first way is if your cursor is on a line with a squiggly, hover over the squiggly line to see the recommendation.

Also, to the left of the code you will see a little light bulb icon.  Left clicking that icon will also give you some information (similar to what hovering over the squiggly line will get you).

Finally, you can view all the “problems” that PSScriptAnalyzer has detected in one place in the output pane.  At the bottom of your screen you should have an area that looks similar to this.  If you don’t, you can enable it by going to View -> Output, or Ctrl+Shift+U.

If you click on the Problems tab, you will see all your “problems” in one place.

Next up, is creating a Visual Studio Code profile, which works exactly like a PowerShell profile.  You will seen in the screenshot showing the output window that my prompt is a $ (because PowerShell is money friend!).  How did I do that?

  1. Browse to your user documents folder and then to the WindowsPowerShell folder (C:\users\jacob\documents\WindowsPowerShell)
  2. Create a file called Microsoft.VSCode_profile.ps1
    1. Clearly you can do this through PowerShell
    2. Put whatever you want VS Code to load into your profile
  3. Reload VS Code by typing Ctrl+Shift+P and then Reload
  4. If you type $profile in the VS Code terminal window you will see an output similar to this

Next up is themes  This seems pretty obvious in retrospect but there are all kinds of themes out there you can use to customize your editor experience.  To browse through the available themes in VS Code click the Extensions icon in the left hand toolbar (square icon at the bottom) and in the search extensions in marketplace box just type Themes.  This will give you a giant list of themes but no sorting.  What about most installed themes?  Type themes @sort:installs .  What about most popular?  Type themes @sort:rating-desc .  The screenshots in this blog are from the built-in Dark theme (highlighted in the screenshot below).  You can view all the available color themes by opening up the command palette (Crtl+Shift+P), type Themes and then select Preferences: Color Theme and then browse through the available options using the up and down arrows.  Once you install a theme extension and reload VS Code it will populate into this list.

Extensions work in exactly the same way.  In the search box on the Extensions section you can type @sort:installs to see the most installed extensions (PowerShell is currently #9).  The extensions I have installed are shown below.  Azure Resource Manager Tools gets you intellisense and snippets for writing ARM Templates in VS Code, Settings Sync allows you to sync your VS Code settings across devices using GitHub, Visual Studio Code Git Hub Support allows you to save and commit code changes to GitHub (which is an entire blog post on its own) and vscode-icons gets you cool icons in the explorer window.

Finally, here are some other user settings you can set that I also learned about at the PowerShell Summit:

  1. editor.fontfamily
  2. editor.fontsize
  3. extensions.autoupdate
  4. editor.formatonsave
  5. editor.formatonpaste
  6. terminal.integrated.fontsize
  7. terminal.integrated.fontfamily

You can read about some more tips and tricks from the VS Code GitHub page.

What are some of your favorite VS Code tips and tricks?

 

Finding All Client Mailboxes in the Office 365 Partner Portal

Someone at work asked me “how many client mailboxes are we supporting in Office 365?”. It’s easy to tell how many clients you are supporting, as soon as you login to the partner portal it tells you that number. I had never tried to get the number of client mailboxes before, but I strongly suspected that I could do it using PowerShell. Turns out I was right!

The first thing you will need to do is to connect to Office 365 using PowerShell. If you haven’t done this before, go to this page and download the Office 365 PowerShell Module and install it.

After that is done, execute the commands below which will connect you to Office 365. It’s important to note that the credential you use must have delegated admin access to all the client accounts or the steps in this article won’t work.

Once that is done running the commands below will provide a list of all the commands in the MSOnline Module as well as only those commands which contain the word partner. I was hoping there was a cmdlet that would allow me to get all my clients information which I could then iterate through to get a list of all mailboxes.

MSOnlinePartner

The Get-MSOLPartnerContract looks interesting, lets try that.

MSOnlinePartnerContract

Well, that gets me a lot of Tenant ID’s, which doesn’t appear to be useful. Or does it? I know from using Office 365 with PowerShell in the past that there are commands that use the TenantID parameter to retrieve information.

Doing some more investigation I run the series of commands below. First I am storing all the Tenant ID’s in a $Clients variable, and then using Get-Member to see what properties this variable contains. Seeing that one of them is Default Domain Name, I run $Clients.DefaultDomainName which sure enough enumerates all of my client domain names so I can see that they are in fact my clients. I am then storing all the Tenant ID’s in another variable $Tenants which I will be using shortly.

Earlier I mentioned that I knew from past experience that there were cmdlets in the MSOnline module that used TenantID as a parameter. How could I find out what those commands are? By running the command below. I reduced the output by using -Verb Get since I am going to be getting the number of mailboxes.

Scrolling through the list I see that Get-MSOLUser is one of the cmdlets that uses TenantID as a parameter and think that might be a good place to start. I take one of my Tenant ID’s in my $Tenants variable and run the command below.

I was hoping to see that there was a domain or client name property, but unfortunately there is not. So in order to figure that out I ran the commands below. What I am doing here is taking the first user that comes up for the tenant and getting their User Principal Name (UPN). Since each client’s users have the same UPN, this will be OK. After that I am splitting the UPN at the ‘@’ symbol and using that information for the domain. This is definitely not the only way to do this (or probably the best) but it worked for me.

Next I am creating a hashtable with my properties and then creating a new PSObject Object and saving this information to a .CSV file. Notice in the properties section that for the Domain I am having to use $Domain[1] because when it split the UPN at the ‘@’ symbol it split it into two pieces, the section before the ‘@’ and the section after it.

Finally, I have tied all this into a Function I am calling Get-PartnerMailboxes that will iterate through all my Tenant ID’s and create a .CSV listing the domain and total number of licensed users for each tenant.