Staying connnected to O365 in between script executions

This forum can be browsed by the general public. Posting is limited to current SAPIEN license holders with active maintenance and does not offer a response time guarantee.
Forum rules
DO NOT POST LICENSE NUMBERS, ACTIVATION KEYS OR ANY OTHER LICENSING INFORMATION IN THIS FORUM.
Only the original author and our tech personnel can reply to a topic that is created in this forum. If you find a topic that relates to an issue you are having, please create a new topic and reference the other in your post.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 5 years and 10 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
User avatar
TCNinja
Posts: 7
Last visit: Sun Jun 02, 2019 12:06 am

Staying connnected to O365 in between script executions

Post by TCNinja »

I am running a trial of Powershell Studio 2018 and would like to know how to create a connection to Office 365 and remain connected rather than executing connection code in my script like I can do on Windows PowerShell ISE. I don't want to be authenticating to O365 every time I test run my script.
User avatar
mxtrinidad
Posts: 399
Last visit: Tue May 16, 2023 6:52 am

Re: Staying connnected to O365 in between script executions

Post by mxtrinidad »

What's the problem with PowerShell Studio? You can highlight the connection code, right-click and "Run Selection in Console". This will get you connected. Then highlight the next set of code, right-click and "Run Selection in Console".

You shouldn't have any problem with it. Just make sure the console you're using is at same platform your O365 need to execute: either Windows PowerShell 64bit or 32bit.

:)
User avatar
TCNinja
Posts: 7
Last visit: Sun Jun 02, 2019 12:06 am

Re: Staying connnected to O365 in between script executions

Post by TCNinja »

Thank you for your response. I actually got it to work once somehow where Run from Console (Ctrl + F8) did work and maintained my O365 sessions but I am not able to replicate it again. I can't even get the O365 login to pop up when Running from Console. When I run with F5, it works but obviously it doesn't maintain the connection after the script ends, so I would need to log in every time.

Here's the most basic of connecting to MSOLService and the login window will not pop up when running from console....
  1. Import-Module MSOnlineExtended
  2. Connect-MsolService
Is there a setting or something that will allow the O365 login window to pop up when running from console?
User avatar
mxtrinidad
Posts: 399
Last visit: Tue May 16, 2023 6:52 am

Re: Staying connnected to O365 in between script executions

Post by mxtrinidad »

Interesting! Last time I use O365 with PowerShell I only use code to connect to it. I never use the popup windows for it.
If I can recall, unless it has change since that time, I remember O365 connecting thru a PSSession which remote connect to it.

Let me do some research, I do have O365. Also, let see if one of my colleagues wants to jump in and assist.
:)
User avatar
mxtrinidad
Posts: 399
Last visit: Tue May 16, 2023 6:52 am

Re: Staying connnected to O365 in between script executions

Post by mxtrinidad »

OK!!

There's some missing information.

Which version of Windows PowerShell you are on? And, is it 32 or 64 bit?
Make sure you on the correct Windows PowerShell environment.

Now, the Connect-MSolService will start the windows popup. Check if its behind an application, because it won't show in from of PowerShell Studio.

Another thing I notice. You're importing module MSOnlineExt but you'll need MSOnline too.

But, the import module is not needed if you're using Windows PowerShell 5.x, because all modules are automatically imported as you execute the commands.

Everything looks good on my side. I'm connected!
:)
User avatar
TCNinja
Posts: 7
Last visit: Sun Jun 02, 2019 12:06 am

Re: Staying connnected to O365 in between script executions

Post by TCNinja »

OK,

I made some serious progress here... It's not perfect but it seems to work.

First let's discuss the goal. I want to be able to have an active connection to MSOLService and O365 Exchange so when I run my scripts during development, I won't have to login every time. As you may know, you can only have 3 PSSessions on Exchange so you have to be adamant in closing them or else you will get locked out and will need to wait for them to time out before reconnecting again.

The first problem was getting to Connect to MSOL Service and popping up a credential window. I fixed that by starting PowerShell Studio as Administrator.

The second issue is getting PowerShell Studio's Intellisense to provide me with Exchange Online's command options while coding. It's hard because you cannot import the Exchange Module into PowerShell.... but actually.... you can. Here's how I did it.

I opened the standard Windows PowerShell ISE and ran the following code:
  1. $UserCredential = Get-Credential
  2. $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection
  3. Import-PSSession $Session
I entered my creds, and since I am an MFA user, I used a combination of my UPN and an App Password that I generated from my O365 profile. Once authenticated, you see that gray bar downloading and importing the Exchange PowerShell Module. When that's done, you will the temp filename of the module. Search for it. It's actually a folder containing 3 files in the C:\Users\<UserName>\AppData\Local\Temp directory. Copy it to another directory as it will be deleted at the end of your session.
In PowerShell Studio, go to the Import Module window, click on Import Module and navigate to the folder, and select .psd1 file. The import takes a while so don't assume your computer froze. Just wait it out. After it's done, click on Build Cache, then close the window.
Now if you type "Get-Mailbox" in your code, you should now see the intellisense options which will make coding against Exchange easier.

Now I have a connection script that I can run and connect me to MSOL and Exchange and stay connected while I code my admin scripts.
  1. #Import-Module MSOnlineExtended
  2. #Import-Module tmp_4e2zx2wv.ngn
  3. Connect-MsolService
  4. $UserCredential = Get-Credential
  5. $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection
  6. Import-PSSession $Session
  7.  
BTW, those Import-Module commands screw things up so I commented them out. Is there an option where when I enter a command, it does not automatically prefix the code with those Import-Module commands?
Last edited by TCNinja on Fri May 18, 2018 3:06 pm, edited 1 time in total.
User avatar
mxtrinidad
Posts: 399
Last visit: Tue May 16, 2023 6:52 am

Re: Staying connnected to O365 in between script executions

Post by mxtrinidad »

Good! I only have O365 and not Exchange (you miss to say that). PowerShell Studio can work with remote modules at the console only (the same technique you already know).

My O365 MSOnline connectivity never got dropped, so it stay connected until I clear the connection.

But then again, I'm Admin in my system. So, there might be some set time out.

:)
User avatar
TCNinja
Posts: 7
Last visit: Sun Jun 02, 2019 12:06 am

Re: Staying connnected to O365 in between script executions

Post by TCNinja »

Yes it does work with remote modules, however you do not get the intellisense features. i.e. if you type an Exchange command such as "Get-Mailbox", it did not show any of drop down options for the command. It was quite annoying.

To clarify, our O365 is composed of E3 w/EMS or as they call it now, O365 E3 and we sync AzureAD to our local AD domain. Exchange, Skype, and SharePoint are all in the cloud. So when doing some admin scripting, I've been known to access local AD, MSOL, and Exchange all the same time. This will come in handy for on-boarding and off-boarding scripts that I would like to create rather than having my team doing it manually. Fun times ahead!

I would love to see your script that you mentioned earlier if you care to share it.

Thank you for your help.
User avatar
mxtrinidad
Posts: 399
Last visit: Tue May 16, 2023 6:52 am

Re: Staying connnected to O365 in between script executions

Post by mxtrinidad »

Sure thing!

It's a simple code that help automate connection as long as you're using it for yourself. Of, course you could be very creative with PowerShell with other technologies (ie. SQLServer).

You did a good job with the other piece.

Code: Select all

## - O365 - Credential automation to prevent popup Windows:
$MyUserName = "UserName";
$MyPassword = ConvertTo-SecureString '$Pwd01Here!' -asplaintext -force;
$MyCred = new-object -typename System.Management.Automation.PSCredential `
					 -argumentlist $MyUserName, $MyPassword;

Connect-MsolService -Credential $MyCred

## - Execute commands to test connection:
Get-MsolCompanyInformation
Get-MsolUser
Get-MsolSubscription
Get-MsolDomain
:)
User avatar
TCNinja
Posts: 7
Last visit: Sun Jun 02, 2019 12:06 am

Re: Staying connnected to O365 in between script executions

Post by TCNinja »

Thanks again but I may have spoke too soon. The most basic issue still does not work regularly and I can't figure out why.

A simple one command does not fire up the credentials screen.

Try just running "Connect-MsolService" with Ctrl+F8 so it runs from the Console. It's supposed to bring up the Microsoft login window. I got it to work before, but when I close out of everything and try it again, it doesn't. Even when I start PowerShell Studio as Administrator.

What's funny is that it works fine when I run it from the traditional Windows PowerShell Console (which is 5.1 64-bit, BTW).

I don't get it... it makes me wonder if it's a bug. I'm running the latest Sapien PowerShell Studio 2018. It's driving me nuts.
This topic is 5 years and 10 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.