首页 > 解决方案 > Copy-item 脚本的问题

问题描述

我有一个脚本需要将文件列表复制到目标服务器上的 ceraitn 目录和位置。我能够理解我需要创建一个 csv 文件,如下所示:

源和目标 CSV

我需要从您那里了解如何将文件从源位置制作到目标位置的相邻文件。有任何想法吗?

我的代码如下所示:

 # customize log file
$date = (Get-Date -Format d).ToString() | foreach {$_ -replace "/", "_"}
$time = (Get-Date)

$scriptDir = "D:\Scripts\ServerBuildToolkitT1\SingleFileUpfate\"
$logDir = "D:\Scripts\ServerBuildToolkitT1\Logs\SingleFileUpfate\"

$logFileName = "SingleFileUpfate $date.log"

$sources = @()
$destinsyions = @()

function CSV {
    Import-Csv D:\Scripts_PS\SD.csv | ForEach-Object {
        $sources += $_."Source Location"
        $destinations += $_."Destination Location"
    }
}

# this file contains the list of destination server that you want copy
# file/folder to
$computers = Get-Content "D:\Scripts_PS\ServerList.txt"

function main 
{
    foreach ($computer in $computers) {
        foreach ($destination in $destinations) {

        Write-Output "$([DateTime]::Now) Copying files update to $computer now" |
            Out-File -FilePath "$logDir\$logFileName" -Append
        Copy-Item $sources -Destination "\\$computer\$destination" -Force -Recurse -Verbose:$false
    }
}
}

csv
main

Write-Output "$([DateTime]::Now) the operation SingleFileUpdate completed successefully" |
    Out-File -FilePath "$logDir\$logFileName" -Append

我已经更新了脚本(如上所示),现在我收到以下错误“警告:未指定一个或多个标题。已使用以“H”开头的默认名称代替任何丢失的标题。”

标签: powershellscripting

解决方案


推荐阅读