我的目標是將保留策略應用於新創建的郵箱。以前的文章here的詳細信息。Powershell:從交換查詢中遍歷名稱列表
我當前的腳本代碼位於here易於讀取或以下:
# 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"
當我附和$NeedsRetentions
我收到我需要申請保留策略,以AD用戶的列表。但由於某種原因,當我通過下面的循環遍歷變量列表時,它錯誤地指出-Identity無效。
ForEach ($NeedsRetention in $NeedsRetentions){
set-mailbox -Identity $NeedsRetention -RetentionPolicy "PurgeDeletedItemsFolder_60days"
}
要解決我以顯示各個用戶名受着腳本中的循環減少到這樣的:
ForEach ($NeedsRetention in $NeedsRetentions){
[System.Windows.Forms.MessageBox]::Show($NeedsRetention)
}
這樣做會導致一個消息框顯示爲在可變每一行但字符串似乎是空的,因此默認消息顯示在消息框中。
任何想法,爲什麼$NeedsRetention
不正確地傳遞到我的循環任務?
您存儲在'NeedsRetentions'中的對象是郵箱,您嘗試傳遞給''-identity'的變量可能是字符串。我沒有這個模塊,所以我不能測試,但你可以給這個鏡頭'set-mailbox -Identity($ NeedsRetention).identity -RetentionPolicy「PurgeDeletedItemsFolder_60days」' –
@Kai Zhao這很有道理,但是當我做此更改我收到變量列出的每個郵箱的以下錯誤'無法將參數綁定參數'身份',因爲它爲空。 + CategoryInfo:InvalidData:(:) [設置郵箱],ParameterBindingValidationException」 + FullyQualifiedErrorId:ParameterArgumentValidationErrorNullNotAllowed,設置郵箱 + PSComputerName:munprdcasht04.exchange.com' – mttp1990
'-identity'需要的名字嗎?什麼'get-mailbox | get-member'看起來像?應該是您可以使用的物業。 –