locked
Tratamento de erro em script para powershell RRS feed

  • General discussion

  • Bom dia amigos, sou iniciante em criar script para powershell, estou criando um script que envie uma mensagem para vários computadores de uma vez via msg. Acontece que quando o computador esta desligado ou não logado na rede aparece esse erro então gostaria de criar um try/catch para tratar o erro, mas não sei exatamente o que jogar como parâmetro no catch, alguém poderia me ajudar?

    msg.exe : Erro 1722 ao obter nomes de sessão
    Em linha:11 caractere:15
    +            msg <<<<  /server:$computador * /time:3600 "$mensagem" 
        + CategoryInfo          : NotSpecified: (Erro 1722 ao obter nomes de sessão:String) [], RemoteException
        + FullyQualifiedErrorId : NativeCommandError
     


    • Changed type Bill_Stewart Friday, July 27, 2018 7:55 PM
    • Moved by Bill_Stewart Friday, July 27, 2018 7:55 PM Not in English
    Monday, May 7, 2018 12:11 PM

All replies

  • Sorry but this forum is English only.

    -- Bill Stewart [Bill_Stewart]

    Monday, May 7, 2018 2:00 PM
  • English

    The solution is to use a simple Try{} Catch{}

    so the answer would be something like:

    Try{

    #What you want to try

    get-service | where{ $_.Status -eq "Started"}

    } catch{

    #this will catch the exception and by default is within a value object that is $_ so to enter the exception message is like this

    write-host "$_.Exception.Message" exit -1;

    }

    In Portugues (thank google translator):

    A solução é usar um simples Try {} Catch {}

    então a resposta seria algo como:



    • Edited by j0rt3g4 Wednesday, May 9, 2018 5:07 AM
    Wednesday, May 9, 2018 5:06 AM
  • Your Try/Catch example won't work as posted.  The command will never terminate to an exception.

    Carefully read the following for an explanation.

    help about_try_catch


    \_(ツ)_/

    Wednesday, May 9, 2018 5:16 AM
  • My example is just for show, it's not an exact cmdlet that will give an exception, of course, the point is not to get an exception in that point is just to add something where he can use the code where he is getting the error, not more. So please stop being so kindly about every answer I posted on this forum. 
    Tuesday, May 15, 2018 6:58 AM
  • My example is just for show, it's not an exact cmdlet that will give an exception, of course, the point is not to get an exception in that point is just to add something where he can use the code where he is getting the error, not more. So please stop being so kindly about every answer I posted on this forum. 

    It serves no purpose and will never be helpful.  I am posting this so you will consider learning PowerShell before giving misleading and incorrect answers.

    Bad information is never helpful.


    \_(ツ)_/

    Tuesday, May 15, 2018 7:02 AM