Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

powershell try catch statement

Posted on 2013-02-01
2
Medium Priority
?
767 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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

My attempt to use PowerShell and other great resources found online to simplify the deployment of Office 365 ProPlus client components to any workstation that needs it, regardless of existing Office components that may be needing attention.
In this post we will be converting StringData saved within a text file into a hash table. This can be further used in a PowerShell script for replacing settings that are dynamic in nature from environment to environment.
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ā€¦
Screencast - Getting to Know the Pipeline

610 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