A guide on simplifications for O365 admins - III - Visual Studio Code profiles

In the last post I tried to show how to make PowerShell life easier. You can read it here

This post is picking up the same issue, but the tool is a bit different. All cloud admins may know that Visual Studio Code supports PowerShell scripts and in addition to that makes life easier by having a source control for those scripts via GitHub oder TFS. But still you may encounter the problem, that you have to login to every cloud service seperately in every VSCode session, which can be annoying. But as VSCode supports PowerShell you can apply the same set of functions mentioned in my previous post in VSCode as well.

All you have to do is open up VSCode create a new PSScript and run the following code:

Test-Path $Profile

If you receive a False, then you can set up your profile with the following commands:

Notepad $Profile

Now this opens up a blank profile, which needs to adapted in order to implement functions, which allow you to login to all cloud services, like we did in the PS DIE. Therefore, you can paste the following code:

Import-Module MSOnline

function Connect-Tenant1
{
    [Cmdletbinding()]
    param
    (
        [Microsoft.PowerShell.EditorServices.Extensions.EditorContext]$context
    )
    $CloudUsername = "admin@tenant1.onmicrosoft.com"
  $File =  "C:\Users\adm.sbi\Documents\Tools\PowerShell\tenant1_pw.txt"
  $CloudCred=New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $CloudUsername, (Get-Content $File | ConvertTo-SecureString)
  #Connect to Office 365
  Connect-MsolService -Credential $CloudCred
  #EXO
    $SessionEX = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $CloudCred -Authentication Basic -AllowRedirection
  Import-PSSession $SessionEX
    Add-AzureAccount -Credential $CloudCred
  Select-AzureSubscription -SubscriptionName "AzureSubscription"
  Login-AzureRmAccount -Credential $CloudCred
  Import-Module "C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.Online.SharePoint.PowerShell.dll"
  Connect-SPOService -Url https://tenant1-admin.sharepoint.com -credential $CloudCred
  Clear-Host
}

Now hit save and restart VSCode. Now if you start typing connect-ten you will get the suggestions Connect-Tenant1, which is pretty cool, isn’t it?

/static/img/VSCode.png

And this is it, now you’re good to go with your new PS environment. If you have further questions, you can of course contact my via Twitter, mail or other services…