?
Solved

powershell try catch statement

Posted on 2013-02-01
2
Medium Priority
?
766 Views
Last Modified: 2013-02-05
Im trying to see if I can use Try Catsch to move AD accounts. Sometines you will have accounts that already exist in the destination container. I can use Try Catch and I can see the accounts that cannot be moved. However, I cant seem to collect them so that I can re-process them in a different way
$aduser | % {

try {
   move-qadobject $_.dn -newparentcontainer $ou | out-null
}
catch {
   $problemusers += $_.dn
}

$problemusers is blank


try {
   move
0
Comment
Question by:bndit
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 71

Accepted Solution

by:
Chris Dent earned 1140 total points
ID: 38846618
Hmm don't use Try / Catch, they do the wrong thing for this really. That is, they trap unhandled exceptions and aren't the best way to catch output from CmdLets throwing terminating errors.

Perhaps try:
Move-QADObject $_.DN -NewParentContainer $ou -ErrorAction SilentlyContinue | Out-Null
# Test whethe the last command succeeded with the reserved variable $?.
# Negated with !, testing for $? set to False
If (!$?) {
  $problemusers += $_.dn
}

Open in new window

You could also check for something before moving, worth noting even if it's an approach you wish to use :)
If (!(Get-QADObject $_.Name -SearchRoot $OU)) {
  Move-QADObject $_.DN -NewParentContainer $OU | Out-Null
}

Open in new window

HTH

Chris
0
 
LVL 2

Author Closing Comment

by:bndit
ID: 38858158
Thanks for the clarification.
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
A brief introduction to what I consider to be the best editor for PowerShell.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question