简体   繁体   English

Powershell:循环遍历交换查询中的名称列表

[英]Powershell: looping through list of names from exchange query

My Goal is to apply a retention policy to newly created mailboxes. 我的目标是将保留策略应用于新创建的邮箱。 Details from previous article here . 上一篇文章的详细信息在这里

My Current Script code is located here for easy readability or below: 为了便于阅读,我的当前脚本代码位于此处或下方:

# 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"

When I echo the $NeedsRetentions i receive the list of AD users that i need to apply a retention policy to. 当我回显$NeedsRetentions我会收到需要对其应用保留策略的AD用户列表。 But for some reason, when i loop through the the variable list with the below loop it errors out saying that the -Identity is no valid. 但是由于某种原因,当我使用下面的循环遍历变量列表时,它会错误地指出-Identity无效。

ForEach ($NeedsRetention in $NeedsRetentions){
    set-mailbox -Identity $NeedsRetention -RetentionPolicy "PurgeDeletedItemsFolder_60days"
    }

To troubleshoot i reduced the loop to this in order to display the individual usernames being affected by the script: 为了进行故障排除,我简化了循环,以显示受脚本影响的单个用户名:

ForEach ($NeedsRetention in $NeedsRetentions){
[System.Windows.Forms.MessageBox]::Show($NeedsRetention)
}

Doing this causes a message box to show up for every line in the variable but the string seems to be empty and thus the default message shows up on the message box. 这样做会使变量中的每一行都显示一个消息框,但字符串似乎为空,因此默认消息显示在消息框上。

在此处输入图片说明

Any ideas why $NeedsRetention isn't passing correctly into my loop task? 为什么$NeedsRetention无法正确传递到我的循环任务中?

The problem is that you saved the result from a Format-* -cmdlet (in this case Format-Table by using the alias ft ). 问题是您从Format-* -cmdlet(在本例中为使用别名ft Format-Table )保存了结果。

Never save the results from Format-* cmdlets to a variable. 永远不要将结果从Format-* cmdlet保存到变量中。 Format-* -cmdlets are for GUI/visual presentation only and turns your useful mailbox-objects into special format-objects that are useless for anything else then console-/file-output. Format-* -cmdlet仅用于GUI /可视化显示,并将您有用的邮箱对象转换为特殊的格式对象,该格式对象对控制台/文件输出没有其他用途。

Modify: 修改:

$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) 

To: 至:

$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")}

If you want to output the $NeedsRetentions -objects to the console before continuing with modifying the mailboxes, then do that as a separate command (without saving the format-output). 如果要在继续修改邮箱之前将$NeedsRetentions -objects输出到控制台,则可以将其作为单独的命令执行(不保存format-output)。 Ex: 例如:

$NeedsRetentions | Format-Table -AutoSize

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM