Get-ChildItem -Path not working

Konstantin 1 Reputation point
2021-04-02T17:17:25.577+00:00

Hello to All.

I have very simple code

$ShowDirectory =  "C:\FTP" 

This code working 

Invoke-Command  -ComputerName  $serverName -Credential $cred  {Get-ChildItem -Path "C:\FTP" | select  FullName, LastAccessTime  }

and this is not

Invoke-Command  -ComputerName  $serverName -Credential $cred    {Get-ChildItem  -Path $ShowDirectory | select  FullName, LastAccessTime  }  

it's not accepted path to the directory as a variable.

Thank you for help.

Konstantin

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,359 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Andreas Baumgarten 96,036 Reputation points MVP
    2021-04-02T18:12:10.843+00:00

    Hi @Konstantin ,

    You have to pass the variable to the script block.

    Please try this:

     $ShowDirectory =  "C:\FTP"   
     Invoke-Command -ComputerName  $serverName -Credential $cred -ScriptBlock {Get-ChildItem -Path $using:ShowDirectory | Select-Object FullName, LastAccessTime}    
    

    ----------

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten

    0 comments No comments

  2. Konstantin 1 Reputation point
    2021-04-02T18:21:54.26+00:00

    Thank you AndreasBaumgarten!!!!!