Assistance with Invoke-Command ScriptBlock error.

Welcome Forums General PowerShell Q&A Assistance with Invoke-Command ScriptBlock error.

Viewing 3 reply threads
  • Author
    Posts
    • #278886
      Participant
      Topics: 2
      Replies: 2
      Points: 21
      Rank: Member

      Hello All,
      What I am trying to accomplish is for this script to take a list of remote servers and find the “Service Name” of a service, and then convert that into a PID that will be used later for process termination. This is only a snippet of a script that will take a list of servers, ask the user for the “Service Name” of a service and kill that task. For a single server, I have it working.
      Here is the issue, the script below is as far as I gotten toward my goal with a list of servers. This particular version seems the best, but gives an error “Get-Process: Cannot find a process withthe name “whatever”. I have verified the process exists with the name provided. In fact, the process that I am testing with has the same name as the Service Name except the process has ‘exe’ appended. Didn’t matter however.
      It appears that Invoke-Command has trouble with multiple commands in the ScriptBlock, but I don’t know another way to make this happen.
      I am limited to PoSH 5.1 and Get-Process and Get-WMIObject can’t accept the $server variable in the -ComputerName parameter of Invoke-Command. They only work if thier -ComputerName parameter is used.
      Any assistance would be appreciated.

    • #278913
      Participant
      Topics: 5
      Replies: 252
      Points: 998
      Helping Hand
      Rank: Major Contributor

      Variables defined outside of the Invoke-Command script block cannot be referenced inside the Invoke-Command script block.  So in your case line 7, $SvcName does not exist ($null).  To “fix” this you would need to preface $using: or pass the object in as an argument to a parameter.  Either way I would change the approach.  Based on what your requirements are per the post here’s what I would do:

      Note the computername parameter of invoke-command will take an array so no need for the foreach loop.  Any objects returned from the remote script block will be assigned to $ted with a new property of PSComputerName that will tell you which server it came from.

      • This reply was modified 1 month, 1 week ago by Mike R..
    • #278919
      Participant
      Topics: 17
      Replies: 1951
      Points: 3,987
      Helping Hand
      Rank: Community Hero

      There are numerous issues. This is invoking a command locally, to invoke a command locally again to make a remote call from the local server to the remote server. WMI and Get-Process (which in the background is using WMI) can make RPC calls to the server with DCOM:

      If you want to use PS Remoting with WSMAN, then you can do it like this to execute the commands remotely on the server. To pass a local variable to the remote session, you need to use the ‘Using:’ keyword:

      Also, keep in mind that this is assuming there is 1 returned value. Using -match is a regex, a better comparison would be using -eq (equals) for a straight comparison.

    • #278925
      Participant
      Topics: 2
      Replies: 2
      Points: 21
      Rank: Member

      Yea, I had a feeling that I was on a bad path with this one, but couldn’t sort out exactly what. Thank you for the guidance.

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