试图用Add-QADGroupMember捕获错误 [英] trying to catch error with Add-QADGroupMember

查看:140
本文介绍了试图用Add-QADGroupMember捕获错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个脚本来批量添加CSV用户,然后从另一个用户添加组。



它的工作正常,除了一些组无法添加(使用AD MMC时我得到访问被拒绝 - 我们必须得到一个不同的组来为我们添加它们)。问题是Add-QADGroupMember默认失败。我想以某种方式捕获错误并列出我必须由不同组添加的组。



我尝试过尝试/ catch,但它没有t / b>

这是现在的代码:

  $ users = Import-Csv .\UserList.csv 

foreach($ user in $ users){
$ SameAs = $ user.SameAs
$ UserGroups =(Get-QADUser $ SameAs).MemberOf
foreach($ Group in $ UserGroups){
Add-QADGroupMember $ group -Member $ user.SamAccountName | Out-Null
}
}

我只是无法让它抛出错误或感谢任何帮助。

解决方案

您必须将错误反应设置为停止。



我与PowerCLI和所有命令都有同样的问题。



尝试这样:

  Add-QADGroupMember $ group -Member $ user.SamAccountN ame -ErrorAction Stop | Out-Null 

或者您可以使用

$设置ErrorActionPreference全局b
$ b

  $ ErrorActionPreference =停止


I'm writing a script to bulk add users from a CSV, then add groups from another user.

It's working fine, except that some groups i'm not able to add (I get access denied when using the AD MMC - we have to get a different group to add them for us). The problem is that Add-QADGroupMember silently fails. I would like to somehow catch the error and list the groups that I have to get added by the different group.

I've tried try/catch, but it doesn't work... i'm at a loss.

Here is the code at the moment:

$users = Import-Csv .\UserList.csv

foreach ($user in $users) {
$SameAs = $user.SameAs
$UserGroups = (Get-QADUser $SameAs).MemberOf
foreach ($group in $UserGroups) {
Add-QADGroupMember $group -Member $user.SamAccountName |Out-Null
}
}

I'm just not able to get it to throw an error or exception when it fails to add a group.

Thanks for any help.

解决方案

You have to set the erroraction to "stop".

I had the same problem with PowerCLI and all commands from there.

Try it so:

Add-QADGroupMember $group -Member $user.SamAccountName -ErrorAction Stop |Out-Null

or you can set the ErrorActionPreference global with

$ErrorActionPreference = "Stop"

这篇关于试图用Add-QADGroupMember捕获错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆