English 中文(简体)
用 PowerShell 远程复制文件
原标题:Copy file remotely with PowerShell
最佳回答

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

Copy-Item -Path \serverbc$programs	emp	est.txt -Destination \serverac$programs	emp	est.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.

问题回答

在WMF 5 中,从 < power> Powershell 版本 5 开始 (包含在 Windows Server 2016, < a href="https://www.microsoft.com/download/details.aspx?id=50395" rel=“nreferrer" > download,作为WMF 5 中前版本的一部分 ) 的版本 < /a>, 重新移动是可能的。 这样做的好处是, 即使出于任何原因, 您可以访问共享, 也可以工作 。

要做到这一点,启动复制的本地会话必须安装 PowerShell 5 或更高。远程会话确实需要安装 Powershell 5 -- -- 它与Powershell 低至 2 的版本和Windows Server 低至 2008 R2. [1]

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

$b = New-PSSession B

然后,还是从A:

Copy-Item -FromSession $b C:Programs	emp	est.txt -Destination C:Programs	emp	est.txt

复制项目到 B 时使用 < code>- to Session 完成。 请注意, 本地路径在两种情况下都使用; 您必须跟踪您正在使用的服务器 。


[1:当复制或复制到仅拥有PowerShell 2的远程服务器时,请注意中的位置,在撰写时,该错误意味着重复复制文件与 -Tsession 不起作用,显然复制根本不与 /session 有效。

使用 net use New-PSDrive 创建新驱动器:

New-PsDrive: 创建新的 PsDrive, 只能在 PowerShell 环境中可见 :

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

Net 使用: 在操作系统的所有部分创建可见的新驱动器 。

Net use y: \ServerNameShare
Copy-Item BigFile Y:BigFileCopy

仅当远程文件需要您的证书才能访问时, 您可以使用 cmdlet new- Object 生成一个 System. Net. WebClient 对象, 比如“ 远程复制文件”, 这样可以生成一个 System. Net. WebClient 对象 。

$Source = "\192.168.x.xsomefile.txt"
$Dest   = "C:Usersusersomefile.txt"
$Username = "username"
$Password = "password"

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

$WebClient.DownloadFile($Source, $Dest)

如果您需要上传文件, 您可以使用上传文件 :

$Dest = "\192.168.x.xsomefile.txt"
$Source   = "C:Usersusersomefile.txt"

$WebClient.UploadFile($Dest, $Source)

上述答案的 " 坚固 " / " 坚固 " 对我来说是行不通的。

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

故你为我而制服了这头牛:

netsh advfirewall firewall set rule group="File and Printer Sharing" new enable=yes

然后从我的东道主 我的机器在运行箱 我刚刚这样做了:

\{IP address of nanoserver}C$




相关问题
Mutually exclusive powershell parameters

SCENARIO I m writing a cmdlet for Powershell 2.0 using Visual Studio 2008 and .NET 3.5 the cmdlet requires 3 arguments. my intended grammar of the cmdlet is something like this: cmdletname [foo|...

Run a program from PowerShell with timeout

I ll write a script that runs a program and wait for it finished. But if the program is not finished within a specified time I want that the program is killed.

How to transpose data in powershell

I have a file that looks like this: a,1 b,2 c,3 a,4 b,5 c,6 (...repeat 1,000s of lines) How can I transpose it into this? a,b,c 1,2,3 4,5,6 Thanks

Powershell v2 remoting and delegation

I have installed Powershell V2 on 2 machines and run Enable-PsRemoting on both of them. Both machines are Win 2003 R2 and are joined to the same active directory domain and I can successfully run ...

PowerShell -match operator and multiple groups

I have the following log entry that I am processing in PowerShell I m trying to extract all the activity names and durations using the -match operator but I am only getting one match group back. I m ...