Error handling in PowerShell provoked by a null dsquery











up vote
2
down vote

favorite












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?










share|improve this question
























  • 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.
    – Robert Westerlund
    Apr 3 '14 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/…
    – srbob
    Apr 3 '14 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.
    – Robert Westerlund
    Apr 3 '14 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.
    – srbob
    Apr 3 '14 at 9:28















up vote
2
down vote

favorite












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?










share|improve this question
























  • 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.
    – Robert Westerlund
    Apr 3 '14 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/…
    – srbob
    Apr 3 '14 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.
    – Robert Westerlund
    Apr 3 '14 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.
    – srbob
    Apr 3 '14 at 9:28













up vote
2
down vote

favorite









up vote
2
down vote

favorite











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?










share|improve this question















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?







powershell






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 5:18









Peter Mortensen

13.3k1983111




13.3k1983111










asked Apr 3 '14 at 7:52









srbob

3181212




3181212












  • 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.
    – Robert Westerlund
    Apr 3 '14 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/…
    – srbob
    Apr 3 '14 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.
    – Robert Westerlund
    Apr 3 '14 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.
    – srbob
    Apr 3 '14 at 9:28


















  • 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.
    – Robert Westerlund
    Apr 3 '14 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/…
    – srbob
    Apr 3 '14 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.
    – Robert Westerlund
    Apr 3 '14 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.
    – srbob
    Apr 3 '14 at 9:28
















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.
– Robert Westerlund
Apr 3 '14 at 8:30




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.
– Robert Westerlund
Apr 3 '14 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/…
– srbob
Apr 3 '14 at 8:40






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/…
– srbob
Apr 3 '14 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.
– Robert Westerlund
Apr 3 '14 at 8:54






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.
– Robert Westerlund
Apr 3 '14 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.
– srbob
Apr 3 '14 at 9:28




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.
– srbob
Apr 3 '14 at 9:28












3 Answers
3






active

oldest

votes

















up vote
0
down vote



accepted










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
}





share|improve this answer























  • 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"
    – srbob
    Apr 3 '14 at 8:23


















up vote
1
down vote













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
}





share|improve this answer






























    up vote
    1
    down vote













    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
    }





    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














       

      draft saved


      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f22831141%2ferror-handling-in-powershell-provoked-by-a-null-dsquery%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      0
      down vote



      accepted










      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
      }





      share|improve this answer























      • 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"
        – srbob
        Apr 3 '14 at 8:23















      up vote
      0
      down vote



      accepted










      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
      }





      share|improve this answer























      • 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"
        – srbob
        Apr 3 '14 at 8:23













      up vote
      0
      down vote



      accepted







      up vote
      0
      down vote



      accepted






      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
      }





      share|improve this answer














      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
      }






      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Nov 11 at 5:09









      Peter Mortensen

      13.3k1983111




      13.3k1983111










      answered Apr 3 '14 at 8:17









      saw

      343




      343












      • 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"
        – srbob
        Apr 3 '14 at 8:23


















      • 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"
        – srbob
        Apr 3 '14 at 8:23
















      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"
      – srbob
      Apr 3 '14 at 8:23




      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"
      – srbob
      Apr 3 '14 at 8:23












      up vote
      1
      down vote













      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
      }





      share|improve this answer



























        up vote
        1
        down vote













        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
        }





        share|improve this answer

























          up vote
          1
          down vote










          up vote
          1
          down vote









          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
          }





          share|improve this answer














          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
          }






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 11 at 5:11









          Peter Mortensen

          13.3k1983111




          13.3k1983111










          answered Apr 3 '14 at 8:44









          skataben

          1,26011421




          1,26011421






















              up vote
              1
              down vote













              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
              }





              share|improve this answer



























                up vote
                1
                down vote













                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
                }





                share|improve this answer

























                  up vote
                  1
                  down vote










                  up vote
                  1
                  down vote









                  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
                  }





                  share|improve this answer














                  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
                  }






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 11 at 5:16









                  Peter Mortensen

                  13.3k1983111




                  13.3k1983111










                  answered Apr 3 '14 at 8:47









                  Mark Bertenshaw

                  4,81122134




                  4,81122134






























                       

                      draft saved


                      draft discarded



















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f22831141%2ferror-handling-in-powershell-provoked-by-a-null-dsquery%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Xamarin.iOS Cant Deploy on Iphone

                      Glorious Revolution

                      Dulmage-Mendelsohn matrix decomposition in Python