2017-07-26 62 views
0

我写了一个脚本,它看起来像是在尝试将文件从一个位置复制到多个Windows服务器时要求输入。将文件复制到多个服务器的脚本

我在这里做错了什么?我只是想在没有任何交互的情况下执行脚本,它应该将文件从源复制到多个服务器上的目标。

点脚本到文本文件

$Computers = Read-Host "C:\File Copy\Source Server" 

设置文件位置EI c中的varible:\ TEMP \ File.xxx

$Source = Read-Host "C:\File Copy\prod.csv" 

设置varible的文件目的地

$Destination = Read-Host "C:\File copy\Servers" 

在屏幕上显示计算机名称

Get-Content $Computers | foreach {Copy-Item $Source -Destination \\$_\c$\$Destination} 
+0

您不需要读取主机。不知道你在最后一行尝试做什么,但... – bunzab

回答

2

Read-Host从控制台主机获取输入。如果不想提示,只需删除这些变量并将变量设置为等于路径字符串。

另外,您的$Computers变量看起来不像它指向的计算机名称为Get-Content的文件。其次,您的目标变量看起来不像是与UNC路径正确连接的东西\\$_\C$我已更新脚本以解决这两个问题。

# Point the script to a text file with a list of computers 
$Computers = "C:\File Copy\Source Server\ComputerList.txt" 

# Sets the variable for the source file location 
$Source = "C:\File Copy\prod.csv" 

# Sets the variable for the file destination 
$Destination = "File copy\Servers" 

# Get the content of $computers and copy Source to Destination 
Get-Content $Computers | ForEach-Object {Copy-Item $Source -Destination (Join-Path "\\$_\c`$\" $Destination)} 
+0

在你的例子中值得使用类似'“C:\ File Copy \ SourceServerList.txt”'的东西,使它明显是一个需要的文件? –

+0

@JamesC。我同意,很难知道是什么意图,但我已经用我认为的意图更新了脚本 – BenH

相关问题