2

I want to avoid errors provoked by a null dsquery. I tried this:

try {
     dsquery user forestroot -samid $a[$i] | dsget user -email | Select-String '@' | select -Expand Line >> output.txt
}
catch [Exception] {
    return $_.Exception.Message
}

But I'm still getting:

dsget : dsget failed:'Target object for this command' is missing.
At ExpiringCertificates.ps1:35 char:49
+         dsquery user forestroot -samid $a[$i] | dsget user -email | Select-Strin ...
+                                                 ~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (dsget failed:'T...nd' is missing.:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

type dsget /? for help.

How should I handle it?

4
  • Are you certain you can pipe the result like you are trying to? dsquery is a command line tool and I doubt it writes object output to the pipeline like PowerShell cmdlets generally does. But of course, I might be missing something here. By the way, any special reasons you are not using the ActiveDirectory PowerShell module instead of dsquery? Is it a really old domain controller? With the cmdlets in the ActiveDirectory module you'd be working with objects the whole time instead of with output strings. Commented Apr 3, 2014 at 8:30
  • Yes @robert.westerlund it works. I tried to use ActiveDirectoy module however I didn't managed to query to the forestroot. I opened other post: stackoverflow.com/questions/22450817/… Commented Apr 3, 2014 at 8:40
  • It seems as though you have been given answers on how to query the forest root using the ActiveDirectory module in that question. Just read all the answers, you shouldn't provide further information on your question as answers, provide it as edits to your question and comment to an answer if it's related to the answer. Commented Apr 3, 2014 at 8:54
  • I tried everything they told me @robert.westerlund with no success and pasted the errors. I answered my own question twice because: a) formatting more clearly the error I got so that every reader could read it easyly. b) Solve my own question. Commented Apr 3, 2014 at 9:28

3 Answers 3

1

Try this:

try
{
    dsquery user forestroot -samid $a[$i] | dsget user -email | Select-String '@' | select -Expand Line >> output.txt
}
catch
{
    Write-Error -ErrorRecord $_
}

If $ErrorActionPreference is set to 'Stop', Write-Error will throw an exception and halt execution. Otherwise, it will print to the error stream and continue execution. This allows the caller to decide whether or not to continue on error and keeps you from having to set global variables in your scripts.

If you need to print and return the error message, use the -ErrorVariable parameter:

catch
{
    $errorVar = $null
    Write-Error -ErrorRecord $_ -ErrorVariable errorVar
    return $errorVar
}

Or, if you need to return the error message without printing it, add the "2>" redirect:

catch
{
    $errorVar = $null
    Write-Error -ErrorRecord $_ -ErrorVariable errorVar 2> $null
    return $errorVar
}
Sign up to request clarification or add additional context in comments.

Comments

1

dsquery and dsget are not PowerShell commands, so PowerShell looks at the standard error, and processes it, turning it into an ErrorRecord with a "NativeCommandError" exception attached to it, and then sends a text representation of that record to the standard output.

However, if you process the standard error yourself, you have a bit more flexibility:

dsquery user forestroot -samid $a[$i] | dsget user -email | Select-String '@' | select -Expand Line 2>&1
If ($_ -is [Management.Automation.ErrorRecord]) {
    $message = $_.Exception.Message
    # Do what you want with $message.
} Else {
    $_ >> output.txt
}

Comments

0

This might work for you:

try {
     $erroractionpreference = 'stop'
     dsquery user forestroot -samid $a[$i] | dsget user -email | Select-String '@' | select -Expand Line >output
}
catch [Exception] {
    return $_.Exception.Message
}

1 Comment

Hi! It partly works because it stops execution. What I need is to continue but show a controlled message such as: "User not found in AD"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.