I'm trying to get PowerShell to copy files from a remote computer (on which I have admin rights through AD) to the local computer. It fails in the strangest place. Here's a snippet of the script:
$configs = Get-ChildItem -Recurse -ErrorAction SilentlyContinue -Filter "*.config" $serverUNCPath
foreach($config in $configs){
$config_target_dir = $dest.Path + $config.Directory.FullName.Replace($serverUNCPath,"")
if(Test-Path -Path $config_target_dir){
Copy-Item $config -Destination $config_target_dir
}
}
It fails with the message
Cannot find path 'D:\ServerDeploy\TestMachine1\website\web.config' because it does not exist.
At :line:39 char:12
+ Copy-Item <<<< $config -Destination $config_target_dir
The path D:\ServerDeploy\TestMachine1\website
exists. I'm going mad over this.
What can I do to fix it?
Copy-Item cmdlet is used to copy a directory by passing the path of the directory to be copied and destination path where the folder is to be copied.
Just use the function SHGetFolderPath, which returns the path to whatever folder you want.
To copy items in PowerShell, one needs to use the Copy-Item cmdlet. When you use the Copy-Item, you need to provide the source file name and the destination file or folder name. In the below example, we will copy a single file from the D:\Temp to the D:\Temp1 location.
xcopy is the windows command. It works with both PowerShell and cmd as well because it is a system32 utility command.
Eeeeh.... OK?
If I replaced the line
Copy-Item $config -Destination $config_target_dir
with
Copy-Item $config.FullName $config_target_dir
it suddenly magically worked....
What gives?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With