{"id":207,"date":"2019-02-12T03:36:42","date_gmt":"2019-02-12T08:36:42","guid":{"rendered":"http:\/\/itblog.ldlnet.net\/?p=207"},"modified":"2019-02-12T04:12:30","modified_gmt":"2019-02-12T09:12:30","slug":"connect-to-all-powershell-modules-in-o365-with-one-script","status":"publish","type":"post","link":"https:\/\/itblog.ldlnet.net\/index.php\/2019\/02\/12\/connect-to-all-powershell-modules-in-o365-with-one-script\/","title":{"rendered":"Connect to all PowerShell Modules in O365 with one script"},"content":{"rendered":"\n<p>Let&#8217;s say you&#8217;re an admin that needs to connect to Office365 via PowerShell often. Now, there are many different websites or blogs that will show you how to connect to each session via PowerShell. That can cause a headache since you can end up having five different PowerShell sessions running in five different windows. You end up having to enter a username and password all those times, which can become time consuming.<\/p>\n\n\n\n<p>I want to show you here how to combine all those sessions into one script where, if you&#8217;re security is tight enough on your computer, you don&#8217;t even have to enter credentials. This way, you can click on one icon and pull up all the O365 PowerShell commands that you&#8217;ll need to manage your organization.<\/p>\n\n\n\n<p>First you need to download the following PowerShell Module Installation Files so that your PowerShell Database will have the correct modules installed:<\/p>\n\n\n\n<p class=\"has-small-font-size\"><a rel=\"noreferrer noopener\" href=\"https:\/\/go.microsoft.com\/fwlink\/p\/?LinkId=286152\" target=\"_blank\"><em><strong>Microsoft Online Service Sign-in Assistant for IT Professionals RTW<\/strong><\/em><\/a><strong><br><\/strong><a rel=\"noreferrer noopener\" href=\"https:\/\/go.microsoft.com\/fwlink\/p\/?linkid=236297\" target=\"_blank\"><em><strong>Windows Azure Active Directory Module for Windows PowerShell v2<\/strong><\/em><\/a><strong><br><\/strong><a rel=\"noreferrer noopener\" href=\"https:\/\/go.microsoft.com\/fwlink\/p\/?LinkId=255251\" target=\"_blank\"><em><strong>SharePoint Online Management Shell<\/strong><\/em><\/a><strong><br><\/strong><a rel=\"noreferrer noopener\" href=\"https:\/\/go.microsoft.com\/fwlink\/p\/?LinkId=532439\" target=\"_blank\"><em><strong>Skype for Business Online, Windows PowerShell Module<\/strong><\/em><\/a><\/p>\n\n\n\n<p>Next, we want to setup the CLI (Command Line Interface) to be too cool for school. I have learned it helps to have knowledge of how to customize the CLI window. You can do all of this in PowerShell ISE or Notepad, which ever you prefer. Here are the commands for the script that I use to setup the CLI:<\/p>\n\n\n\n<pre class=\"lang:PowerShell nums:False\" title=\"Setup the CLI for PowerShell\">#Set the PowerShell CLI. Make sure you have a directory named \"C:\\PowerShell\"\nset-location c:\\PowerShell\n$a = (Get-Host).UI.RawUI\n$a.BackgroundColor = \"black\"\n$a.ForegroundColor = \"yellow\"\n$a.WindowTitle = \"(Your Company Name) PowerShell for ALL O365 PowerShell Services\"\n$curUser= (Get-ChildItem Env:\\USERNAME).Value\nfunction prompt {\"(Your Company Name) O365 PS: $(get-date -f \"hh:mm:ss tt\")&gt;\"}\n$host.UI.RawUI.WindowTitle = \"(Your Comapny Name) O365 PowerShell &gt;&gt; User: $curUser &gt;&gt; Current Directory: $((Get-Location).Path)\"<\/pre>\n\n\n\n<p>Next, you want to set your Execution Policy and put in your credentials so that you won&#8217;t be prompted to enter the user credentials when you run the script. <br><br><strong>NOTE: MAKE SURE YOU KEEP YOUR SCRIPT SAFE AS THE CREDENTIALS ARE VISIBLE WITHIN THE SCRIPT IN PLAIN TEXT!<\/strong><br><br>You can, alternatively, set your script to prompt for credentials every time by using the following:<\/p>\n\n\n\n<p class=\"has-text-color has-small-font-size has-medium-pink-color\"><strong>$LiveCred = Get-Credential<\/strong><\/p>\n\n\n\n<p>Here is that part of the script:<\/p>\n\n\n<pre class=\"lang:PowerShell nums:False\" title=\"Setup Execution Policy and Credentials\">#Setup Execution Policy and Credentials using your Tenant Admin Credentials\nSet-ExecutionPolicy Unrestricted\n$user = \"tenantadmin@companyname.onmicrosoft.com\"\n$pass = \"adminpassword\"\n$secpass = $pass | ConvertTo-SecureString -AsPlainText -Force\n$LiveCred = New-Object System.Management.Automation.PSCredential -ArgumentList $user, $secpass<\/pre>\n\n\n\n<p>Now we get into the importing of the modules for each O365 service:<\/p>\n\n\n\n<p class=\"has-small-font-size\">Get the MSOnline Module:<\/p>\n\n\n<pre class=\"lang:PowerShell nums:False\" title=\"Get the MSOnline Module\">#Set up the powershell cmdlets for Office365\nWrite-Host \"Getting MSOnline Module\" -ForegroundColor Green\nGet-Module MSOnline<\/pre>\n\n\n\n<p class=\"has-small-font-size\">Connect to the MSOnline Service:<\/p>\n\n\n<pre class=\"lang:PowerShell nums:False\" title=\"Connect to the MSOnline Service\">#Connect the MS Online Service\nWrite-Host \"Connecting to the MSOnline Service\" -ForegroundColor Green\nConnect-MSOLService -Credential $LiveCred<\/pre>\n\n\n\n<p class=\"has-small-font-size\">Connect to Azure AD PowerShell:<\/p>\n\n\n<pre class=\"lang:PowerShell nums:False\" title=\"Connect to the Azure AD PowerShell\">#Connect to Azure Ad PowerShell\nWrite-Host \"Connecting to Azure AD PowerShell\" -ForegroundColor Green\nConnect-AzureAD -Credential $LiveCred<\/pre>\n\n\n\n<p class=\"has-small-font-size\">Connect to SharePoint Online PowerShell:<br><strong>NOTE &#8211; <\/strong><em><strong>MAKE SURE YOU CHANGE TO YOUR COMPANY NAME IN THE URL!!<\/strong><\/em><\/p>\n\n\n<pre class=\"lang:PowerShell nums:False\" title=\"Connect to the SharePoint Online PowerShell\">#Connect to SharePoint Online PowerShell\nWrite-Host \"Connecting to SharePoint Online through PowerShell\" -ForegroundColor Green\nImport-Module Microsoft.Online.SharePoint.PowerShell -DisableNameChecking\nConnect-SPOService -Url https:\/\/companyname-admin.sharepoint.com -credential $LiveCred<\/pre>\n\n\n\n<p class=\"has-small-font-size\">Connect to Exchange Online PowerShell:<\/p>\n\n\n<pre class=\"lang:PowerShell nums:False\" title=\"Connect to the Exchange Online PowerShell\">#Connect to Exchange Powershell\nWrite-Host \"Connecting to Exchange Online through PowerShell\" -ForegroundColor Green\n$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https:\/\/ps.outlook.com\/powershell\/ -Credential $LiveCred -Authentication Basic -AllowRedirection\nImport-PSSession $Session<\/pre>\n\n\n\n<p class=\"has-small-font-size\">Connect to Skype For Business Online PowerShell:<\/p>\n\n\n<pre class=\"lang:PowerShell nums:False\" title=\"Connect to the Skype For Business Online PowerShell\">#Connect the Skype For Business Online Powershell Module\nWrite-Host \"Connecting to Skype For Business Online through PowerShell\" -ForegroundColor Green \nImport-Module SkypeOnlineConnector\n$sfboSession = New-CsOnlineSession -Credential $LiveCred\nImport-PSSession $sfboSession<\/pre>\n\n\n\n<p class=\"has-small-font-size\">Connect to the Security &amp; Compliance PowerShell:<br><strong><em>NOTE &#8211; This one I still get &#8220;Access Denied&#8221; when trying to connect. I have looked for an answer to that issue, but have not found one. Please comment with a link if you have an answer so that I can update this script!<\/em><\/strong><\/p>\n\n\n<pre class=\"lang:PowerShell nums:False\" title=\"Connect to the Compliance and Security Center through PowerShell\">#Connect the Security & Compliance Center PowerShell Module\nWrite-Host \"Connecting to O365 Security & Compliance Online through PowerShell\"-ForegroundColor Green\n$SccSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https:\/\/ps.compliance.protection.outlook.com\/powershell-liveid -Credential $LiveCred -Verbose -Authentication Basic -AllowRedirection\nImport-PSSession $SccSession -Prefix cc<\/pre>\n\n\n\n<p class=\"has-small-font-size\">Lastly, put in a note to show that the PS load is completed:<\/p>\n\n\n<pre class=\"lang:PowerShell nums:False\" title=\"Final Text Showing Completion\">Write-Host \"Be sure to check for any connectivity errors!\" -ForegroundColor Green\nWrite-Host \"Also, Remember to run 'Get-PSSession | Remove-PSSession' before closing your PowerShell Window!\" -ForegroundColor Green\nWrite-Host \"Successfully connected to all O365 PowerShell Services for (CompanyName)!\" -ForegroundColor Green<\/pre>\n\n\n\n<p>So Here is the final script in its entirety:<\/p>\n\n\n\n<pre class=\"lang:PowerShell\" title=\"ConnectO365All.ps1\">#Set the PowerShell CLI. Make sure you have a directory named \"C:\\PowerShell\"\nset-location c:\\PowerShell\n$a = (Get-Host).UI.RawUI\n$a.BackgroundColor = \"black\"\n$a.ForegroundColor = \"yellow\"\n$a.WindowTitle = \"(Your Company Name) PowerShell for ALL O365 PowerShell Services\"\n$curUser= (Get-ChildItem Env:\\USERNAME).Value\nfunction prompt {\"(Your Company Name) O365 PS: $(get-date -f \"hh:mm:ss tt\")&gt;\"}\n$host.UI.RawUI.WindowTitle = \"(Your Company Name) O365 PowerShell &gt;&gt; User: $curUser &gt;&gt; Current Directory: $((Get-Location).Path)\"\n\n#Setup Execution Policy and Credentials using your Tenant Admin Credentials\nSet-ExecutionPolicy Unrestricted\n$user = \"tenantadmin@companyname.onmicrosoft.com\"\n$pass = \"adminpassword\"\n$secpass = $pass | ConvertTo-SecureString -AsPlainText -Force\n$LiveCred = New-Object System.Management.Automation.PSCredential -ArgumentList $user, $secpass\n\n#Set up the powershell cmdlets for Office365\nWrite-Host \"Getting MSOnline Module\" -ForegroundColor Green\nGet-Module MSOnline\n\n#Connect the MS Online Service\nWrite-Host \"Connecting to the MSOnline Service\" -ForegroundColor Green\nConnect-MSOLService -Credential $LiveCred\n\n#Connect to Azure Ad PowerShell\nWrite-Host \"Connecting to Azure AD PowerShell\" -ForegroundColor Green\nConnect-AzureAD -Credential $LiveCred\n\n#Connect to SharePoint Online PowerShell\nWrite-Host \"Connecting to SharePoint Online through PowerShell\" -ForegroundColor Green\nImport-Module Microsoft.Online.SharePoint.PowerShell -DisableNameChecking\nConnect-SPOService -Url https:\/\/companyname-admin.sharepoint.com -credential $LiveCred\n\n#Connect to Exchange Powershell\nWrite-Host \"Connecting to Exchange Online through PowerShell\" -ForegroundColor Green\n$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https:\/\/ps.outlook.com\/powershell\/ -Credential $LiveCred -Authentication Basic -AllowRedirection\nImport-PSSession $Session\n\n#Connect the Skype For Business Online Powershell Module\nWrite-Host \"Connecting to Skype For Business Online through PowerShell\" -ForegroundColor Green \nImport-Module SkypeOnlineConnector\n$sfboSession = New-CsOnlineSession -Credential $LiveCred\nImport-PSSession $sfboSession\n\n#Connect the Security &amp; Compliance Center PowerShell Module\nWrite-Host \"Connecting to O365 Security &amp; Compliance Online through PowerShell\"-ForegroundColor Green\n$SccSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https:\/\/ps.compliance.protection.outlook.com\/powershell-liveid -Credential $LiveCred -Verbose -Authentication Basic -AllowRedirection\nImport-PSSession $SccSession -Prefix cc\n\nWrite-Host \"Be sure to check for any connectivity errors!\" -ForegroundColor Green\nWrite-Host \"Also, Remember to run 'Get-PSSession | Remove-PSSession' before closing your PowerShell Window!\" -ForegroundColor Green\nWrite-Host \"Successfully connected to all O365 PowerShell Services for (CompanyName)!\" -ForegroundColor Green<\/pre>\n\n\n\n<p>Now you can create your icon for your desktop so that you can easily access the script. I would save the script to your Scripts directory. <\/p>\n\n\n\n<p class=\"has-small-font-size\">That will usually be <strong><em>C:\\Users\\&#8217;username&#8217;\\Documents\\WindowsPowerShell\\Scripts <\/em><\/strong>or wherever directory you choose.<\/p>\n\n\n\n<p class=\"has-small-font-size\">To start, right click the desktop and choose <em><strong>New > Shortcut<\/strong><\/em><br>In the <strong>Target<\/strong> Field, enter the following for your PowerShell Shortcut, pointing to the path of  your script:<br><br><strong><em>C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe -noexit -ExecutionPolicy Unrestricted -File &#8220;C:\\Users\\username\\Documents\\WindowsPowerShell\\Scripts\\ConnectO365All.ps1&#8221;<\/em><\/strong><br><br>Click on the <strong>Advanced<\/strong> button and check the box: <strong>Run As Administrator<\/strong><br>Under the <strong>General<\/strong> Tab, name your shortcut: <strong><em>(CompanyName) O365 All PowerShell<\/em><\/strong><br>Click <strong>OK<\/strong> to save the shortcut to your desktop.<\/p>\n\n\n\n<p>LAST BUT NOT LEAST, RUN THE FOLLOWING COMMAND BEFORE EXITING OR CLOSING YOUR POWERSHELL WINDOW. THIS WILL REMOVE ALL THE SESSIONS YOU&#8217;VE CONNECTED TO:<\/p>\n\n\n\n<p class=\"has-text-color has-small-font-size has-medium-pink-color\"><strong>Get-PSSession\u00a0|\u00a0Remove-PSSession<\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" style=\"text-align:center\">HAPPY SCRIPTING!<br>LEARN, DO, LIVE!<\/h2>\n\n\n\n<p class=\"has-small-font-size\">References:<br><strong><em><a rel=\"noreferrer noopener\" aria-label=\"Connect to all O365 Services in one PowerShell Window (opens in a new tab)\" href=\"https:\/\/docs.microsoft.com\/en-us\/office365\/enterprise\/powershell\/connect-to-all-office-365-services-in-a-single-windows-powershell-window\" target=\"_blank\">Connect to all O365 Services in one PowerShell Window<\/a><\/em><\/strong><br><strong><em><a rel=\"noreferrer noopener\" aria-label=\"How to connect to all O365 Services through PowerShell (opens in a new tab)\" href=\"https:\/\/www.itprotoday.com\/office-365\/how-connect-office-365-services-powershell\" target=\"_blank\">How to connect to all O365 Services through PowerShell<\/a><\/em><\/strong><br><strong><em><a href=\"https:\/\/gregbesso.wordpress.com\/office-365\/connecting-to-office-365-everything-via-powershell\/\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\"Connecting to Office 365 &quot;Everything&quot; via PowerShell (opens in a new tab)\">Connecting to Office 365 &#8220;Everything&#8221; via PowerShell<\/a><\/em><\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Let&#8217;s say you&#8217;re an admin that needs to connect to Office365 via PowerShell often. Now, there are many different websites or blogs<\/p>\n<p class=\"link-more\"><a class=\"myButt \" href=\"https:\/\/itblog.ldlnet.net\/index.php\/2019\/02\/12\/connect-to-all-powershell-modules-in-o365-with-one-script\/\">Read More<\/a><\/p>\n","protected":false},"author":1,"featured_media":209,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[48,4,2,3,22,16],"tags":[41,90,78,95,88,77,8,94,23,93],"class_list":["post-207","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-active-directory","category-exchange","category-general","category-powershell","category-sharepoint","category-windows","tag-active-directory","tag-azure","tag-exchange-onlilne","tag-msonline","tag-o365","tag-office365","tag-powershell","tag-security-and-compliance","tag-sharepoint","tag-skype-for-business","odd"],"_links":{"self":[{"href":"https:\/\/itblog.ldlnet.net\/index.php\/wp-json\/wp\/v2\/posts\/207","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itblog.ldlnet.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itblog.ldlnet.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itblog.ldlnet.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/itblog.ldlnet.net\/index.php\/wp-json\/wp\/v2\/comments?post=207"}],"version-history":[{"count":6,"href":"https:\/\/itblog.ldlnet.net\/index.php\/wp-json\/wp\/v2\/posts\/207\/revisions"}],"predecessor-version":[{"id":216,"href":"https:\/\/itblog.ldlnet.net\/index.php\/wp-json\/wp\/v2\/posts\/207\/revisions\/216"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/itblog.ldlnet.net\/index.php\/wp-json\/wp\/v2\/media\/209"}],"wp:attachment":[{"href":"https:\/\/itblog.ldlnet.net\/index.php\/wp-json\/wp\/v2\/media?parent=207"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itblog.ldlnet.net\/index.php\/wp-json\/wp\/v2\/categories?post=207"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itblog.ldlnet.net\/index.php\/wp-json\/wp\/v2\/tags?post=207"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}