0

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 Answer 1

0

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.