Connect Office 365 Exchange Online through Powershell - powershell

I am trying to perform some operations on Exchange online(Office 365) through powershell.
First I created a new powershell session and exported the modules to local as "o365", so that on any later operation I no need to use Import-PsSession to download the required modules
$cred = Get-Credential
$s = New-PSSession -ConfigurationName "Microsoft.Exchange" -ConnectionUri "https://ps.outlook.com/powershell/" -Credential $cred -Authentication Basic -AllowRedirection
Export-PsSession -session $s -outputModule o365
Now, I am creating new session and importing the existing module "o365".
$cred = Get-Credential
$s = New-PSSession -ConfigurationName "Microsoft.Exchange" -ConnectionUri "https://ps.outlook.com/powershell/" -Credential $cred -Authentication Basic -AllowRedirection
Import-Module o365
Get-DistributionGroup
While running the command "Get-DistributionGroup", powershell prompts me to enter the office 365 credentials once again. Is it possible to avoid entering the credentials once again? I don't want to use Import-PsSession, since it takes more time.

It's prompting because you're asking it to each time. I would set up $cred by creating a new object.
#Initiate Get-Credential cmdlet and store inputs
$getcred = Get-Credential
$username = $getcred.UserName
$password = $getcred.Password
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $password
If you place the above in its own function, you wont have the issue of re-defining the $getcred variable. (That to most is obvious, but thought I'd cover that base)

Related

How to run PS1 file on a Server 2016 from a remote computer?

i wrote a script for Exchange Management Shell. i've saved the script on the server.
my question is: how can i run the script from a remote windows 10 Computer?
*regarding to server authentiication - it's O.K for me to write my server Credentials on the script
i've try this script, but it's asking for user and password every time and cannot exeute the file.
$UserCredential = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://mailserver/PowerShell/ -Authentication Kerberos -Credential $UserCredential
Import-PSSession $Session
Invoke-Command -ComputerName mailserver -FilePath \\mailserver\c$\Script\MPC3.ps1
EDIT:
i want to be able to open the following PS on my Windows computer:
Thanks for helping me !
I do not recommend to store credentials inside a script. But here a way of doing it:
$Username = 'username'
$Password = 'passwd' | ConvertTo-SecureString -AsPlainText -Force
$Credential = New-Object -TypeName 'System.Management.Automation.PSCredential' -ArgumentList $Username, $Password

Does Connect-MsolService -Credential no longer work?

I'm attempting to create the ability to connect to my o365 instance through the use of connect-msolservice. Im running into the issue that no matter what I try Im unable to use -Credential to feed it a username and password and avoid a prompt. This is a massive issue as there is literally no point in attempting to automate a process unless it automates the connection too.
Currently what I'm trying to use:
$AdminName = "admin email account"
$Pass = Get-Content "C:\test.txt" | ConvertTo-SecureString
$Cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $AdminName, $Pass
Import-Module MSOnline
Connect-MsolService -Credential $cred
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $cred -Authentication Basic -AllowRedirection
Import-PSSession $Session
Everywhere I check online tells me that this should be possible and for the life of me I cant find anything obvious that I'm doing incorrectly, albeit, all the posts and info I do find are about a year old. This leads me to the question, is this simply no longer supported? If not and hopefully this is the case, what am I doing incorrectly?

Remove-MsolGroupMember use exchange online to perform this operation

I get the error:
Remove-MsoLGroupMember : You cannot update mail-enabled groups using
this cmdlet. Use Exchange Online to perform this operation
But i have connected to a remote session and imported the exchange online module, why does it still throw error?
$credential = Get-Credential
$lyncSession = New-CsOnlineSession -Credential $credential
$exchangeSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri "https://outlook.office365.com/powershell-liveid/" -Credential $credential -Authentication "Basic" -AllowRedirection
Get-PSSession
Import-PSSession $exchangeSession
Remove-MsoLGroupMember -GroupObjectId $Group_GUID -GroupMemberType User -GroupmemberObjectId $GroupMembers.ObjectId
I was following this link to setup my remote connection to exchange, but i must be doing something wrong. https://technet.microsoft.com/en-us/library/dn362787(v=ocs.15).aspx
Use Remove-DistributionGroupMember instead if you are working with mail enabled security group as Remove-MsoLGroupMember works with regular security groups.
check this: https://technet.microsoft.com/en-us/library/aa998016(v=exchg.160).aspx

Creating remote powershell session to my O365 exchange tenant in Azure functions

I'm trying to write a powershell function on the new azure function service.
The basic stuff work but when I attempt to create a new remote session to my exchange O365 tenant, the function simply stops processing when getting to the New-PSSession line.
There is no problem to create a remote session in azure automate powershell.
Am I missing something here?
Get-PSSession | Remove-PSSession
$username = "xxxx"
$password = convertto-securestring "yyyyy" -asplaintext -force
$Creds = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $password
$Session = New-PSSession -ConnectionUri "https://outlook.office365.com/powershell-liveid/" -Credential $Creds -Authentication Basic -AllowRedirection
Import-PSSession -Session $Session

MSonline Active Directory Automatic login

I am currently using Powershell to create a script that will essentially add a user and license automatically instead of using the GUI. I am currently using MSOnline and MsolService. What I am trying to do is have it automatically login instead of bringing up the login window.
This the the Windows that pops up. I just need it to automatically fill in the credentials and continue.
This is the working code!
$User = “xxxxxx”
$Pass = “xxxxxx”
$Cred = New-Object System.Management.Automation.PsCredential($User,(ConvertTo-SecureString $Pass -AsPlainText -Force))
Import-Module MSOnline
Connect-MsolService -Credential $Cred
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $Cred -Authentication Basic -AllowRedirection
Import-PSSession $Session