I’m trying to understand the use of Invoke-Command in a very specific scenario.
Get-ChildItem -Path C:\Windows\csc -Recurse -File
Running the above command produces the following error:
Get-ChildItem : Access to the path 'C:\Windows\csc' is denied. At line:1 char:1 + Get-ChildItem -Path C:\Windows\csc -Recurse -File + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : PermissionDenied: (C:\Windows\csc:String) [Get-C hildItem], UnauthorizedAccessException + FullyQualifiedErrorId : DirUnauthorizedAccessError,Microsoft.PowerShell. Commands.GetChildItemCommand
Invoke-Command -ComputerName localhost -ScriptBlock {Get-ChildItem -Path C:\Windows\csc -Recurse -File}
Wrapping that same command in the ScriptBlock of Invoke-Command as shown above
will actually output all the files in that folder.
What is Invoke-Command doing that allows it to output all the files instead
of generating an error?