【发布时间】:2021-05-14 08:44:21
【问题描述】:
我是 Powershell 新手,请多多包涵。
我正在尝试编写一个 powershell 脚本,该脚本将扫描 Exchange 2010 中的多个邮箱并将最后收到的日期写入现有的 CSV 文件。 由于服务器正在运行 Exchange 2010,它具有 PowerShell v2,因此我无法使用 PS v3 中可用的 -Append 参数,因此我尝试使用 out-file 和 Add-Content,但两者都失败并出现下面列出的错误第二次运行脚本。 我假设我在导入时做错了什么,或者在写入现有的 CSV 文件,但我不知道到底是什么。
# Path to the CSV file. This needs to be in place before the scrpt runs
$results = Import-CSV -Path 'C:\temp\numberofmails.csv' -Header "Mailbox","TotalItems","LastReceived",
# go through the mabList array and get the identity, number of items and last received date and time
Foreach($i in $mbxList){
$data = Get-MailboxFolderStatistics -Identity $i -IncludeOldestAndNewestItems | Where {$_.Name -match "Inbox"} |select identity,ItemsInFolder,NewestItemReceivedDate
$mbID=$data.identity
$numItems=$data.ItemsInFolder
$lastReceived=$data.NewestItemReceivedDate
#Append data to the CSV file using PS v2 so cannot use append as can be done in v3
$exp = $results
$exp.Mailbox = $mbID
$exp.TotalItems = $numItems
$exp.LastReceived = $LastReceived
#$exp | ConvertTo-CSV -NoTypeInformation | Select-Object -Skip 1| Out-File -Append 'C:\temp\numberofmails.csv'
$exp | ConvertTo-CSV -NoTypeInformation | Select-Object -Skip 1| Add-Content 'C:\temp\numberofmails.csv' -Encoding UTF8
}
第二次运行脚本时收到错误。
Property 'Mailbox' cannot be found on this object; make sure it exists and is settable.
At C:\Temp\ReceivedItemsv2.ps1:29 char:6
+ $exp. <<<< Mailbox = $mbID
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyAssignmentException
Property 'TotalItems' cannot be found on this object; make sure it exists and is settable.
At C:\Temp\ReceivedItemsv2.ps1:30 char:6
+ $exp. <<<< TotalItems = $numItems
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyAssignmentException
Property 'LastReceived' cannot be found on this object; make sure it exists and is settable.
At C:\Temp\ReceivedItemsv2.ps1:31 char:6
+ $exp. <<<< LastReceived = $LastReceived
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyAssignmentException
【问题讨论】:
标签: powershell exchange-server exchange-server-2010