There is nothing wrong with your bypass, which is in fact the (nearly) correct method to use Copy-VMGuestFile.
Copy-VMGuestFile is made to copy only 1 file, no recursion for subfolders.
See also this older thread where Vitali, from the PowerCLI Dev Team, confirmed that this is the intended working for the cmdlet.
Note1: since this script uses the -replace operator, the 1st value on the right side (the $src in this case) is interpreted as a RegEx. Hence we have to escape all RegEx special characters (like a \)
Note2: if you want the folder Source created as well, you will have to include it yourself in the Destination
Note3: the Force switch is required, otherwise (sub)folders will not be created
Note4: to handle al files recursively, you need to add the Recurse switch on the Get-ChildItem cmdlet
Note5: since the Force parameter will create folders, we only need to list files in the Source, hence the File switch on the Get-ChildItem cmdlet
I normally use it like this.
$vmName = 'VM'$src = 'C:\Source'
$dst = 'C:\Destination\Source'
$vm = Get-VM -Name $vmName
Get-ChildItem -Path D:\Demos -Recurse -File |
ForEach-Object -Process {
$sCopy = @{
VM = $vm
Source = $_.FullName
Destination = $_.FullName -replace ([RegEx]::Escape($src)), $dst
LocalToGuest = $true
Force = $true
GuestUser = 'MyAccount'
GuestPassword = 'MyPassword'
Verbose = $true
}
Copy-VMGuestFile @SCopy
}
---------------------------------------------------------------------------------------------------------
Was it helpful? Let us know by completing this short survey here.