简体   繁体   English

如何丢弃PowerShell ActiveDirectory cmdlet的错误?

[英]How to discard errors of PowerShell ActiveDirectory cmdlets?

I am writing a PowerShell script (in a PowerShell 5.1 environnement) and I need to list all users from groups set in a folder's permissions. 我正在编写PowerShell脚本(在PowerShell 5.1环境中),并且需要列出文件夹权限中设置的组中的所有用户。 But some groups are not relevant so when I try to Get-ADGroupMember on it, I've got an expected error. 但是某些组不相关,因此当我尝试在其上使用Get-ADGroupMember时,出现了预期的错误。

To discard this error, I tried the following : 要丢弃此错误,我尝试了以下操作:

Get-ADGroupMember Fake_Group -Server ad.example.com 2>&1 $null
Get-ADGroupMember Fake_Group -Server ad.example.com 2>&1 | Out-Null

But in both cases, the result is the same : error is displayed. 但是在两种情况下,结果都是相同的:显示错误。

get-aduser : Cannot find an object with identity: 'Fake_Group' under 'DC=example.com'.
At line:1 char:1
+ Get-ADGroupMember Fake_Group -Server ad.example.com 2>&1 | ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Fake_Group:ADGroup) [Get-ADGroupMember], ADIdentityNotFoundException
    + FullyQualifiedErrorId : ActiveDirectoryCmdlet:Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException,Microsoft.ActiveDirectory.Management.Commands.GetADGroupMember

So my question is : why is this error still displayed ? 所以我的问题是:为什么仍然显示此错误?

And then, how could I discard this error or is there a better way to list users from groups set in a folder's permissions than just try to Get-ADGroupMember on the whole result of Get-Acl even on no relevant object ? 然后,我怎么能抛弃此错误呢?还是有一种更好的方法从文件夹权限设置的组中列出用户,而不仅仅是对整个Get-Acl结果尝试Get-ADGroupMember甚至没有相关的对象?

Because Out-Null does nothing in this regard, you would need to use try/catch statements and might even need to add -ErrorAction Stop as not all errors in AD commands are terminating errors: 由于Out-Null在这方面没有执行任何操作,因此您将需要使用try / catch语句,甚至可能需要添加-ErrorAction Stop,因为并非AD命令中的所有错误都将终止错误:

Try{
    Get-ADGroupMember $GROUPNAME -Server $SEVRER -ErrorAction Stop
    #The group is found, do whatever you want here
}Catch{
    Write-Host "Some error occured"
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM