Invoke-Command + Get-ChildItem = Output?

Welcome Forums General PowerShell Q&A Invoke-Command + Get-ChildItem = Output?

Viewing 2 reply threads
  • Author
    Posts
    • #255659
      Participant
      Topics: 5
      Replies: 9
      Points: 68
      Rank: Member

      Hello!

      I have a script that used Start-Job to start Get-ChildItem script. For reasons that are too long to get into, I wanted to try this methodology with Invoke-Command instead!

      The command looks like this:

      However, instead of $a containing the output for the Get-Childitem command ( a list of executables), it instead contains the output for the job!

      What am I doing wrong?
      I have attempted to use Receive-Job but have had no success yet. This is the error I get when I try to receive-job

      Thanks ahead of time!

      • This topic was modified 2 weeks, 2 days ago by izShell57.
      • This topic was modified 2 weeks, 2 days ago by grokkit.
    • #255695
      Participant
      Topics: 0
      Replies: 81
      Points: 362
      Helping Hand
      Rank: Contributor

      If you want the output of the remote command to be stored in $a, don’t use the -AsJob switch. If you continue to use a job, then once $a shows a State other than Running, you can use $a | Receive-Job for the output. Access denied errors could be a number of things. I’d make sure the account running Invoke-Command has access to perform the remote command as if it were running the command locally on the target system. Admin elevation may be required in the shell as well.

    • #255866
      js
      Participant
      Topics: 32
      Replies: 842
      Points: 2,690
      Helping Hand
      Rank: Community Hero

      Here’s an example. In powershell 7 it comes out blank though, lol. For this example, the prompt has to be elevated.

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