Powershell DirectoryService object error neither caught nor trapped

后端 未结 2 1488
长发绾君心
长发绾君心 2021-01-22 17:22

This is part of my script:

Trap {Write-Output \'Authentication Error trapped\'}
Try {New-Object System.DirectoryServices.DirectoryEntry $strDistinguishedName,$st         


        
2条回答
  •  深忆病人
    2021-01-22 17:29

    I had a similar error and the comment from @TessellatingHeckler helped me. Piping the result to Out-Null fixed it for me. So you can try the following:

    Trap {Write-Output 'Authentication Error trapped'}
    Try {New-Object System.DirectoryServices.DirectoryEntry $strDistinguishedName,$strUsername,$strPlainPassword -ErrorAction stop | Out-Null}
    Catch{Write-Output 'Authentication Error catched'}
    Write-Output 'Script has not trapped nor catched the error but continued'
    

提交回复
热议问题