以下脚本在servers.txt中的服务器上创建名为指定日期的文件夹,然后将运行脚本的文件夹复制到该文件夹。
例如,它在SERVER1、SERVER2等上创建文件夹"3-22-15 (夜间)“,然后将”包含脚本的目录“复制到"3-22-15 (夜间)”。
$CurrentLocation = get-location
$DeploymentDate = "3-22-15 (night)"
Foreach($server in get-content "servers.txt"){
New-Item -ErrorAction SilentlyContinue -ItemType directory -Path \\$server\C$\Deployments\$DeploymentDate
copy-item -Path $CurrentLocation -Destination \\$server\C$\Deployments\$DeploymentDate\ -ErrorAction SilentlyContinue -recurse
}如何修改脚本以包括复制到\\$server\C$\Deployments\$DeploymentDate\的每个文件的文件验证
我希望它输出一个错误消息与文件,没有通过验证检查,但继续复制。
我打算尝试一下这样的方法:
function SafeCopy ($SourcePath,$DestinationPath,$SourceFileName) {
#MD5 Check Function
function Check-MD5 ($FilePath) {
$md5=New-Object -TypeName System.Security.Cryptography.MD5CryptoServiceProvider
$hash=[System.BitConverter]::ToString($md5.ComputeHash([System.IO.File]::ReadAllBytes($FilePath)))
Return $hash
} # EndOf function Check-MD5
$MD5Source=Check-MD5 $SourcePath
Copy-Item $SourcePath\$SourceFileName $DestinationPath
$MD5Destination=Check-MD5 $DestinationPath
if (Test-Path $DestinationPath) {
if ($MD5Destination -match $MD5Source) {
Write-Host "`nThe file `"$SourcePath`" has been copied in `"$DestinationPath`" successfully.`n" -ForegroundColor DarkGreen
} else {
Write-Host "`nThe file `"$SourcePath`" has been copied in `"$DestinationPath`" but the CRC check failed!`n" -ForegroundColor DarkRed
}
} else {
Write-Host "`nThe file `"$SourcePath`" has not been copied in `"$DestinationPath`"!`n" -ForegroundColor DarkRed
}
} # EndOf function SafeCopy但我不确定如何实现它。
发布于 2015-07-14 14:46:14
我不是一个有才华的编剧,也不会在电台上演奏,但我已经把这些放在一起,它似乎起作用了。如前所述,比较的文件大小越大,运行速度就越慢。
Param
(
[parameter(Mandatory=$true,Position=1,ValueFromPipeLine=$true)][string]$source,
[parameter(Mandatory=$true,Position=2,ValueFromPipeLine=$true)][string]$dest
)
$countsource = @(Get-ChildItem -Path $source -Directory)
$countdest = @(Get-ChildItem -Path $dest -Directory)
IF($countsource = $countdest){
"$source equals $dest"
}
Else{
"$source does not match $dest"
}示例用法和输出。TODO:如果文件夹不具有相同数量的项(子文件夹和文件),则添加文件夹项计数比较、警告或中断。
因此,复制文件夹,然后在复制完成后作为函数运行Folder-Compare。
PS C:\Scripts> .\Folder-Compare.ps1 -source C:\Scripts\temp -dest C:\aaa
C:\Scripts\temp does not match C:\aaahttps://stackoverflow.com/questions/29188031
复制相似问题