首页 > 解决方案 > PowerShell 哈希表问题 - office 365

问题描述

我需要在 O365 中为大约 80 个用户设置 OOF 消息。

我找到了一个 cmdlet Set-MailboxAutoReplyConfiguration,我可以使用它来自动化该过程,它看起来不错,但我可能遗漏了一些东西。

这是代码:

$usersfile = import-csv "C:\Users\Out Of office bulk\Users.csv"



$setmailbox = @{
'Identity' = $usersfile.UserPrincipalName
'AutoReplyState'  = 'Scheduled'
'externalaudience' = 'all'
'InternalMessage' = 'I am not here'
'ExternalMessage' = 'I am not here'
'StartTime' = '01/02/2020 01:00:00'
'EndTime' = '02/02/2020 23:00:00'
}

Set-MailboxAutoReplyConfiguration @setmailbox

我的问题是 Identity 参数,

当我运行 $setmailbox

我可以看到它从 csv 文件中显示了正确的 UPN:

Name                           Value                                                                                                                                                                                                                                                          
----                           -----                                                                                                                                                                                                                                                          
AutoReplyState                 Scheduled                                                                                                                                                                                                                                                      
externalaudience               all                                                                                                                                                                                                                                                            
Identity                       {blah@blah.com, blah2@blah.com}                                                                                                                                                                                                                       
StartTime                      01/02/2020 01:00:00                                                                                                                                                                                                                                            
EndTime                        02/02/2020 23:00:00                                                                                                                                                                                                                                            
InternalMessage                I am not here                                                                                                                                                                                                                                                  
ExternalMessage                I am not here                                                                                                                                                                                                                                                  

但是当我运行脚本时,我收到了这个错误:

Cannot process argument transformation on parameter 'Identity'. Cannot convert the "System.Collections.ArrayList" value of type "System.Collections.ArrayList" to type "Microsoft.Exchange.Configuration.Tasks.MailboxLocationIdParameter".
    + CategoryInfo          : InvalidData: (:) [Set-MailboxAutoReplyConfiguration], ParameterBindin...mationException
    + FullyQualifiedErrorId : ParameterArgumentTransformationError,Set-MailboxAutoReplyConfiguration
    + PSComputerName        : outlook.office365.com

我试图改变这一点:

'Identity' = $usersfile.UserPrincipalName

几乎任何事情,它没有工作。

非常感谢你的帮助。

PS:

我知道我可以做这样的事情:


Import-Csv 'C:\Users.csv' | ForEach-Object {
$user = $_."UserPrincipalName"
Set-MailboxAutoReplyConfiguration `
 -Identity $user -AutoReplyState Scheduled -StartTime "07/10/2018 01:00:00" ` 
 -EndTime "7/15/2018 23:00:00" -InternalMessage "I am not here" -ExternalMessage "I am not here."
}

但出于实践目的,我更喜欢使用哈希表来更好地理解它。

解决方案:感谢@Lee_Dailey,我没有注意到identity参数每次只接受一个用户,所以多用户的解决方案是使用foreach-object.

标签: powershelloffice365

解决方案


推荐阅读