UNC Network Path issues with PowerShell - powershell

Omitted sensitive data.
I can't get PowerShell to connect to a shared folder.
Copy-Item : Cannot find path '\\192.168.0.000\Common Data\Software\Crystal Reports\CRRedist2005_X64.msi' because it does not exist.
This does work on one machine but not another.
$source = "\\192.168.0.000\Common Data\Software\Crystal Reports\CRRedist2005_X64.msi"
Copy-Item $source -Destination $env:USERPROFILE\Downloads -Verbose
This doesn't work on either machine
Copy-Item "\\192.168.0.000\Common Data\Software\Crystal Reports\CRRedist2005_X64.msi" -Destination $env:USERPROFILE\Downloads -Verbose
I've tried using the New-PSDrive method
New-PSDrive -Name S -Root "\\192.168.0.000\Common Data\" -PSProvider FileSystem
Copy-Item "S:\Software\Crystal Reports\CRRedist2005_X64.msi" -Destination $env:USERPROFILE\Downloads -Verbose
Gives The specified drive root "\\192.168.0.000\Common Data\" either does not exist, or it is not a folder.
The FileSystem:: also gives path not found
Copy-Item "FileSystem::\\192.168.0.000\Common Data\Software\Crystal Reports\CRRedist2005_X64.msi" -Destination $env:USERPROFILE\Downloads -verbose
If I copy the address into file explorer it works no problems.
I'm going to crazy trying to figure out why it's not working.
Edit: Remapped the drives and it worked on the second computer.

Related

Powershell copy-item creates a folder instead of copying file

I mapped a webdav folder with new-psdrive, cd to a folder, then try to copy one file to a local folder. The script code (draft) is the following:
New-PSDrive -Name WEBDAV -PSProvider FileSystem -Root "\\testserver#SSL\davwwwroot\myApp"
cd WEBDAV:
cd "notes\user"
Copy-Item -Path ".\important.txt" -Destination "c:\temp"
What I see, that after the Copy-Item a new empty FOLDER "C:\temp\important.txt" is created. Not a file. When I change the "copy-item" command to "robocopy" it works perfectly. I am wondering what is wrong with the copy-item... How could I use it to do the work?!
simply specify the file name:
Copy-Item -Path ".\important.txt" -Destination "c:\temp.txt"

Empty folders when using Copy-Item Network Drive to Local Drive

I'm currently using this Powershell script
Function Copy-ItemUNC {
New-PSDrive -Name "B" -PSProvider "FileSystem" -Root "\\ServerName\serverupdates\deploy\Program Files\"
Copy-Item -Path "\\servername\serverupdates\deploy\Program Files\*" -Destination 'C:/Program Files';
}
When I run the script it creates the folders but there are no subfolders within them.
Second problem I have is that I have to manually open Windows Explorer and type in the path to connect to it first in order for this script to even run. Is there a way to fix that too?
It's because you are not asking for subdirs, as that requires you to do this.
Copy-Item -Recurse
-Recurse <SwitchParameter>
Indicates that this cmdlet performs a recursive copy.
Required? false
Position? named
Default value False
Accept pipeline input? False
Accept wildcard characters? false
(Get-Command -Name Copy-Item).Parameters.Keys
Get-Help -Name Copy-Item -Full
Get-Help -Name Copy-Item -Examples
This command copies the mar1604.log.txt file to the C:\Presentation directory. The command does not delete the original file.
Example 2: Copy the contents of a directory to another directory
PS C:\>Copy-Item "C:\Logfiles" -Destination "C:\Drawings" -Recurse
So, this line should be like this..
Copy-Item -Path "\\servername\serverupdates\deploy\Program Files\*" -Destination 'C:/Program Files' -Recurse
You also do not really need this..
New-PSDrive -Name "B" -PSProvider "FileSystem" -Root "\\ServerName\serverupdates\deploy\Program Files\"
... based on what you are after. Especially since you are not using it anywhere in your code. That semi-colon is also not needed.

Robocopy commands to copy a file to over 50 remote machines

I started looking at robocopy yesterday to try to copy and overwrite a file from one destination to many remote computers. I've tried Robocopy to copy files to a remote machine but it doesn't work. I get the same error as the person in the link. Does anybody have any suggestions or lead me in the right way ? thank you so much !
You could just use PowerShell for this. It has an inefficiency issue wherein it would copy one at a time but that shouldnt be an issue for 50ish machines. This could help if you made a PowerShell script
$computers = Get-Content "C:\filewithcomputers.txt"
$fileToCopy = "C:\filetocopy.txt"
ForEach($computer in $Computers){
Copy-Item -Path $fileToCopy -Destination "\\$computer\C`$\Temp"
}
The would copy the file $fileToCopy to each server in the file C:\filewithcomputers.txt assuming that the file contained a list of computer with each one on its own line. The file would be copied to the temp folder on each machine. Update the paths as required for your scenario. I only suggest this since you tagged powershell-remoting. If you are not adept with PowerShell maybe someone else can give you a better answer more of what you are looking for. Using RoboCopy for one file seemed tedious.
If you wanted to check to see if a folder exists and is accessible you could do something like this.
$computers = Get-Content "C:\filewithcomputers.txt"
$fileToCopy = "C:\filetocopy.txt"
ForEach($computer in $Computers){
$destinationx86 = "\\$computer\C`$\Program Files (x86)"
$destination = "\\$computer\C`$\Program Files"
If(Test-Path $destinationx86){
# Copy this to Program Files (x86)
Copy-Item -Path $fileToCopy -Destination $destinationx86
} Else {
# Copy this to Program Files
Copy-Item -Path $fileToCopy -Destination $destination
}
}
If you need to connect with different credentials, you can use
$credential = Get-Credential
New-PSDrive -Name "Computer01" -PSProvider FileSystem -Root "\\Computer01\Share" -Credential $credential -Scope global
Now you can copy to e.g. Computer01:\Folder01\
If you have set your environment up to support PSRemoting and have placed the file in a file share you can use PowerShell Remoting to instruct many computers to retrieve the file themselves nearly simultaneously with Invoke-Command. You can limit the number of simultaneous actions using -ThrottleLimit depending on the size of the source file and how robust the network/server are:
$computers = Get-Content "C:\filewithcomputers.txt"
$originalsource = "\\fileserver\shared\payload.exe"
$originaldestination = "c:\"
$scriptblockcontent = {
param($source,$destination)
Copy-Item -Path $source -Destination $destination
}
Invoke-Command –ComputerName $Computers –ScriptBlock $scriptblockcontent `
–ThrottleLimit 50 -ArgumentList $originalsource,$originaldestination

Need help on Powershell Copy-Item from network drives

I am trying to use Copy-Item from remote machine to another remote machine with the command:
Copy-Item -Path "\\machine1\abc\123\log 1.zip" -Destination "\\machine2\\c$\Logs\"
I am constantly getting Error "Cannot find Path "\\machine1\abc\123\log 1.zip"
I can access that path and copy manually from there.
I am opening PowerCLI as administrator and running this script... I am absolutely stuck here and not sure how to resolve it.
This seems to work as is on PowerShell v3. I don't have v2 handy to test with, but there are two options that I'm aware of, which ought to work. First, you could map PSDrives:
New-PSDrive -Name source -PSProvider FileSystem -Root \\machine1\abc\123 | Out-Null
New-PSDrive -Name target -PSProvider FileSystem -Root \\machine2\c$\Logs | Out-Null
Copy-Item -Path source:\log_1.zip -Destination target:
Remove-PSDrive source
Remove-PSDrive target
If this is something you're going to do a lot, you could even wrap this in a function:
Function Copy-ItemUNC($SourcePath, $TargetPath, $FileName)
{
New-PSDrive -Name source -PSProvider FileSystem -Root $SourcePath | Out-Null
New-PSDrive -Name target -PSProvider FileSystem -Root $TargetPath | Out-Null
Copy-Item -Path source:\$FileName -Destination target:
Remove-PSDrive source
Remove-PSDrive target
}
Alternately, you can explicitly specify the provider with each path:
Copy-Item -Path "Microsoft.PowerShell.Core\FileSystem::\\machine1\abc\123\log 1.zip" -Destination "Microsoft.PowerShell.Core\FileSystem::\\machine2\\c$\Logs\"
this works all day for me:
$strLFpath = "\\compname\e$\folder"
$strLFpath2 = "\\Remotecomputer\networkshare\remotefolder" #this is a second option that also will work
$StrRLPath = "E:\localfolder"
Copy-Item -Path "$StrRLPath\*" -Destination "$strLFpath" -Recurse -force -Verbose
things to watch:
Copy-item define the LAST item as the object.
for copying the content of a folder you NEED the \*
If you are copying the folder it self to a new location then you do not need to declare the content.
I use this daily:
Robocopy /E \\\SOURCEIP\C$\123\ \\\DESTIP\C$\Logs\
There is an empty space in the middle. For ROBCOPY, /E does a copy. You can google if you need to do a move.
Or:
$SourceIP = Read-Host "Enter the Source IP"
$DESTIP = Read-Host "Enter the Destination IP"
Robocopy /E \\\\$SourceIP\C$\123\ \\\\$DESTIP\C$\Logs\
####Just adjust the C$ path on both#####

Copy a single file to a non-existing folder

It's easy to copy multiple files to a folder that doesn't exists and let it be created:
Copy-Item C:\Temp\aa C:\Temp2\DoesNotExist
The command above will create the folder DoesNotExist. That's what I'm after.
But what is the PowerShell syntax to the same when the source is only a single file?
Copy-Item C:\Temp\test.txt C:\Temp2\DoesNotExist
I tried C:\Temp2\DoesNotExist\ (with the trailing slash), but Powershell says "The filename, directory name, or volume label syntax is incorrect." and refuses to copy the single file.
If you're looking for a one liner solution, you can do this.
copy "C:\test2.txt" -Destination (New-Item "C:\Temp2\DoesNotExist\" -Type container -force) -Container -force
I think Geoff Guynn's one-liner should be as follows:
Copy-Item -Path "C:\test2.txt" -Destination (New-Item "C:\Temp2\DoesNotExist\" -ItemType directory -Force) -Force
The parameter for the cmdlet New-Item should be -ItemType and the intended "Type" should be directory.
The additional parameter -Container for the cmdlet Copy-Item seems to me superfluous; on the one hand it is set to $true by default anyway, on the other hand a single file should be copied and not the folder structure should be preserved.