2012-05-24 123 views
60

我正在编写powershell脚本,我想从服务器A运行。 我想连接到服务器B并将文件复制到服务器A作为备份。使用Powershell远程复制文件

如果不能做到那么我想从服务器A连接到服务器B和文件复制到另一个目录服务器B.

我看到他们Copy-Item命令,但我不明白如何给它一个计算机名称。

我本来以为我可以做类似

Copy-Item -ComputerName ServerB -Path C:\Programs\temp\test.txt -Destination (not sure how it would know to use ServerB or ServerA) 

我怎样才能做到这一点?

+3

要使用复制项目,您必须使用UNC路径,如“\\ ServerB \ C $ \ Programs \ temp \ test.txt” –

回答

67

Simply use the administrative shares to copy files between systems. It's much easier this way.

Copy-Item -Path \\serverb\c$\programs\temp\test.txt -Destination \\servera\c$\programs\temp\test.txt; 

By using UNC paths instead of local filesystem paths, you help to ensure that your script is executable from any client system with access to those UNC paths. If you use local filesystem paths, then you are cornering yourself into running the script on a specific computer.

这只能当用户下谁有权带来管理股PowerShell会话中运行。 我建议使用服务器B普通网络共享与只读访问到每一个人,只需调用(从服务器A):

Copy-Item -Path "\\\ServerB\SharedPathToSourceFile" -Destination "$Env:USERPROFILE" -Force -PassThru -Verbose 
+7

此方法的一个可能问题是Copy-Item不支持备用凭据(如果您必须以不同的用户运行该命令)。在这种情况下,需要使用New-PSDrive方法。 –

+1

或者你可以使用'Invoke-Command'。 –

+0

此解决方案仅适用于阻止UNC共享的主机之间没有防火墙的情况。在这种情况下,正确的解决方案如下('Copy-Item -FromSession')。 – Marc

35

为什么不使用net useNew-PSDrive来创建新的驱动器。

新PSDrive来:创建一个新PSDrive来仅在PowerShell中environement可见:

New-PSDrive -Name Y -PSProvider filesystem -Root \\ServerName\Share 
Copy-Item BigFile Y:\BigFileCopy 

NET USE:创建操作系统的所有部分可见的新的驱动器。

Net use y: \\ServerName\Share 
Copy-Item BigFile Y:\BigFileCopy 
13

只需在远程文件需要你的凭证来获得访问的情况下,可以生成使用cmdlet的新物体“复制远程文件”,像这样

$Source = "\\192.168.x.x\somefile.txt" 
$Dest = "C:\Users\user\somefile.txt" 
$Username = "username" 
$Password = "password" 

$WebClient = New-Object System.Net.WebClient 
$WebClient.Credentials = New-Object System.Net.NetworkCredential($Username, $Password) 

$WebClient.DownloadFile($Source, $Dest) 

System.Net.WebClient对象或者如果你需要上传你可以使用一个文件UploadFile

$Dest = "\\192.168.x.x\somefile.txt" 
$Source = "C:\Users\user\somefile.txt" 

$WebClient.UploadFile($Dest, $Source) 
+1

hack detected :) –

+0

@klm_你能解释一下你的意思吗? – FastTrack

0

没有上述答案适合我。不停地收到此错误:

Copy-Item : Access is denied 
+ CategoryInfo   : PermissionDenied: (\\192.168.1.100\Shared\test.txt:String) [Copy-Item], UnauthorizedAccessException> 
+ FullyQualifiedErrorId : ItemExistsUnauthorizedAccessError,Microsoft.PowerShell.Commands.CopyItemCommand 

所以为我做:

netsh advfirewall firewall set rule group="File and Printer Sharing" new enable=yes 
从我的主机

然后我在运行框中机只是这样做\ {nanoserver的IP} \ C $

+1

您有机会遇到共享+文件系统权限问题。请记住,限制性最强的权限会赢,所以即使您有权访问NTFS文件系统层,如果共享权限限制了您,那么您将无法写入。 :) –

+0

这不适合我,仍然得到错误 –

39

从PowerShell版本5开始(包含在Windows Server 2016,downloadable as part of WMF 5 for earlier versions中),这可以通过远程进行。这样做的好处是,即使无论出​​于何种原因,您都无法访问股票,它仍然有效。

为此,启动复制的本地会话必须安装PowerShell 5或更高版本。远程会话确实需要而不是需要安装PowerShell 5 - 它适用于低至2的PowerShell版本和低于2008 R2的Windows Server版本。

从服务器A,创建一个会话到服务器B:

$b = New-PSSession B 
从A

,然后静静的:

Copy-Item -FromSession $b C:\Programs\temp\test.txt -Destination C:\Programs\temp\test.txt 

项目复制到B与-ToSession完成。请注意,在这两种情况下都使用本地路径。你必须跟踪你所在的服务器。

+2

我发现这两个服务器都没有必要安装PS 5。我仅在源服务器(Windows 10)安装了PS 5的情况下执行了成功的测试。目标是安装了默认PS的Windows Server 2012 R2($ PSVersionTable.PSVersion报告4)。 –

+2

如果您在源上使用-ToSession,则只需要安装源5。如果您在目标上使用-FromSession,则只需安装目标需要的PS 5。 –

+1

当你只安装了Hypervisor(没有服务器)时,这也适用,不需要使用会话设置共享! – dashesy