首页 > 解决方案 > 媒体协议的 PowerShell 脚本不起作用

问题描述

场景 1:没有文件与扩展名匹配
实际输出:脚本退出但没有消息
期望输出:脚本显示消息说明这一事实并退出

我没有得到想要的输出

场景 2:目标目录中存在一些文件
实际输出:脚本行 108,111 每个文件输出一个错误
所需输出:不输出错误,只跟踪未复制/移动的文件

场景3:在脚本执行期间,显示进度条
实际输出:在脚本执行后,进度条和摘要都消失了
期望输出:在脚本执行后,通过打印到shell来保持摘要

这是脚本

[CmdletBinding()]
    param
    (
    [Parameter(Mandatory=$true)]
    [string]$phoneName

    ,[Parameter(Mandatory=$true)]
    [string]$sourceFolder

    ,[Parameter(Mandatory=$true)]
    [string]$targetFolder

    ,[Parameter(Mandatory=$false)]
    [string]$filter='(\..*)$' #format '(<extension>) | (<extension>)$'

    ,[Parameter(Mandatory=$false)]
    [string]$switch = 'copy'
    )

function Get-ShellProxy
{
    if( -not $global:ShellProxy)
    {
        $global:ShellProxy = new-object -com Shell.Application
    }
    $global:ShellProxy
}

function Get-Phone
{
    param($phoneName)
    $shell = Get-ShellProxy
    # 17 (0x11) = ssfDRIVES from the ShellSpecialFolderConstants (https://msdn.microsoft.com/en-us/library/windows/desktop/bb774096(v=vs.85).aspx)
    # => "My Computer" — the virtual folder that contains everything on the local computer: storage devices, printers, and Control Panel.
    # This folder can also contain mapped network drives.
    $shellItem = $shell.NameSpace(17).self
    $phone = $shellItem.GetFolder.items() | where { $_.name -eq $phoneName }
    return $phone
}

function Get-SubFolder
{
    param($parent,[string]$path)
    $pathParts = @( $path.Split([system.io.path]::DirectorySeparatorChar) )
    $current = $parent
    foreach ($pathPart in $pathParts)
    {
        if ($pathPart)
        {
            $current = $current.GetFolder.items() | where { $_.Name -eq $pathPart }
        }
    }
    return $current
}

$phoneFolderPath = $sourceFolder
$destinationFolderPath = $targetFolder
# Optionally add additional sub-folders to the destination path, such as one based on date

$phone = Get-Phone -phoneName $phoneName
$folder = Get-SubFolder -parent $phone -path $phoneFolderPath

$items = @( $folder.GetFolder.items() | where { $_.Name -match $filter } )

if ($items)
{
    $totalItems = $items.count
    if ($totalItems -gt 0)
    {
        # If destination path doesn't exist, create it only if we have some items to move
        if (-not (test-path $destinationFolderPath) )
        {
            $created = new-item -itemtype directory -path $destinationFolderPath
        }

        Write-Verbose "Processing Path : $phoneName\$phoneFolderPath"

        if($switch -eq 'copy'){

            Write-Verbose "Copying to : $destinationFolderPath"
        }
        else{

            Write-Verbose "Moving to : $destinationFolderPath"
        }


        $shell = Get-ShellProxy
        $destinationFolder = $shell.Namespace($destinationFolderPath).self
        $count = 0;
        foreach ($item in $items)
        {
            $fileName = $item.Name

            ++$count
            $percent = [int](($count * 100) / $totalItems)
            Write-Progress -Activity "Processing Files in $phoneName\$phoneFolderPath" `
                -status "Processing File ${count} / ${totalItems} (${percent}%)" `
                -CurrentOperation $fileName `
                -PercentComplete $percent

            # Check the target file doesn't exist:
            $targetFilePath = join-path -path $destinationFolderPath -childPath $fileName

            if (test-path -path $targetFilePath)
            {
                if($switch -eq 'copy'){
                    write-error "Destination file exists - file not copied:`n`t$targetFilePath"
                }
                else{
                    write-error "Destination file exists - file not moved:`n`t$targetFilePath"
                }
            }
            else
            {
                if($switch -eq 'copy'){

                    $destinationFolder.GetFolder.copyHere($item)

                }
                else{

                    $destinationFolder.GetFolder.moveHere($item)

                }

                if (test-path -path $targetFilePath)
                {
                    # Optionally do something with the file, such as modify the name (e.g. removed phone-added prefix, etc.)
                }
                else
                {

                    if($switch -eq 'copy'){
                         write-error "Failed to copy file to destination:`n`t$targetFilePath"
                    }
                    else{
                         write-error "Failed to move file to destination:`n`t$targetFilePath"
                    }
                }
            }
        }
    }
}




标签: powershell

解决方案


推荐阅读