Create folders and filters with PowerShell - powershell

With the following command with my user admin and role impersonation can access to my account:
$UserCredential = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection
Import-PSSession $Session -DisableNameChecking -AllowClobber
Once the connection is made, can I switch to another account?
I am trying to create folders and filters to execute the commands using New-MailboxFolder but doesn't work directly from my account. It's possible?
Thanks

Yes you can change current user. You will create another script to do what you want to do as another user. Then in your current script use this:
$UserCredential = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection
Start-Process powershell.exe -Credential $UserCredential -NoNewWindow -ArgumentList "-noexit -command TheNewCreatedScriptFullPath.ps1"

Related

How do we fetch shared mailbox list in powershell using service account credentials

Following is the script I used to fetch the shared mailbox list by getting adminId and password. But I want to fetch the details using the service account, can anyone help me out
$Credentials = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $Credentials -Authentication Basic -AllowRedirection
Import-PSSession $Session -DisableNameChecking
Get-Mailbox -RecipientTypeDetails SharedMailbox -ResultSize:Unlimited | ft Name,WindowsEmailAddress

Accessing credential in the script executing from Invoke-Command

I am trying to execute the following command in PowerShell:
Invoke-Command -ComputerName <computerName> -FilePath script.ps1 -Credential $cred
Using Invoke-Command I am executing a PowerShell script "script.ps1" providing credentials in $cred.
Is there any way I can use the passed variable $cred in the above Invoke-Command, in the script again without asking a user about credentials?
i.e. something like below:
script.ps1:
New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $cred -Authentication Basic -AllowRedirection
You can have the script accept a credential parameter:
script.ps1:
Param([PSCredential]$Credential)
New-PSSession ... -Credential $Credential
and invoke it while passing $cred as an argument like this:
Invoke-Command -Computer $computer -FilePath script.ps1 -ArgumentList $cred -Credential $cred

The term 'remove-distributiongroupmember' is not recognized as the name of a cmdlet

What are the modules that has to be imported for this error in powershell ?
The term 'remove-distributiongroupmember' 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.
Connect to Exchange Online PowerShell
$UserCredential = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection
Import-PSSession $Session
#Your code here
Remove-PSSession $Session
Connect to Exchange servers using remote PowerShell
Note: Substitute mailbox01.contoso.com for your own exchange server.
$UserCredential = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://mailbox01.contoso.com/PowerShell/ -Authentication Kerberos -Credential $UserCredential
Import-PSSession $Session
#Your code here
Remove-PSSession $Session

Error with add user in a group using Add-MsolGroupMember

I am trying to add user in a group but I am getting error:
Add-MsolGroupMember -GroupObjectId $group1.ObjectId -GroupMemberType "User" -GroupMemberObjectId 077cf65b-4b9f-44e4-9f34-6c96a063a0df
As bunzab said, we should use Add-DistributionGroupMember to add member.
First, we should connect to exchange online powershell:
$UserCredential = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection
Import-PSSession $Session
After that, we can use this powershell to add member, like this:
More information about connect to exchange online powershell, please refer to this link.

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