Welcome › Forums › General PowerShell Q&A › Invoke-Command + Get-ChildItem = Output?
- This topic has 2 replies, 3 voices, and was last updated 4 months, 2 weeks ago by
Participant.
-
AuthorPosts
-
-
September 10, 2020 at 11:19 am #255659
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:
PowerShell123$a = Invoke-Command -AsJob -ComputerName $env:computername -ScriptBlock {Get-ChildItem -Path C:\ *.exe }echo $aHowever, instead of $a containing the output for the Get-Childitem command ( a list of executables), it instead contains the output for the job!
PowerShell123Id Name PSJobTypeName State HasMoreData Location Command-- ---- ------------- ----- ----------- -------- -------15 Job15 RemoteJob Running True WIN-GNNG6UDHR6J ...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-jobPowerShell123Connecting to remote server WIN-GNNG6UDHR6J failed with the following error message : Access is denied. For more information, see the about_Remote_Troubleshooting Help topic.+ CategoryInfo : OpenError: (WIN-GNNG6UDHR6J:String) [], PSRemotingTransportException+ FullyQualifiedErrorId : AccessDenied,PSSessionStateBrokenThanks ahead of time!
-
September 10, 2020 at 2:10 pm #255695
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.
-
September 11, 2020 at 10:27 am #255866
Here’s an example. In powershell 7 it comes out blank though, lol. For this example, the prompt has to be elevated.
PowerShell1invoke-command localhost -asjob { dir } | receive-job -wait -AutoRemoveJob
-
-
AuthorPosts
- The topic ‘Invoke-Command + Get-ChildItem = Output?’ is closed to new replies.