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

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

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?

Trying to run a script, using Exchange Online, connection works, Get-UnifiedGroup works, but Set doesn't exist?

So I'm trying to run a script that hides certain email addresses from a client's outlook addressbook.
$Username = "xxx"
$Password = "xxx" | ConvertTo-SecureString -AsPlainText -Force
$UserCredential = New-Object System.Management.Automation.PSCredential -ArgumentList $Username,$Password
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection
Import-PSSession $Session
Get-UnifiedGroup -ResultSize Unlimited | Where-Object {$_.SharePointSiteUrl} | select PrimarySmtpAddress, SharePointSiteUrl | Set-UnifiedGroup -HiddenFromAddressListsEnabled $true
Remove-PSSession $Session
This will hide all the email addresses that have a sharepoint URL connected to them from creating team sites. Anyway, Get-UnifiedGroup works, but when I do the following:
Set-UnifiedGroup -HiddenFromAddressListsEnabled $true
it says this:
Set-UnifiedGroup: The term 'Set-UnifiedGroup' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
I've searched google but can't seem to find a proper solution to my problem... I also did the following commands, to no avail:
Set-ExecutionPolicy RemoteSigned
and
Install-Module MSOnline
I don't know where to look anymore, I'm by not means used to writing Powershell...
Looking at the Microsoft documentation "Set-UnifiedGroup -HiddenFromAddressListsEnabled" does not take pipeline input.
(https://learn.microsoft.com/en-us/powershell/module/exchange/users-and-groups/set-unifiedgroup?view=exchange-ps)
I would suggest trying:
$Username = "xxx"
$Password = "xxx" | ConvertTo-SecureString -AsPlainText -Force
$UserCredential = New-Object System.Management.Automation.PSCredential -ArgumentList $Username,$Password
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection
Import-PSSession $Session
$groups = Get-UnifiedGroup -ResultSize Unlimited | Where-Object {$_.SharePointSiteUrl} | select PrimarySmtpAddress, SharePointSiteUrl
foreach ($group in $groups){
set-unifiedgroup -identity $group.PrimarySmtpAddress -HiddenFromAddressListsEnabled $true
}
Remove-PSSession $Session
With regards to "Set-UnifiedGroup: The term 'Set-UnifiedGroup' is not recognized as the name of a cmdlet"
You will need to add the appropriate role in the exchange admin centre to be able to access the cmdlet.
You will need to be an administrator to make these changes;
https://outlook.office365.com/ecp/?rfr=Admin_o365
Login and select "Permissions" on the left.
I think "Organization Management" should give you the cmdlet you are looking for.
Edit that role and add the user that you are using for the above powershell script to the members list.
This will usually take 30-60 minutes to become effective.

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

Connect Office 365 Exchange Online through 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)