0

when a form is shown, I try to get the files in a folder of a remote server passing a variable. This gives me an empty object. But if instead of a variable I pass a string with the path to my folder I get the correct files.

$form.Add_Shown({
$fold = 'c:/mypath/test';

#this returns no file
$files = Invoke-Command -Credential $cred -ComputerName $server   -ScriptBlock {Get-ChildItem -Path $fold } ;

#this code correctly returns the files present in the folder 
#$files = Invoke-Command -Credential $cred -ComputerName $server   -ScriptBlock {Get-ChildItem -#Path 'c:/mypath/test'} ;

$txt.Text="files $files";


})

what could be the problem?

Gyonder
  • 3,674
  • 7
  • 32
  • 49
  • 2
    `{Get-ChildItem -Path $fold }` -> `{Get-ChildItem -Path $using:fold }`, see [about_Remote_Variables](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_remote_variables) – Mathias R. Jessen Mar 12 '21 at 14:25
  • 2
    As the linked duplicate shows this is a very common and well documented scenario. Your local variables don't exist remotely. You have several ways to use the local variable remotely, one of which @MathiasR.Jessen pointed out is `$using:variable` – Doug Maurer Mar 12 '21 at 14:27

0 Answers0