Invoke-Command with Try/Catch and adding to an external array

Welcome Forums General PowerShell Q&A Invoke-Command with Try/Catch and adding to an external array

Viewing 18 reply threads
  • Author
    Posts
    • #275274
      Participant
      Topics: 9
      Replies: 30
      Points: 129
      Rank: Participant

      How would I add a computer to an array within an Invoke-Command cmdlet also utilizing try/catch statement?

      I have the following which isn’t behaving as I’m expecting:

      • The If/Else loop is being ignored and each time I run it, it just runs the If section regardless.
      • The Try/Catch appears to be working for execution of the If command errors but the “ErrorAction Stop” argument appears to be terminating the script on the first WinRM connection error it hits rather than logging and continuing with the rest.
      • Nothing is being added to the $ExecutionIssues variable on failure.

      Am I missing something obvious?

      • This topic was modified 1 month, 2 weeks ago by jshizzle14. Reason: Code formatting incorrect
      • This topic was modified 1 month, 2 weeks ago by jshizzle14. Reason: Removed dodgy formatting
      • This topic was modified 1 month, 2 weeks ago by jshizzle14. Reason: Corrected formatting
      • This topic was modified 1 month, 2 weeks ago by jshizzle14.
      • This topic was modified 1 month, 2 weeks ago by grokkit.
    • #275346
      Participant
      Topics: 17
      Replies: 1951
      Points: 3,988
      Helping Hand
      Rank: Community Hero

      Please fix the code in the post. You should be using PSObjects to return the information you need. There was not an -ErrorAction Stop on the Remove action. In this scenario, you are going to get back the computername and one of three statuses:

      Edit: Also noticed that your Get and Remove are using different paths. The GET ‘system.webServer/defaultDocument/files/add’ and in the remove you are not using ‘add’ anywhere. Has this been tested on a local IIS server before you run it on other servers?

      • This reply was modified 1 month, 2 weeks ago by Rob Simmers.
      • #276630
        Participant
        Topics: 9
        Replies: 30
        Points: 129
        Rank: Participant

        Hi Rob,

        Yes, the Get and Remove are meant to be different as the command to check if a file exists is not the same command used for removing/adding files. These commands have been tested and work as expected.

        Just looking at your code and think if I try and simplify my target it may help as still can’t see how I can get the computername added to a variable which can be used externally from the the Invoke-Command script block.

        I just need the computer to be added to the array for manipulation later. I know you mention that I need to return the item using pscustomobject but can’t see how this would aid in adding to the array?

    • #275364
      Participant
      Topics: 5
      Replies: 252
      Points: 999
      Helping Hand
      Rank: Major Contributor

      I’m struggling to read your post.  Make sure you are doing code blocks for each code section.  Repost and maybe I can follow it.

    • #275550
      Participant
      Topics: 9
      Replies: 30
      Points: 129
      Rank: Participant

      I’ve tried to update the formatting a number of times but every time I do the thread disappears for most the day and then when I can access it again, the formatting is exactly the same again. I’ll try again but this is painful….

      Thanks for the suggestion Rob, looking good. I have found another solution as it goes which simply stores all the errors in the $errmsg variable and outputs them to log at the end. This does actually work well but have no control over the formatting so your suggestion looks much better thanks. I’ll let you know how I get on.

    • #276705
      Participant
      Topics: 9
      Replies: 706
      Points: 2,837
      Helping Hand
      Rank: Community Hero

      The site definitely has issues.. BUT if you read the formatting guide and then take your time to format it correctly you’ll have less issues. If your post disappears, please just put a message to the mods in the tech support section, they will release it fairly quickly.

      • #277095
        Participant
        Topics: 9
        Replies: 30
        Points: 129
        Rank: Participant

        It’s not that I’m unsure how to format, it’s that the code PowerShell formatting option is simply missing from the RTF bar above for me sometimes.

      • #277128
        Participant
        Topics: 4
        Replies: 425
        Points: 748
        Helping Hand
        Rank: Major Contributor

        If the button is missing, you can either switch to Text view and use the CRAYON button or refresh the page a few times.  Usually after two or three refreshes the <> comes back for me.

    • #277944
      Participant
      Topics: 9
      Replies: 30
      Points: 129
      Rank: Participant

      Ok, I’m now trying to return information from the Invoke-Command using custom objects. I tried using Rob’s method but servers running old versions of PowerShell weren’t outputting the message as expected.

      The below seems to give me better results, however, still have a few issues:

      • The catch section doesn’t appear to be returning anything in the $results variable
      • How do I go about assigning the computers, depending on result, to variables now I have the information outside of the Invoke-Command block? I’m thinking of using an If/Else statement based on keywords in the Message value string but not sure how to check against this as $results.message doesn’t appear to work.
    • #278001
      Participant
      Topics: 5
      Replies: 252
      Points: 999
      Helping Hand
      Rank: Major Contributor

      I would remove your pipe after closing out the Invoke-Command script block.  With this in place you are assigning a formatted table to $results instead of the actual objects you want.

       

      • This reply was modified 1 month, 1 week ago by Mike R..
    • #278010
      Participant
      Topics: 9
      Replies: 30
      Points: 129
      Rank: Participant

      Ah yes that makes sense thanks. I can now call my data using $results.message which helps a great deal. Any ideas on why my catch doesn’t appear to be catching though?

    • #278037
      Participant
      Topics: 17
      Replies: 1951
      Points: 3,988
      Helping Hand
      Rank: Community Hero

      [pscustomobject] type accelerator was available in version 3. If you are running v2, you should seriously consider updating those servers to 5.1 for security reasons alone. As Mike R assisted, you can use a New-Object -TypeName PSObject -Property $myHashTable to support older versions.

    • #278052
      Participant
      Topics: 5
      Replies: 252
      Points: 999
      Helping Hand
      Rank: Major Contributor

      What are your indications that it is not working?  Are you getting an error message or is the script terminating?

      What is the purpose of the try/catch.  It appears the only possible terminating error in your try block would be the Get-Service…  Are you just trying to determine if that service is installed?  If so, you don’t need a try/catch block to do that.

       

       

    • #278055
      Participant
      Topics: 9
      Replies: 30
      Points: 129
      Rank: Participant

      That’s the problem, I’m not in the position to be able to update PowerShell on these servers so need to filter them out in order to just work on the ones I can.

      I can’t see any previous reference to New-Object -TypeName PSObject -Property $myHashTable in any of the replies above. Maybe I’m missing something?

      Regardless, I’m good to go other than the Catch simply not catching now and just can’t see why it’s not working.

    • #278064
      Participant
      Topics: 17
      Replies: 1951
      Points: 3,988
      Helping Hand
      Rank: Community Hero

      The accelerator can’t be used, so you need to do it like this:

    • #278073
      Participant
      Topics: 5
      Replies: 252
      Points: 999
      Helping Hand
      Rank: Major Contributor

      BTW, I really have not paid a lot attention to what you are trying to do, just getting your code working, but now that I look at it, is seems you are just trying to get the PS major version and determine if a service is running on remote servers.  If that’s all it is, I would avoid the whole creating a message to return from the remote machine and just collect the data.  You can analyze it however you want from the local machine after.  Like this:

       

    • #278079
      Participant
      Topics: 9
      Replies: 30
      Points: 129
      Rank: Participant

      Thanks Rob. I’m actually working on a different part of the script now but the subject is still relevant. Here’s my actual code with your suggestion so it’s clear what I’m trying to achieve:

      This simply shows errors in the console but doesn’t catch them in a controlled way still. I’ve purposely left out the ComputerName property for all the statements as will simply use PSComputerName.

      • This reply was modified 1 month, 1 week ago by jshizzle14. Reason: Amended code
    • #278106
      Participant
      Topics: 9
      Replies: 30
      Points: 129
      Rank: Participant

      BTW, I really have not paid a lot attention to what you are trying to do, just getting your code working, but now that I look at it, is seems you are just trying to get the PS major version and determine if a service is running on remote servers. If that’s all it is, I would avoid the whole creating a message to return from the remote machine and just collect the data. You can analyze it however you want from the local machine after. Like this:
      <textarea class=”urvanov-syntax-highlighter-plain print-no” style=”tab-size: 4; font-size: 14px !important; line-height: 18px !important; z-index: 0; opacity: 0;” readonly=”readonly” data-settings=”dblclick”></textarea>

      Thanks Mike but how does this handle connection errors though. I’m trying to assign failed connections to a variable so they can be output to the bottom of a log file to flag as needing manual attention.

      Also, I’ve tried your code and the output is not showing information as expected:

      services : {System.ServiceProcess.ServiceController, System.ServiceProcess.ServiceController, System.ServiceProcess.ServiceController, System.ServiceProcess.ServiceController…}
      PSVersion : {PSRemotingProtocolVersion, BuildVersion, PSCompatibleVersions, PSVersion…}
      PSComputerName : ComputerName
      RunspaceId : fb32f0bb-da71-42e0-8155-507bdb1c5966

      I’m not sure why the RunspaceId is present either as Select-Object should be excluding this.

    • #278109
      Participant
      Topics: 5
      Replies: 252
      Points: 999
      Helping Hand
      Rank: Major Contributor

      What is the error you are receiving?  If you only have the one property, there is no need for a custom object, just return the string.  PSComputername will still be added as a property.

    • #278121
      Participant
      Topics: 5
      Replies: 252
      Points: 999
      Helping Hand
      Rank: Major Contributor

      PSComputerName and RunspaceID properties are created automatically with Invoke-Command.  If you want to catch connection issues, it would have to be handled by the Invoke-Command and not inside the remote hosts script block.  Here is a simplified example.  The variable $connectionissues will capture all the errors.  You can call it later and even output the contents to a file if you want.

      $connectionissues will have a targetobject property which will be the computername with the connection issue.

    • #278139
      Participant
      Topics: 9
      Replies: 30
      Points: 129
      Rank: Participant

      Haha that makes error collection a little easier……! That’s exactly what I was after. Thanks for that.

      It seems it didn’t like having the Select-Object properties on separate lines. When I use just the one line the output is as expected:

      I’m sure there’s a reason but the main thing is that I think this does everything I need now but am sure I’ll be back if it doesn’t 😉

      Thanks for bearing with me guys. Much appreciated as always.

    • #278142
      Participant
      Topics: 5
      Replies: 252
      Points: 999
      Helping Hand
      Rank: Major Contributor

      Also, I’ve tried your code and the output is not showing information as expected:

      services : {System.ServiceProcess.ServiceController, System.ServiceProcess.ServiceController, System.ServiceProcess.ServiceController, System.ServiceProcess.ServiceController…}

      PSVersion : {PSRemotingProtocolVersion, BuildVersion, PSCompatibleVersions, PSVersion…}

      PSComputerName : ComputerName

      RunspaceId : fb32f0bb-da71-42e0-8155-507bdb1c5966

      I’m not sure why the RunspaceId is present either as Select-Object should be excluding this.

      That looks correct for $results.  In PowerShell everything is an object.  What is displayed on the screen is just a representation of the object.  If you use the property dereference operator, you can access the individual properties.  i.e. $results.pscomputername or $results.services.  The pipe at the end of the I sent should give you usable output to the screen.

       

Viewing 18 reply threads
  • You must be logged in to reply to this topic.