为 50 个用户启用电子邮件转发的 PowerShell 脚本

PowerShell script to enable email forwarding for 50 users

这是代码,我需要导入 CSV 并启用从 office 365 转发到其他电子邮件地址

$creds = Get-Credential

$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ `
                         -Credential $creds -Authentication Basic -AllowRedirection

Import-Module MSOnline

Connect-MsolService -Credential $creds

Import-PSSession $Session





$emails = Import-CSV "C:\Users\Lev\Desktop\Email.csv"

ForEach($mail in $emails)
{

Set-Mailbox $mail.Work_email -ForwardingSmtpAddress $mail.home_email -DeliverToMailboxAndForward $true 

}

我修复了代码,现在这是一个有效的代码。

我的评论作为正确答案:

你需要换行

Set-Mailbox -Identity $email.Work_email -DeliverToMailboxAndForward $true $email.home_email -ForwardingSMTPAddress $true

Set-Mailbox -Identity $email.Work_email -DeliverToMailboxAndForward $true -ForwardingSMTPAddress $email.home_email

参见 Set-Mailbox

中的示例 1