PowerShell: Copy-Item Cannot find path - powershell

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?

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?

Related

I have an issue with using Copy-Item and a Veriable for destination

This Might be an easy one. But I can figure out what is going wrong with my simple copy script.
I have a shared directory that I am copying items from. I am printing out the destination path to console so I know it is correct But I am receiving a powershell error I do not understand.
Here is my script
#Files to copy
#Get Installers from folder
$APPS = Get-ChildItem \\Server1\shared\APPS -Name
#ForEach loop to identify and move files
ForEach($APP in $APPS) {
$dest = "\\Server1\Shared\APPS\$APP"
#Write-host to see destination path on console
write-host $dest
#copy item from destination path to local directory
Copy-Item $dest -Destination "c:\apps\"
}
This seems straight forward. But I don't understand why I am receiving the following error
\\Server1\Shared\APPS\LTCDesktopSetup.exe
Copy-Item : The filename, directory name, or volume label syntax is incorrect.
At C:\Users\computer1\documents\PowerShell\Moving Installer to local drive.ps1:13 char:2
+ Copy-Item $dest -Destination "c:\apps\"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Copy-Item], IOException
+ FullyQualifiedErrorId : System.IO.IOException,Microsoft.PowerShell.Commands.CopyItemCommand
Augusto,
I'd suggest this syntax:
$APPS = (Get-ChildItem "\\mybooklive\CMShared\NAS-Downloads" -filter *.exe).FullName
ForEach ($App in $Apps) {
copy-item "$APP" -Destination "G:\Test\Copy-Test" -Force
}
or the more compact:
$APPS = (Get-ChildItem "\\mybooklive\CMShared\NAS-Downloads" -filter *.exe).FullName |
copy-item -Destination "G:\Test\Copy-Test" -Force
Getting FullName vs Name so you don't have to add the source path back in.
Using -Filter so you only get .exe files (this is an assumption from the variable name $Apps).
The force will take care of some IO problems like the file already existing.
HTH
Naturally, you'll have to substitute your paths for my test ones.

Copy Files on Same Directory using Powershell

I am trying to write powershell Script which will create backupfolder on same Path where Application exist and need to copy the folders & files into backupfolder before deploying. Below are the command was using to perform but am getting error
$Source = "C:\XYZ"
$BackupFolder = New-Item -ItemType Directory -Force -Path $source_$(Get-Date)
Copy-Item -Path $Source\* $BackupFolder -Force
Error: Cannot copy item C:\XYZ\Backup_18-02-2017 on to itself
Try:
Copy-Item $Source\* $BackupFolder -Exclude $BackupFolder
That will eliminate the folder that you are copying into as a source that is being copied from.
Variables can contain underscores. The following works and displays the string "asdf"
$a_ = "adsf"; $a_
Your New-Item cmdlet call should have failed since $source_ is not a variable and would return null. This is default behavior for PowerShell. When I run your code as is I get the following:
New-Item : Cannot find drive. A drive with the name '02/18/2017 22' does not exist.At line:1 char:1
+ New-Item -ItemType Directory -Force -Path "$source_$(Get-Date)" -what ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (02/18/2017 22:String) [New-Item], DriveNotFoundException
+ FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.NewItemCommand
So I would have expected your folder variable to be null. wOxxOm brings this up in comment as well
Several options to address what I am sure is the partial source of your issue.
$BackupFolder = New-Item -ItemType Directory -Force -Path "$source`_$(Get-Date)"
$BackupFolder = New-Item -ItemType Directory -Force -Path "$($source)_$(Get-Date)"
$BackupFolder = New-Item -ItemType Directory -Force -Path ("{0}_{1} -f "$source, Get-Date)
You will still have to try and exclude this folder from the copy as well like Keith Hill's answer is telling you
Copy-Item $Source\* $BackupFolder -Exclude $BackupFolder
try Something like this
$Source = "C:\XYZ"
$Destination="{0}{1:yyyyMMdd}" -f $source, (Get-Date)
New-Item -ItemType Directory -Force -Path $Destination
Copy-Item -Path $Source\* $Destination -Recurse -Force
If I understand the question correctly. You want to take "C:\XYZ" and backup into the same directory called "C:\XYZ\backup_$DATE". What you will actually do is create a loop that will break once it reaches the max 248 characters. If we use the -exclude option then we can exclude the backup directory "C:\XYZ\backup_$DATE".
This function will do the trick and also gives you error handling.
Function Get-CopyDirectory{
#####################
# Dynamic Variables #
#####################
$Date = Get-Date -format ddMM-yyyy
$Exclude="Backup*"
####################
# Static Variables #
####################
$AppPath = "F:\Test\"
$BackupPath = "$AppPath\BACKUP_$Date\"
if (Test-Path $BackupPath) {
Write-Host "Backup Exist" -f Cyan
}
else
{
Copy-Item "$AppPath\*" $BackupPath -Exclude $Exclude -recurse -verbose
}
}
CLS
Get-CopyDirectory

Copy-File in scriptblock can't find path

I have implemented a PS Script that deploys code on multiple servers at the same time. Here I need to copy some source file from one server to another. See the code below:
for ($i=1; $i -le 5; $i++) {
$serverName="iwflO" + $i
$sourceFile="\\iwdflO1\C$\Deploy\bin"
$destination="\\$serverName\C$\Program Files (X86)\Shian\MyService\bin\"
$Myblock = {
Param{$sourceFile,$destination)
Copy-Item -Force -Recurse $sourceFile -Destination $destination
}
$result = Invoke-Command -ComputerName $ServerName -Credential "shian" -ScriptBlock $Myblock -ArgumentList $sourceFile,$destination;
$result;
}
cd c:\
It's working fine for iwflO1 which is the root server from where I'm running the script but for other servers it's giving me an error like
Cannot find Path "\iwdflO1\C$\Deploy\bin" because it does not exist.
But if I logged in to iwflO2 or any other server and hit the path manually its working fine.
I can see the mistake is with the block :
Instead of this:
$Myblock={param{$sourceFile,$destination)
copy-Item -Force -Recurse $sourceFile -Destination $destination
}
Do this:
$Myblock={param($sourceFile,$destination)
copy-Item -Force -Recurse $sourceFile -Destination $destination
}
This is working fine if I am hardcoding the server names(tested in my local)
Since you are using admin share, directly try this:
Copy-Item -Path \\serverA\c$\programs\temp\test.txt -Destination \\serverB\c$\programs\temp\test.txt;
Note: You have to specify the file. Else you get-childitem -recurse inside the source folder and put it directly in the destination .
Hope it helps.

Powershell Copy-Item going up few directories

I am trying to provide a path to Copy-Item cmdlet in Powershell which goes up few directories but I am getting error
"ERROR - failed with error: "A positional parameter cannot be found
that accepts argument '......\'."
The command I am trying to execute is
Copy-Item $Source + "..\..\..\" + ($environment) + "\*.config" $destination
Can anyone please guide me how can I go up few directories while providing a path to Copy-Item
You need to parenthese the first argument (source):
Copy-Item ($Source + "..\..\..\" + ($environment) + "\*.config") $destination
Consider using the Join-Path cmdlet when combining a path. You could also write something like:
$sourceDir = Join-Path (Get-Item $Source).Parent.Parent.Parent $environment
Get-ChildItem -Path $sourceDir -Filter '*.config' | Copy-Item -Destination $destination
I just had to put double quotes around the whole path rather than concatenating.
Copy-Item "$Source\..\..\..\$environment\*.config" $destination

Mass file copy to domain computers

I am trying to take a screen saver file I've made and copy it to all of our desktops and laptops \system32 folder. I created a computers text file and found this script, but I keep getting this error. Any help would be appreciated.
Running this in Powershell 3.0 on a 2012 Server logged in as an admin.
$computers = gc "\\server\share\scripts\computers.txt"
$source = "\\share\scripts\MySlideshow.scr"
$dest = "C:\Windows\System32"
foreach ($computer in $computers) {
if (test-Connection -Cn $computer -quiet) {
Copy-Item $source -Destination \\$computer\$dest -Recurse
} else {
"$computer is not online"
}
}
Error:
Copy-Item : The given path's format is not supported.
At C:\users\tech\desktop\scripts\screen.ps1:6 char:9
+ Copy-Item $source -Destination \\$computer\$dest -Recurse
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Copy-Item], NotSupportedException
+ FullyQualifiedErrorId : System.NotSupportedException,Microsoft.PowerShell.Commands.CopyItemCommand
The resulting UNC format for you destination is invalid. You're passing
"\\computer\c:\windows\system32"
when you should be passing
"\\computer\c$\windows\system32"
Try quoting the -destination parameter like this too:
Copy-Item $source -Destination "\\$computer\$dest" -Recurse
You'll also need to use single-quotes when assigning to $dest to prevent powershell from trying to expand the dollar sign as a variable sigil.
$dest = 'c$\windows\system32'
Debug your script by using copy-item -whatif ... to ensure that you're passing the correct parameters.