首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Copy-Item执行文件验证检查

使用Copy-Item执行文件验证检查
EN

Stack Overflow用户
提问于 2015-03-22 05:05:08
回答 1查看 5.9K关注 0票数 1

以下脚本在servers.txt中的服务器上创建名为指定日期的文件夹,然后将运行脚本的文件夹复制到该文件夹。

例如,它在SERVER1、SERVER2等上创建文件夹"3-22-15 (夜间)“,然后将”包含脚本的目录“复制到"3-22-15 (夜间)”。

代码语言:javascript
复制
$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\的每个文件的文件验证

我希望它输出一个错误消息与文件,没有通过验证检查,但继续复制。

我打算尝试一下这样的方法:

代码语言:javascript
复制
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

但我不确定如何实现它。

EN

回答 1

Stack Overflow用户

发布于 2015-07-14 14:46:14

我不是一个有才华的编剧,也不会在电台上演奏,但我已经把这些放在一起,它似乎起作用了。如前所述,比较的文件大小越大,运行速度就越慢。

代码语言:javascript
复制
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。

代码语言:javascript
复制
PS C:\Scripts> .\Folder-Compare.ps1 -source C:\Scripts\temp -dest C:\aaa
 C:\Scripts\temp does not match C:\aaa
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29188031

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档