使用 PowerShell 远程复制文件 [英] Copy file remotely with PowerShell

查看:64
本文介绍了使用 PowerShell 远程复制文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写要从服务器 A 运行的 PowerShell 脚本.我想连接到服务器 B 并将文件复制到服务器 A 作为备份.

I am writing a PowerShell script that I want to run from Server A. I want to connect to Server B and copy a file to Server A as a backup.

如果这不能完成,那么我想从服务器 A 连接到服务器 B 并将文件复制到服务器 B 中的另一个目录.

If that can't be done then I would like to connect to Server B from Server A and copy a file to another directory in Server B.

我看到了Copy-Item 命令,但我不知道如何给它一个计算机名称.

I see the Copy-Item command, but I don't see how to give it a computer name.

我以为我可以做类似的事情

I would have thought I could do something like

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

我该怎么做?

推荐答案

只需使用管理共享在系统之间复制文件.这种方式容易多了.

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;

通过使用 UNC 路径而不是本地文件系统路径,您可以帮助确保您的脚本可以从任何客户端系统执行访问那些 UNC 路径.如果您使用本地文件系统路径,那么您正在逼迫自己在特定计算机上运行脚本.

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 会话在对两个管理共享都具有权限的用户下运行时有效.

This only works when a PowerShell session runs under the user who has rights to both administrative shares.

我建议在服务器 B 上使用常规网络共享,对每个人都具有只读访问权限,只需调用(从服务器 A):

I suggest to use regular network share on server B with read-only access to everyone and simply call (from Server A):

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

这篇关于使用 PowerShell 远程复制文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆