我的目标是将保留策略应用于新创建的邮箱。上一篇文章here的详细信息。
我当前的脚本代码位于here,以便于阅读:
summa <- df %>% group_by(id) %>%
summarize(mondays = #numberof mondays,
tuesdays = #number of tuesdays,
.........)
当我回显# Get Start Time for script timer
$startDTM = (Get-Date)
#Authenticate using cached credentials or re-prompt for credentials.
if (Test-Path C:\temp\mycred.xml) {
$UserCredential = Import-CliXML C:\temp\mycred.xml}
else{
Get-Credential | Export-CliXml C:\temp\mycred.xml
$UserCredential = Import-CliXML C:\temp\mycred.xml}
#Connect to Exchange Server
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://munprdcasht04.exchange.com/PowerShell/ -Authentication Kerberos -Credential $UserCredential
Import-PSSession $Session
#returns alias' for mailboxes where creation date is <= 7 days and
#resides on "ABC" or "DEF" server and has no retention policy applied
$NeedsRetentions = (Get-Mailbox -ResultSize Unlimited| Where-Object {
($_.WhenCreated –ge ((Get-Date).Adddays(-7))) -and
(($_.ServerName -like "*munprdmbxa*") -or ($_.ServerName -like "*wauprdexa*")) -and
($_.retentionpolicy -ne "PurgeDeletedItemsFolder_60days")} |
ft -auto alias)
ForEach ($NeedsRetention in $NeedsRetentions){
set-mailbox -Identity $NeedsRetention -RetentionPolicy "PurgeDeletedItemsFolder_60days"
}
# Get End Time
$endDTM = (Get-Date)
# Echo Time elapsed
"Elapsed Time: $(($endDTM-$startDTM).totalseconds) seconds"
时,我收到了需要应用保留策略的AD用户列表。但由于某种原因,当我使用下面的循环遍历变量列表时,它会错误地说-Identity无效。
$NeedsRetentions
要进行故障排除,我将循环缩减到此以显示受脚本影响的各个用户名:
ForEach ($NeedsRetention in $NeedsRetentions){
set-mailbox -Identity $NeedsRetention -RetentionPolicy "PurgeDeletedItemsFolder_60days"
}
执行此操作会导致变量中的每一行显示一个消息框,但字符串似乎为空,因此消息框中会显示默认消息。
为什么ForEach ($NeedsRetention in $NeedsRetentions){
[System.Windows.Forms.MessageBox]::Show($NeedsRetention)
}
没有正确传递到我的循环任务中的任何想法?
答案 0 :(得分:1)
问题是您使用别名Format-*
保存了Format-Table
- cmdlet(在本例中为ft
)的结果。
永远不要将Format-*
cmdlet的结果保存到变量中。 Format-*
- cmdlet仅用于GUI /视觉呈现,并将您有用的邮箱对象转换为特殊的格式对象,除了控制台/文件输出之外的任何其他内容都无用。
修改:
$NeedsRetentions = (Get-Mailbox -ResultSize Unlimited| Where-Object {
($_.WhenCreated –ge ((Get-Date).Adddays(-7))) -and
(($_.ServerName -like "*munprdmbxa*") -or ($_.ServerName -like "*wauprdexa*")) -and
($_.retentionpolicy -ne "PurgeDeletedItemsFolder_60days")} |
ft -auto alias)
要:
$NeedsRetentions = Get-Mailbox -ResultSize Unlimited| Where-Object {
($_.WhenCreated –ge ((Get-Date).Adddays(-7))) -and
(($_.ServerName -like "*munprdmbxa*") -or ($_.ServerName -like "*wauprdexa*")) -and
($_.retentionpolicy -ne "PurgeDeletedItemsFolder_60days")}
如果要在继续修改邮箱之前将$NeedsRetentions
- 对象输出到控制台,请将其作为单独的命令执行(不保存格式输出)。例如:
$NeedsRetentions | Format-Table -AutoSize