EMS Powershell V2 如何附加到 CSV
EMS Powershell V2 how to append to CSV
我是 Powershell 的新手,所以请多多包涵。
我正在尝试编写一个 powershell 脚本,该脚本将扫描 Exchange 2010 中的多个邮箱并将最后收到的日期写入现有的 CSV 文件。
由于服务器是 运行ning 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
$results
是具有 Mailbox
、TotalItems
和 LastReceived
属性的对象集合。您可以选择向该集合添加一个新对象,也可以过滤其中一个对象并更改其属性值。您似乎想要创建一个新对象,将其转换为 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
我是 Powershell 的新手,所以请多多包涵。
我正在尝试编写一个 powershell 脚本,该脚本将扫描 Exchange 2010 中的多个邮箱并将最后收到的日期写入现有的 CSV 文件。 由于服务器是 运行ning 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
$results
是具有 Mailbox
、TotalItems
和 LastReceived
属性的对象集合。您可以选择向该集合添加一个新对象,也可以过滤其中一个对象并更改其属性值。您似乎想要创建一个新对象,将其转换为 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