powershell - 输出的复制项 'preview'

标签 powershell

有关 PowerShell Copy-Item 命令的快速问题。我想知道您是否有一个目录结构并想要覆盖另一个目录结构是否有办法在“预览”模式下运行 Copy-Item 命令。它会将其覆盖的文件从目录 a 输出到目录 b,但实际上并不执行 Copy-Item 命令。

感谢任何帮助或建议。

谢谢。

最佳答案

有趣的问题! 这是我尝试在 Powershell 中完成这一切,因此不需要 RoboCopy。

function Copy-Preview {
    [CmdletBinding(DefaultParameterSetName = 'ByPath')]
    param(
        [Parameter(ValueFromPipeline = $true, Mandatory = $true, ParameterSetName = 'ByPath', Position = 0)]
        [ValidateScript({ Test-Path $_ })]
        [string]$Path,

        [Parameter(ValueFromPipeline = $true, Mandatory = $true, ParameterSetName = 'ByLiteralPath', Position = 0)]
        [ValidateScript({ Test-Path $_ })]
        [string]$LiteralPath,

        [Parameter(ValueFromPipeline = $true, Mandatory = $true, Position = 1)]
        [string]$Destination,

        [string]$Filter = $null,
        [string]$Include = $null,
        [string]$Exclude = $null,
        [switch]$Recurse,
        [switch]$Force
    )

    if ($PSCmdlet.ParameterSetName -eq 'ByLiteralPath') { $Path = $LiteralPath }

    # determine if $Path and $Destination hold a file or a directory
    $srcIsFolder = (Test-Path $Path -PathType Container -ErrorAction SilentlyContinue)
    # cannot use Test-Path here because then the path has to exist.
    # assume if it has an extension the path is a file, otherwise a directory
    # NOTE:
    # This is certainly not fullproof, so to avoid problems, always make sure 
    # the destination ends with a backslash if a directory is intended.
    $destIsFolder = (-not ([System.IO.Path]::HasExtension($Destination)))

    if ($destIsFolder -and !(Test-Path $Destination -PathType Container)) {
        Write-Host "Destination path does not exist yet. All files from '$Path' will be copied fresh" -ForegroundColor Green
        return
    }
    elseif ($srcIsFolder -and (!$destIsFolder)) {
        # should not happen: source is a directory, while the destination is a file..
        Write-Error "When parameter Path points to a directory, the Destination cannot be a file.."
        return
    }

    $count = 0
    if ($srcIsFolder -and $destIsFolder) {
        # Both the source and the destinations are folders
        # make sure both paths are qualified for .Replace() further down
        if (-not $Path.EndsWith("\")) { $Path += "\" }
        if (-not $Destination.EndsWith("\")) { $Destination += "\" }

        $splat = @{
            Filter  = $Filter
            Include = $Include
            Exclude = $Exclude
            Recurse = $Recurse
            Force   = $Force
        }
        # add either Path or LiteralPath to the parameters as they are mutually exclusive
        if ($PSCmdlet.ParameterSetName -eq 'ByPath') { $splat.Path = $Path }
        else { $splat.LiteralPath = $LiteralPath }

        $srcFiles  = Get-ChildItem @splat | Select-Object -ExpandProperty FullName

        # reuse the splat parameters hash for the destination, but change the Path
        if ($splat.LiteralPath) {($splat.Remove("LiteralPath"))}
        $splat.Path = $Destination

        $destFiles = Get-ChildItem @splat | Select-Object -ExpandProperty FullName

        foreach ($srcItem in $srcFiles) { 
            $destItem = $srcItem.Replace($Path, $Destination)
            if ($destFiles -contains $destItem) {
                Write-Host "'$destItem' would be overwritten"
                $count++
            }
        }
    }
    elseif (!$srcIsFolder) {
        # the source is a file
        if (!$destIsFolder) {
            # the destination is also a file
            if (Test-Path $Destination -PathType Leaf) {
                Write-Host "'$Destination' would be overwritten"
                $count++
            }
        }
        else {
            # source is file, destination is a directory
            $destItem = Join-Path $Destination (Split-Path $Path -Leaf)
            if (Test-Path $destItem -PathType Leaf) {
                Write-Host "'$destItem' would be overwritten"
                $count++
            }
        }
    }

    $msg = "$count item{0} would be overwritten by Copy-Item" -f $(if ($count -ne 1) { 's' })
    $dash = "-" * ($msg.Length)
    Write-Host "$dash`r`n$msg" -ForegroundColor Green
}

关于powershell - 输出的复制项 'preview',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51755304/

相关文章:

powershell - 在Powershell中打印有用的变量值(特别是涉及$ null/空字符串)

sql-server - 在PowerShell中调用存储过程以插入SQL Server数据库

powershell - 将参数传递给作业初始化脚本

powershell - Powershell:将Foreach用于Get-EC2Snapshot

Powershell 2.0 远程加载 .Net 4.0 dll

powershell - 测试是否是 “False Like”

python - 设置 FLASK_DEBUG=1 不适用于 Powershell

powershell - Powershell字符串格式在控制台中不起作用

powershell - 文件名到CSV

powershell - 使用Powershell脚本发送SNMP陷阱