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?
powershell
add a comment |
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?
powershell
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 theActiveDirectory
PowerShell module instead of dsquery? Is it a really old domain controller? With the cmdlets in theActiveDirectory
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 theActiveDirectory
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
add a comment |
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?
powershell
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
powershell
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 theActiveDirectory
PowerShell module instead of dsquery? Is it a really old domain controller? With the cmdlets in theActiveDirectory
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 theActiveDirectory
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
add a comment |
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 theActiveDirectory
PowerShell module instead of dsquery? Is it a really old domain controller? With the cmdlets in theActiveDirectory
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 theActiveDirectory
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
add a comment |
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
}
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
add a comment |
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
}
add a comment |
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
}
add a comment |
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
}
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
add a comment |
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
}
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
add a comment |
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
}
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
}
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
add a 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"
– 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
add a comment |
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
}
add a comment |
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
}
add a comment |
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
}
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
}
edited Nov 11 at 5:11
Peter Mortensen
13.3k1983111
13.3k1983111
answered Apr 3 '14 at 8:44
skataben
1,26011421
1,26011421
add a comment |
add a comment |
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
}
add a comment |
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
}
add a comment |
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
}
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
}
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
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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 theActiveDirectory
PowerShell module instead of dsquery? Is it a really old domain controller? With the cmdlets in theActiveDirectory
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