Powershell 不允许使用空元素

Powershell 不允许使用空元素,powershell,Powershell,我似乎无法将任何内容导出到CSV。我做了一些浏览和阅读,但很难转换我的脚本 $allmailbox = Get-Mailbox -ResultSize 20 foreach ($Mailbox in $allmailbox) { Get-MailboxFolderPermission -Identity ($mailbox.alias+':\calendar') | Where { $_.User -like "Anonymous" -and

我似乎无法将任何内容导出到CSV。我做了一些浏览和阅读,但很难转换我的脚本

$allmailbox = Get-Mailbox -ResultSize 20
foreach ($Mailbox in $allmailbox) {
    Get-MailboxFolderPermission -Identity ($mailbox.alias+':\calendar') |
        Where {
            $_.User -like "Anonymous" -and
            $_.AccessRights -ne "None" -or
            $_.User -like "Default" -and
            $_.AccessRights -ne "None" -or
            $_.User -like "Default" -and
            $_.AccessRights -ne "AvailabilityOnly"
        } |
        select Identity, User, AccessRights
} | Export-Csv C:\CSVs\calstest.csv

如果这就是您要寻找的,尽管您必须添加反勾号,但我认为要将其视为一个完整的查询,不允许的空元素是指
foreach
末尾的
。循环的结构方式不允许使用管道。下面显示了一种可以工作的不同方法

下面的内容应包含在一个CSV文件中:

Get-Mailbox -ResultSize 20 | foreach {Get-MailboxFolderPermission -Identity $($_.Alias+":\calendar") |  Where {$_.User -like "Anonymous" -and $_.AccessRights -ne "None" -or $_.User -like "Default" -and $_.AccessRights -ne "None" -or $_.User -like "Default" -and $_.AccessRights -ne "AvailabilityOnly"}| select Identity,User,AccessRights} | Export-Csv C:\CSVs\calstest.csv -NoTypeInformation
我在没有
Where对象
的情况下测试了它,它成功导出。如果您没有收到任何可能需要的信息,请查看您的
Where
。从一个条件开始,根据需要增加

这是有效的:

$allmailbox = Get-Mailbox -ResultSize 500
$result = Foreach ($Mailbox in $allmailbox){Get-MailboxFolderPermission –Identity ($mailbox.alias+':\calendar') | Where {$_.User -like "Anonymous" -and $_.AccessRights -ne "None" -or $_.User -like "Default" -and $_.AccessRights -ne "None" -or $_.User -like "Default" -and $_.AccessRights -ne "AvailabilityOnly"} | select Identity,User,AccessRights}
$result | Export-CSV C:\CSVs\calstest.csv -NoType

“听起来您需要将查询合并到括号中,”张贴了建议的answerCheck运算符优先级。可能需要使用括号来明确指定比较。如果不将
ForEach($y中的x){}
循环封装在脚本块或子表达式中,则无法对其进行管道传输。要使代码正常工作,请将其修改为
。{ForEach($allmailbox中的mailbox){…}选择Identity,User,AccessRights}}}}导出Csv C:\CSVs\calstest.Csv-NoTypeInfo
(添加了
-NoTypeInfo
以摆脱
导出Csv
生成的讨厌的第一行垃圾)是的,我做了这样的事情{$result=ForEach($y中的x)|$result | Export CSV}等,它成功了,谢谢!在您的情况下,它会在CSV文件中返回此访问权限。“System.Collections.ObjectModel.Collection`1[Microsoft.Exchange.Management.StoreTasks.MailboxFolderAccessRight]”。信息技术不过,我找到了一种方法,将foreach赋值给一个新变量。非常感谢。
$allmailbox = Get-Mailbox -ResultSize 500
$result = Foreach ($Mailbox in $allmailbox){Get-MailboxFolderPermission –Identity ($mailbox.alias+':\calendar') | Where {$_.User -like "Anonymous" -and $_.AccessRights -ne "None" -or $_.User -like "Default" -and $_.AccessRights -ne "None" -or $_.User -like "Default" -and $_.AccessRights -ne "AvailabilityOnly"} | select Identity,User,AccessRights}
$result | Export-CSV C:\CSVs\calstest.csv -NoType