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?
dsqueryis 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 theActiveDirectoryPowerShell module instead of dsquery? Is it a really old domain controller? With the cmdlets in theActiveDirectorymodule you'd be working with objects the whole time instead of with output strings.ActiveDirectorymodule 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.