invoke-command on a remote machine access denied

Tulik23 K 66 Reputation points
2022-04-01T11:06:30.26+00:00

Hi all

I have a small question (i guess)
i am trying to run thie remote commnad and when it tried to write to the location i get access denied
(when i changed the out-file to c:\ it worked but since i need to run it on multiple computers i need the log to be on a shared location

Invoke-Command -ComputerName "ws-it-center1" -scriptblock { Get-PnpDevice | select Status,SystemName,Class,FriendlyName,InstanceId | ft -AutoSize -Wrap | Out-File -Append \media\MEDIA\DELETE\usb1.txt} -credential $cred

thanks

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
12,484 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,576 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. T. Kujala 8,726 Reputation points
    2022-04-01T11:56:54.773+00:00

    Hi @Tulik23 K ,

    You could try the following method.

    $s = New-PSSession -ComputerName "you_computer_name" -Credential(Get-Credential)

    Invoke-Command -Session $s -ScriptBlock -scriptblock { Get-PnpDevice | select Status,SystemName,Class,FriendlyName,InstanceId | ft -AutoSize -Wrap | Out-File -Append \media\MEDIA\DELETE\usb1.txt}

    0 comments No comments

  2. Tulik23 K 66 Reputation points
    2022-04-01T12:13:43.253+00:00

    Hi
    still getting the same error
    also if i run it one few computers it will ask me on each one the cred ?

    Access to the path '\srv-apps-02\share\tk\log.txt' is denied.

    • CategoryInfo : OpenError: (:) [Out-File], UnauthorizedAccessException
    • FullyQualifiedErrorId : FileOpenFailure,Microsoft.PowerShell.Commands.OutFileCommand
    • PSComputerName : ws-it-center1
    0 comments No comments

  3. Rich Matheisen 47,386 Reputation points
    2022-04-01T19:37:55.543+00:00

    If the machine from which the Invoke-Command was launched is "Machine #1", then the machine on which the script block is run ("ws-it-center1") is "Machine #2", and the machine that hosts the share (media") is "Machine #3" -- and you are encountering the "Second Hop Problem".

    You cannot use the credential from "Machine #1" to access "Machine #3" from "Machine #2".

    The simplest solution to this is to use a PSSession. See here for more information: ps-remoting-second-hop

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.