Copying files through a vm session using copy-item shows error when destination directory has square bracket

157 Views Asked by At

Copying files through a vm session using copy-item shows error when destination directory has wildcard (square bracket)

#powershell ver. 5.1.19041.2364

$vm_session = new-pssession -vmname $vmname -credential (get-credential)
$source_item = "c:\source\source.src"

#all destination paths below exist.

copy-item -literalpath $source_item -destination 'c:\[dest]\' -force;
#-> (O) no problem in the local session.

copy-item -literalpath $source_item -tosession $vm_session -destination 'c:\(dest]\' -force
#-> (O) Even in the vm session, it works fine with no square bracket path.

copy-item -literalpath $source_item -tosession $vm_session -destination 'c:\[dest]\' -force
#-> (X) In the vm session, It doesn't work with square brackets.

copy-item -literalpath $source_item -tosession $vm_session -destination 'c:\`[dest]\' -force
#-> (X) escaping square bracket is of no use.

$dest = [Management.Automation.WildcardPattern]::Escape('c:\[dest]\')
copy-item -literalpath $source_item -tosession $vm_session -destination $dest -force
#-> (X) escaping square bracket is of no use II.

Can this be solved? (I can not use copy-vmfile as of now)

thank you for reading.

1

There are 1 best solutions below

0
js2010 On

The only way I see is by moving it there remotely. These names are very inconvenient for powershell, since square brackets are wildcards. Note that powershell 7 has a display bug with get-childitem over invoke-command.

$s = new-pssession comp001
echo hi > file
icm $s { mkdir [dir] } 
copy file c:\users\admin\documents\ -ToSession $s
icm $s { mv file [dir] }
icm $s { dir -LiteralPath [dir] }


    Directory: C:\Users\admin\Documents\[dir]


Mode                LastWriteTime         Length Name                PSComputerName
----                -------------         ------ ----                --------------
-a----        8/25/2022   8:44 AM            416 file                comp001