Invoke-Command with Error Action and Error Variable

Welcome Forums General PowerShell Q&A Invoke-Command with Error Action and Error Variable

  • This topic has 7 replies, 3 voices, and was last updated 1 year ago by
    Participant
    .
Viewing 4 reply threads
  • Author
    Posts
    • #176083
      Participant
      Topics: 1
      Replies: 3
      Points: 1
      Rank: Member

      Hi All,

      I’m relatively new to Powershell and i’m having some trouble with the below script I have put together with some help from a tutorial video.

      What I’m trying to create is a script that captures the TPM chip details of the computers I feed in to the $computers variable . With the details i would like it to Output the successful results into a csv and any computers that fail to connect or has issues it writes to .txt file.

      The successful entries seemed to be caught in the csv, however no errors are being created, yet i can see lots in the shell for computers that are offline etc…

      Thank you in advance for any support and advice 🙂

      Thanks
      Anthony

    • #176122
      Participant
      Topics: 51
      Replies: 209
      Points: 860
      Helping Hand
      Rank: Major Contributor

      This worked for me….

    • #176155
      Senior Moderator
      Topics: 9
      Replies: 1309
      Points: 4,781
      Helping Hand
      Rank: Community Hero

      well, I would do it this way, leaving the error handling for each node expecting a tabular output as result

    • #176188
      Participant
      Topics: 1
      Replies: 3
      Points: 1
      Rank: Member

      Thank you both for your replies, i really appreciate it and its good to see two different methods to get the desired outcome.

      @Kvprasoon, i get an error for the script at line 17 of the script you kindly provided saying it is missing a catch or finally block. Do you know where this is best placed as i can see there are two try’s, but only one catch, so i can see the outcome of the script 🙂

      Thank you again
      Anthony

      • #176197
        Senior Moderator
        Topics: 9
        Replies: 1309
        Points: 4,781
        Helping Hand
        Rank: Community Hero

        oops, that was an editing issue. outer most try block is not required, removed now.

      • #176203
        Participant
        Topics: 1
        Replies: 3
        Points: 1
        Rank: Member

        That’s ok 🙂

        I have run the command and it is successfully exporting the TPM details of the machines it can connect to into the success.csv, however the failed/error ones appear to be vanishing into thin air? It shows the error in the shell saying it can’t connect to the machine, but doesnt seem to put the detail into the csv? Is there any further information I can provide which might help?

        Thanks
        Anthony

    • #176212
      Senior Moderator
      Topics: 9
      Replies: 1309
      Points: 4,781
      Helping Hand
      Rank: Community Hero

      my bad, I didnt read the question properly. Above code will not handle the connectivity error messages.

      try this (untested from my end)

      • #176383
        Participant
        Topics: 1
        Replies: 3
        Points: 1
        Rank: Member

        I corrected the spelling for ErrorAction, but i get the below error when i run this.

        Thanks
        Anthony

Viewing 4 reply threads
  • The topic ‘Invoke-Command with Error Action and Error Variable’ is closed to new replies.