2017-07-26 140 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

相關問題