I am using the following Syntax, to Migrate user`s and create O365 mailboxes in our organization:
# Mailbox Migration Script
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010;
$UserCredential = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection
Import-PSSession $Session
Enable-RemoteMailbox -Identity user.name -PrimarySmtpAddress user.name#company.com -RemoteRoutingAddress user.name#company365.mail.onmicrosoft.com
sleep 30
# After the initial script has done running, run the following:
Get-RemoteMailbox user.name|Set-RemoteMailbox -EmailAddressPolicyEnabled:$true
Everything works ok , but what i would like to do is to convert this, so the data is being read from a CSV file instead so that the actual wont have to be touched.
Im guessing this should be with the import-csv, unfortunately i don`t know what the rest of the syntax should be.
The exact data i need to acquire via csv is the following:
-Identity user.name
-PrimarySmtpAddress user.name#company.com
-RemoteRoutingAddress user.name#company365.mail.onmicrosoft.com
Each part of the data should be acquired from a column in the CSV.
Please assist with creating this script.
Thanks a bunch , in advance to all.
You're right -- Import-CSV is the ticket here.
$ExchangeData = Import-CSV -path C:\MyExchangeData.csv
# Access the data to IMPORT each user.
# Assumes the CSV has "Email, UserName, RemoteEmail" as headers.
foreach ($user in $ExchangeData) {
Enable-RemoteMailbox -Identity $user.UserName -PrimarySmtpAddress $user.Email -RemoteRoutingAddress $user.RemoteEmail
}
Related
I have to create a Powershell script that assigns a new main SMTP address to users of a csv file. They also need some other addresses. Besides that, those users need to change their company name. I am really new to all that stuff and tried to cobble some stuff together which wouldn´t work and it´s driving me crazy. :(
If you need more info please tell me. :)
Here is the code I´ve tried so far:
$Cred = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell -Credential $Cred -Authentication Basic -AllowRedirection
Import-PSSession $Session
Connect-AzureAD -Confirm
#Assigning addresses & company
Import-CSV "C:\CSVPowershell\userstest.csv" | ForEach {
Set-Mailbox $_.Identity -EmailAddresses #{add= $_.MainEmailAddress}
Set-AzureADUser -ObjectId $_.Identity -CompanyName $_.Company
}
#close
Remove-PSSession $Session
Please check the below workaround:
CSV file
UserName, Mailform1, Mailform2
aaa test, atest#companyname.com , atest#companyname_Dept.com
bbb test, btest#companyname.com , btest#companyname_Dept.com
ccc test, ctest#companyname.com , ctest#companyname_Dept.com
PowerShell Script
Note: In your code, you are missing the operation add. Either you have to use Add / Remove in your script to perform the related operation.
Import-CSV "C:\Users\Admin\UserEmailAddress.csv" | ForEach
{
# Changing the Main Email address into your required Email.
#Adding Multiple Email addresses (here 2 Email addresses)
Set-Mailbox $_.UserName -EmailAddresses #{add= $_.Mailform1, $_.Mailform2}
}
Suppose you are trying to add only one SMTP address to User follow the below:
CSV File
UserName, Mailform1
aaa test, atest#companyname.com
bbb test, btest#companyname.com
ccc test, ctest#companyname.com
PowerShell Script
Import-CSV "C:\Users\Admin\UserEmailAddress.csv" | ForEach
{
# Changing the Main Email address into your required Email.
Set-Mailbox $_.UserName -EmailAddresses #{add= $_.Mailform1}
}
Refer here for more information
I'm trying to automate a powershell script which gathers data from O365. I've got a special limited user setup with the privileges required on O365 and also with local logon allowed on the server so that I can "run-as" that user (which I do for all the scripts below. I have verified different, expected errors when running as other users).
The script works fine interactively when credentials are set like this and the session opened:
$cred = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell -Credential $cred -Authentication Basic –AllowRedirection
However, if I create the credentials file for automation with:
Get-Credential | Export-Clixml -Path C:\batch\${env:USERNAME}_cred.xml
And then access from the script via:
$cred = Import-Clixml -Path C:\batch\${env:USERNAME}_cred.xml
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell -Credential $cred -Authentication Basic –AllowRedirection
The credential file load appears to succeed. I then get "Access Denied" on the session open, and then of course the rest of the script fails due to the session being null. I'm cutting and pasting the password in all cases (plus have tried many, MANY times including hand typing) so I don't think it's a simple typo issue. Seems more like something I'm fundamentally misunderstanding about powershell. Ultimately I'd like to not just have the credentials automated, but also have it run from task scheduler if there's any special settings above and beyond that I also need.
I don't see anything wrong from your code from PowerShell perspective. I have tested the way you are creating credentials within a company domain and I was able to create new session by importing credential XML file that was created by exporting the credentials the way you did. I then assume it might be MS Exchange related.
I can suggest alternatives for you to try:
# First we need to get the encrypted password:
$TempCred = Get-Credential
# provide credentials to the prompt
# now the encryption to be saved in a file
$TempCred.Password | ConvertFrom-SecureString | Set-Content C:\mypass.txt
This was the encrypted version of your password is saved as a text.
In your automation script you can now do this:
$username = "yourusername"
$password = Get-Content C:\mypass.txt | ConvertTo-SecureString
$cred = New-Object System.Management.Automation.PsCredential($username, $password)
$session = New-PSSession -Credential $cred .....
I am not sure if this works in your case, it worked in my company domain. Once again it worked for me the XML version too. I am just providing alternatives to try if you are not keen to find out as to why the XML way did not work.
I was able to get this working, in my environment at least, by including a call to Import-PSSession:
$Credential = Import-Clixml -Path D:\Modules\O365Credentials.xml
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $Credential -Authentication Basic -AllowRedirection
Import-PSSession $Session -DisableNameChecking
Get-Mailbox
Does the account in question have MFA enabled? If so, you might try this.
This script:
Downloads Exchange Online Remote PowerShell Module
Installs Exchange Online PowerShell Module
Connects Exchange Online PowerShell using MFA
Or, you can perform these manually. More information, including a detailed walk-through, is available here:
https://o365reports.com/2019/04/17/connect-exchange-online-using-mfa/
Community,
I have a script I pieced together online, that allows me to add a domain/sender to the block list via the Spam filter in Exchange Online. Currently, the script is written for one tenant. I would like it to loop through about 5 tenants, each with different credentials.
I was told I need to create PSCredential Objects, but I'm unsure of how to go about this. Or where to place the code.
Script:
# First we need credentials to use to connect to O365
Write-Host "Enter your O365 Global Administrator credentials"
$UserCredential = Get-Credential
Then we need to define the PS session to connect to O365
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid -Credential $UserCredential -Authentication Basic -AllowRedirection
Now we Open the Session
Import-PSSession $Session -AllowClobber
Prompt for vars
$domainlist = Read-Host -Prompt 'Domain(s) to add to Block list (press ENTER if none, use single space between entries): '
$addresslist = Read-Host -Prompt 'Email address to add to Block list (press ENTER if none, use single space between entries): '
$domains = $domainlist -split " "
$addresses = $addresslist -split " "
Add Domains to list, if any
if (!$domainlist) {
Write-Host "No domains to add...skipping"
} else {
Write-Host "Adding domain name(s) to Default spam Block list...."
Set-HostedContentFilterPolicy -Identity Default -BlockedSenderDomains #{Add=$domains}
}
Add email addresses to list, if any
if (!$addresslist) {
Write-Host "No addresses to add...skipping"
} else {
Write-Host "Adding email address(es) to Default spam Block list...."
Set-HostedContentFilterPolicy -Identity Default -BlockedSenders #{Add=$addresses}
}
Close the Session or bad things happen!!!
Remove-PSSession $Session
When you have to provide credentials in non-interactive mode, you can create a PSCredential object in the following way.
$secpasswd = ConvertTo-SecureString "PlainTextPassword" -AsPlainText –Force
$mycreds = New-Object System.Management.Automation.PSCredential ("username", $secpasswd)
Use it as part of a loop, and you should be able to create PSSesions automatically.
But the rest of your script is still interactive, and it still needs your input.
You could refer to the following link:
PowerShell – How to create a PSCredential object
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.
I am building a script to assign calendar permissions to specific people. I do this often enough to want a script. This is what i have so far.
#authenticate into the office 365 account
$LiveCred = Get-Credential
#Create new session
$Session = New-PSSession -ConfigurationName Microsoft.Exchange `
-ConnectionUri https://ps.outlook.com/powershell/ `
-Credential $LiveCred `
-Authentication Basic `
-AllowRedirection
#Import Session
Import-PSSession $Session
#Ask for user information
$UserCal = Read-Host -Prompt "Who's calandar are we sharing?"
$HostCal = Read-Host -Prompt "Who's needs access?"
$AccessCal = Read-Host -Prompt "What kind of access?"
#Set access for user
Add-MailboxFolderPermission -Identity ${$UserCal}:\calendar `
-User $HostCal `
-AccessRights $AccessCal
I believe the issue is with the ${$UserCal}:\calendar How do i go about using a variable in this fashion?
FYI I am pretty new to Powershell and scripting. Thanks for any help!
Found the answer to my question after doing much more research!
${$UserCal}:\calendar
Should be
$UserCal`:\calendar
All fixed and working perfect!