Copy-Item with over write - powershell

In one of my script, it tries to copy an exe to C:\Windows\Temp folder. For example:
Copy-Item repo\filename.exe -Destination \\$machine_name\C$\Windows\Temp
Error Message:
Copy-Item : The network path was not found.
At D:\CMPortal\Scripts\ClientRepair\RepairCCMClient.ps1:122 char:33
+ if(Copy-Item <<<< cmsetup.exe -Destination \\$install_cmexec\C$\Windows\Temp)
+ CategoryInfo : NotSpecified: (:) [Copy-Item], IOException
+ FullyQualifiedErrorId : System.IO.IOException,Microsoft.PowerShell.Commands.CopyItemCommand
When I run the same command manually, it works. Dont know whats wrong.
Is there any to overwrite the file if the copying file exists in the destination?
Thanks in advance.

Are you using the quotes correctly?
Copy-Item 'repo\filename.exe' -Destination "\\$machine_name\C`$\Windows\Temp" -Force

Does $install_cmexec contains a server name?
Also, make sure the user who runs the script has the appropriate permissions on the target path.

Related

Unable to pass path variable while copying file

i am trying to copy one file from local machine to a server path. when i hardcode the paths it does copy the file with the same syntax however when passing it through variable it through an error
Cannot bind argument to parameter 'Path' because it is null.
+ CategoryInfo : InvalidData: (:) [Copy-Item], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.CopyItemCommand
+ PSComputerName : localhost
here is the code:
$SourcePath ="C:\Temp\Test.txt"
$DestinationPath ="\\ServerPath\Apps"
write-host $SourcePath
write-host $DestinationPath
Copy-Item $SourcePath $DestinationPath
Any thought why i am getting this.
If you really wrote $DestinationPath ="\ServerPath\Apps" then this should be correct:
$DestinationPath ="\\ServerPath\Apps"
Serverpath always start with a double back slash, and depending on escape sequences (different language like C#) you have to double the back slash, so it would be
DestinationPath ="\\\\ServerPath\\Apps"

How can I move some specific files from my Android phone to my PC using PowerShell?

I've written the following code to do copy .mkv files from my phone to my PC.
$source = "This PC\HUAWEI nova 3e\Internal storage\*.mkv"
$dest = "C:\Users\Kapil\Desktop\"
Copy-Item -Path $source -Destination $dest
But it gave me the following error:
Copy-Item : Cannot find path 'C:\Users\Kapil\This PC\HUAWEI nova 3e\Internal storage' because it does not exist.
At line:3 char:1
+ Copy-Item -Path $source -Destination $dest
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (C:\Users\Kapil\...nternal storage:String) [Copy-Item], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.CopyItemCommand
How do I set the correct source path? Is it possible to do this operation this way?
The script is looking in the C: drive for your android storage. You need to provide the full path to the android phone, but I've had trouble with this in the past, since it's not a recognized drive (Like for instance C:), but you can use adb to pull files from android.
Have a look at this:
Android: adb pull file on desktop

Powershell Script "$_.FullName" not working - Sending files via "Copy-VMFile"

I'm trying to use a script I found on the internet to copy all files in a directory but I can't get it to work. Can anybody help debug? I'm guessing the script was used to transfer windows/windows but I need windows --> Linux.
https://www.powershellmagazine.com/2013/12/17/pstip-copying-folders-using-copy-vmfile-cmdlet-in-windows-server-2012-r2-hyper-v/
Get-ChildItem C:\tmp -Recurse -File | % { Copy-VMFile -Name "OpenProject8.3" -SourcePath $_.FullName -DestinationPath "/tmp/" -FileSource Host }
The issue seems to be related to the sourcepath, but im not 100%.
Copy-VMFile : Failed to initiate copying files to the guest.
Failed to copy the source file 'C:\tmp\svn-repositories-20200212010002.tar.gz' to the destination '/tmp/' in the guest.
At line:1 char:43
+ ... -File | % { Copy-VMFile -Name "OpenProject8.3" -SourcePath $_.FullNam ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Copy-VMFile], VirtualizationException
+ FullyQualifiedErrorId : OperationFailed,Microsoft.HyperV.PowerShell.Commands.CopyVMFile
Well.... I feel stupid! The issue was that earlier testing the x-fer had succeeded and then when the file exists, Copy-VMFile won't overwrite the file (even though it uses root account...) and gives a non-descriptive error! The code above works fine as is.

powershell to copy folder(with sub-folders) from one server to another with credentials

I am using below code :
Copy-Item "\\10.X.X.Z\E:\abc\TASKS\AUTO_DAILY_TOLL_NOTICE" -Destination "\\10.X.X.Y\E\AshimTest\myscript" -Recurse
shows up error:
Copy-Item : Cannot find path
'\\10.X.X.Z\E:\CAS\TASKS\AUTO_DAILY_TOLL_NOTICE' because it does not
exist.
At line:1 char:1
+ Copy-Item "\\10.X.X.Y\E:\CAS\TASKS\AUTO_DAILY_TOLL_NOTICE" -Destination
"\\10 ...
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound:
(\\10.0.0.Z\E...ILY_TOLL_NOTICE:String) [Copy-Item],
ItemNotFoundException
+ FullyQualifiedErrorId :
PathNotFound,Microsoft.PowerShell.Commands.CopyItemCommand
I am missing something here.
Both the servers have credentials, is it because of that ??
I am actually copying from 10.X.X.Z server to 10.X.X.Y server where the powershell script is running.
With network drives you usually use e$ rather than e:
If you try the following, what is the result?
Copy-Item "\\10.0.0.49\E$\abc\TASKS\AUTO_DAILY_TOLL_NOTICE" -Destination "\\10.10.0.50\E$\AshimTest\myscript" -Recurse

Copy-Item PowerShell silently continues when source is a network share that does not exist

I'm seeing a strange behavior with the "Copy-Item" cmdlet. It looks like the cmdlet errors out if the source location is a local location that does not exist, but completely ignores that if the source is a network location that does not exist.
For e.g.
Copy-Item \\server\share\thisdoesnotexist\* -Destination c:\temp -ErrorAction Stop
This command doesn't show any errors even if the source share is invalid. However,
Copy-Item c:\thisdoesnotexist\* -Destination c:\temp -ErrorAction Stop
throws an error that looks like:
Copy-Item : Cannot find path 'C:\thisdoesnotexist' because it does not exist.
At line:1 char:1
+ Copy-Item c:\thisdoesnotexist\* -Destination c:\temp -ErrorAction Stop;
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (C:\thisdoesnotexist:String) [Copy-Item], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.CopyItemCommand
What's going on here? I checked TechNet but couldn't see anything there that talked about this. My question is:
Is this expected?
Is there a way to make the cmdlet fail when source network share is invalid as well?
Thanks for any pointers!