invoke-command on a remote machine access denied

Tulik23 K 66 Reputation points

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


Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
9,103 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,173 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. T. Kujala 8,676 Reputation points

    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

    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 42,906 Reputation points

    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