PowerShell脚本,用于从一台服务器的多个文件夹复制同一文件并粘贴到另一台服务器中

PowerShell脚本,用于从一台服务器的多个文件夹复制同一文件并粘贴到另一台服务器中,powershell,Powershell,我在多个文件夹中有一个名为“file.config”的文件(D:\Auth0\file.config,D:\Auth1\file.config,D:\Auth2\file.config…)。我想从所有Auth文件夹复制此文件,并使用PowerShell将其粘贴到另一台服务器上 我的想法如下: $Session = New-PSSession -ComputerName "S01" -Credential "domain\user" Copy-Item "D:\Auth0\file.config"

我在多个文件夹中有一个名为
“file.config”
的文件
(D:\Auth0\file.config,D:\Auth1\file.config,D:\Auth2\file.config…)
。我想从所有Auth文件夹复制此文件,并使用PowerShell将其粘贴到另一台服务器上

我的想法如下:

$Session = New-PSSession -ComputerName "S01" -Credential "domain\user"
Copy-Item "D:\Auth0\file.config" -Destination "\C:\Backups" -FromSession $Session
此脚本仅复制Auth0文件夹中的文件,并粘贴到同一服务器的C驱动器中。要从其他文件夹复制,我必须更改文件夹名称并再次运行脚本

我想运行脚本,从所有Auth文件夹复制此文件并粘贴到另一台服务器中。 请帮忙


谢谢

您不能在脚本中多次运行
Copy Item
命令吗

$Session = New-PSSession -ComputerName "S01" -Credential "domain\user"
Copy-Item "D:\Auth0\file.config" -Destination "\C:\Backups" -FromSession $Session
Copy-Item "D:\Auth1\file.config" -Destination "\C:\Backups" -FromSession $Session
Copy-Item "D:\Auth2\file.config" -Destination "\C:\Backups" -FromSession $Session
好吧,如果我理解正确的话,这应该是可行的:

$DestinationSession = New-PSSession -ComputerName "S01" -Credential "domain\user"
# Is S01 you destination server? And from where are you running this script?
# See the difference between -ToSession and -FromSession
foreach($n in 1..1000)
{
    Copy-Item "D:\Auth$($n)\file.config" -Destination "C:\Backups" -ToSession $DestinationSession
}

大约有1000个文件夹,所以这会有点费时。我可以运行这个,但它将粘贴到同一个服务器上。如何粘贴到另一台服务器?@Aish
Copy Item“D:\Auth0\file.config”-Destination('\\{0}\C$\Backups\'-f$svr)-FromSession$Session
,其中
$svr
是目标服务器名称。您需要UNC路径。@Theo我使用了:
从会话$Session复制项目“D:\Auth0\file.config”-目的地('\\S02\C$\Backups\'-f$S02)-但它不起作用。@Aish不,这当然不起作用。
{0}
的全部含义是它被变量
$svr
中的一个目标服务器替换。看看是怎么回事works@Starlord,这解决了我的部分问题。非常感谢你。