使用 Get-ADGroup 时隐藏错误 [英] Hiding Errors When Using Get-ADGroup

查看:19
本文介绍了使用 Get-ADGroup 时隐藏错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个脚本,如果它不存在,它将建立一个新组.我正在使用 Get-ADGroup 来确保使用以下命令的组不存在:

I'm working on a script that will build a new group if it doesn't exist. I'm using Get-ADGroup to make sure the group doesn't exist using the following command:

$group = get-adgroup $groupName -ErrorAction:SilentlyContinue -WarningAction:SilentlyContinue 

但是当我这样做时,我收到以下错误(我从错误中删除了任何特定于域的数据):

But when I do I get the following error (I removed any domain specific data from the error):

Get-ADGroup : Cannot find an object with identity: '*group name*' under: '*domain*'.
At U:ScriptsWindowsCreate-FolderAccessGroup.ps1:23 char:24
+ $group = get-adgroup <<<< $groupName -ErrorAction:SilentlyContinue -WarningAction:SilentlyContinue
    + CategoryInfo          : ObjectNotFound: (y:ADGroup) [Get-ADGroup], ADIdentityNot
   FoundException
    + FullyQualifiedErrorId : Cannot find an object with identity: '' under: ''.,Microsoft.ActiveDirectory.Management.Commands.GetADGroup

我假设将 ErrorAction 和 WarningAction 设置为 SilentlyContinue 会阻止显示此错误,但事实并非如此.

I assumed setting ErrorAction and WarningAction to SilentlyContinue would keep this error from being displayed but it hasn't.

推荐答案

 try {get-adgroup <groupname>}
  catch  {
      <make new group>
     }

这篇关于使用 Get-ADGroup 时隐藏错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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