How to suppress PowerShell error message during variable assignment

Susanta Dutta

Is there any way I can suppress this error. Instead of this ugly and long error, I would like to capture return code value($?) to determine success or failure

    PS C:\> $str ="<p> Hi </p>"
    PS C:\> $data = [xml]$str
    PS C:\> $?
    True
    PS C:\>
    PS C:\> $str ="<p> Hi <p>"
    PS C:\> $data = [xml] $str
    Cannot convert value "<p> Hi <p>" to type "System.Xml.XmlDocument". Error: "Unexpected end of file has occurred. The following elements are
    not closed: p, p. Line 1, position 11."
    At line:1 char:1
    + $data = [xml] $str
    + ~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidArgument: (:) [], RuntimeException
        + FullyQualifiedErrorId : InvalidCastToXmlDocument

    PS C:\> $data = [xml] $str 2> $null
    Cannot convert value "<p> Hi <p>" to type "System.Xml.XmlDocument". Error: "Unexpected end of file has occurred. The following elements are
    not closed: p, p. Line 1, position 11."
    At line:1 char:1
    + $data = [xml] $str 2> $null
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidArgument: (:) [], RuntimeException
        + FullyQualifiedErrorId : InvalidCastToXmlDocument

    PS C:\>
    PS C:\> $?
    False
    PS C:\>
Tim Ker

Place a try catch around the command

try {$data = [xml] $str } catch {}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to suppress OpenCV error message

From Dev

How to suppress [no test files] message on go test

From Dev

Suppress error message in R

From Dev

Jetbrains: Suppress unresolved variable error (sometimes)

From Dev

PowerShell: How can I suppress the error if file alreadys exists for "mkdir" command?

From Dev

Suppress supplementary error message from unit test

From Dev

How to suppress R startup message?

From Dev

How to suppress Undefined index error?

From Dev

How to fully suppress an error when invoking powershell with a command?

From Dev

how to capture command error message in variable for if block

From Dev

matlab - suppress error message backtrace

From Dev

JAX-RS : Suppress Error Message

From Dev

How to suppress the error message when dividing 0 by 0 using np.divide (alongside other floats)?

From Dev

How to suppress error message of a command?

From Dev

PostgreSQL - How to suppress query statement message

From Dev

tryCatch suppress error message

From Dev

PHP heredoc assignment to a variable error

From Dev

Suppress error window on catastrophic powershell process failure

From Dev

Suppress error message in R

From Dev

PowerShell: How can I suppress the error if file alreadys exists for "mkdir" command?

From Dev

How to fully suppress an error when invoking powershell with a command?

From Dev

How to suppress QXcbConnection: XCB error

From Dev

Not able to suppress the error message using /dev/null

From Dev

Fatal Error Message during Compiling

From Dev

How to suppress certain error message in 'find' command?

From Dev

How to suppress error messages in zsh?

From Dev

lint error Useless assignment to variable

From Dev

How to suppress File in use alert message

From Dev

Suppress "Error during loading" message in standalone Jasmine 3.1 browser report

Related Related

  1. 1

    How to suppress OpenCV error message

  2. 2

    How to suppress [no test files] message on go test

  3. 3

    Suppress error message in R

  4. 4

    Jetbrains: Suppress unresolved variable error (sometimes)

  5. 5

    PowerShell: How can I suppress the error if file alreadys exists for "mkdir" command?

  6. 6

    Suppress supplementary error message from unit test

  7. 7

    How to suppress R startup message?

  8. 8

    How to suppress Undefined index error?

  9. 9

    How to fully suppress an error when invoking powershell with a command?

  10. 10

    how to capture command error message in variable for if block

  11. 11

    matlab - suppress error message backtrace

  12. 12

    JAX-RS : Suppress Error Message

  13. 13

    How to suppress the error message when dividing 0 by 0 using np.divide (alongside other floats)?

  14. 14

    How to suppress error message of a command?

  15. 15

    PostgreSQL - How to suppress query statement message

  16. 16

    tryCatch suppress error message

  17. 17

    PHP heredoc assignment to a variable error

  18. 18

    Suppress error window on catastrophic powershell process failure

  19. 19

    Suppress error message in R

  20. 20

    PowerShell: How can I suppress the error if file alreadys exists for "mkdir" command?

  21. 21

    How to fully suppress an error when invoking powershell with a command?

  22. 22

    How to suppress QXcbConnection: XCB error

  23. 23

    Not able to suppress the error message using /dev/null

  24. 24

    Fatal Error Message during Compiling

  25. 25

    How to suppress certain error message in 'find' command?

  26. 26

    How to suppress error messages in zsh?

  27. 27

    lint error Useless assignment to variable

  28. 28

    How to suppress File in use alert message

  29. 29

    Suppress "Error during loading" message in standalone Jasmine 3.1 browser report

HotTag

Archive