powershell - 将文件从一个源并行复制到多个目标

标签 powershell powershell-2.0

我正在尝试编写一个Microsoft powershell脚本,该脚本基于配置文件将文件从单个源并行复制到多个目标。配置文件是一个CSV文件,如下所示:

服务器类型
server1,生产
server2,暂存

我的脚本使用一个参数(。\ myscript.ps1 buildnumber)进行调用,但是它似乎并未真正删除或复制文件。

我确定我的copy-item和remove-item代码在我独立测试它们后都可以使用,但是我认为这可能与我使用脚本块的方式或我如何使用Start-job有关。

谁能帮助我了解为什么这不起作用?

谢谢
布拉德

<#  
File Deployment Script
#>
#REQUIRES -Version 2

param($build)
$sourcepath = "\\server\software\$build\*" 

$Config = import-csv -path C:\config\serverlist.txt 

$scriptblock1 = {
    $server = $args[0]
    $destpath1 = "\\$server\share\Software Wizard\"
    $destpath2 = "\\$server\share\Software Wizard V4.9XQA\"

    remove-item "$destpath1\*" -recurse -force
    remove-item "$destpath2\*" -recurse -force

    copy-item $sourcepath -destination $destpath1 -recurse -force
    copy-item $sourcepath -destination $destpath2 -recurse -force
}

$scriptblock2 = {
    $server = $args[0]
    $destpath = "\\$server\share\Software Wizard\"
    #remove-item "$destpath\*" -recurse -force 
    copy-item $sourcepath -destination $destpath -recurse -force
}

foreach ($line in $Config) {
    $server = $line.Server
    $type = $line.Type

    if ($type -match "Staging") {

    Write-Host "Kicking job for $server off"
    start-job -scriptblock $scriptblock2 -ArgumentList $server
}


if ($type -match "Production") {
            Write-Host "Kicking job for $server off"
    start-job -scriptblock $scriptblock2 -ArgumentList $server

}
}

最佳答案

从start-job调用时,您的脚本块无权访问在其外部声明的变量。因此$ scriptblock1和$ scriptblock2无法看到$ sourcepath。

关于powershell - 将文件从一个源并行复制到多个目标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5113709/

相关文章:

powershell - 在Powershell下在后台运行docker

powershell - 在任务计划程序Windows 2008 Server中计划Powershell脚本的问题

powershell - 确定当前 PowerShell 脚本位置的最佳方法是什么?

.net - 确定 Windows 当前是否正在播放声音

powershell - powershell 脚本格式表输出中的颜色词

arrays - 在Powershell中,如何通过 “value”而不是 “reference”将对象存储在数组中?

arrays - 在关联数组上排序

powershell - Powershell选择不同的异常(exception)

powershell - 将Powershell对象属性的名称值转换为字符串

xml - 当我尝试将值设置为 xml 属性时,powershell 奇怪地编码了符号