【问题标题】:EMS Powershell V2 how to append to CSVEMS Powershell V2 如何附加到 CSV
【发布时间】: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


    【解决方案1】:

    $results 是具有MailboxTotalItemsLastReceived 属性的对象的集合。您可以选择向该集合添加新对象,也可以过滤其中一个对象并更改其属性的值。您似乎想创建一个新对象,将其转换为 CSV,然后将其附加到文本文件中。

    # Run this code inside of the foreach loop
    # Create new object with desired properties
    $exp = "" | Select-Object Mailbox,TotalItems,LastReceived
    
    # Set property values
    $exp.Mailbox = $mbID
    $exp.TotalItems = $numItems
    $exp.LastReceived = $LastReceived
    
    # Append object to CSV file
    $exp | ConvertTo-CSV -NoTypeInformation |
        Select-Object -Skip 1 |
            Add-Content 'C:\temp\numberofmails.csv' -Encoding UTF8
    

    【讨论】:

    • 非常感谢对问题进行了分类并使其正常工作
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-02-17
    • 1970-01-01
    • 2011-05-22
    • 2011-10-27
    • 2018-05-11
    • 1970-01-01
    • 2013-02-05
    相关资源
    最近更新 更多