以路径为递归文件名的Powershell脚本

1530阅读 0评论2009-10-08 零二年的夏天
分类:WINDOWS


#===========================

#

#Name:

#Author:PopZslam

#Description:

#Version:

#

#===========================



param(
    [String]$LOCAL:sourceDirectory=$input,
    [String]$LOCAL:destinationDirectory=$(Get-Location)
)

#===========================

#    Variables

#===========================


$LOCAL:notifyMessage=@(
    "请指定一个目录名。",
    "请指定目标目录名。",
    "您指定的参数不是目录:$LOCAL:sourceDirectory",
    "目标磁盘没有足够的剩余空间。",
    "您提供的参数不完整。"
)

$LOCAL:destinationDirectorySize=0
$LOCAL:sourceDirectorySize=0
$LOCAL:newName=""
$LOCAL:prefixString=""
#===========================

#    Functions

#===========================


function    get-freespace
{
    param([string]$LOCAL:destinationDirectory=(get-location))
    if($LOCAL:destinationDirectory.Trim() -eq ""){
        $LOCAL:destinationDirectory=(Get-Location);
    }
    
    $LOCAL:driveLeter=split-path -Path "$LOCAL:destinationDirectory" -qualifier
    $LOCAL:t=Get-WmiObject -Class Win32_LogicalDisk -Filter "DeviceID='$LOCAL:driveLeter'"
    foreach ($_ in $LOCAL:t){
        return $_.freespace
    }
}

#===========================

#    Main Part

#===========================


# Check the parameter

if((($LOCAL:sourceDirectory).trim() -eq "") ){
    $LOCAL:notifyMessage[4]
    "command -source -destination"
    "command -source 'c:\source' -destination 'd:\destination'"
    exit
}

$LOCAL:destinationDirectorySize=get-freespace($LOCAL:destinationDirectory)

if (Test-Path -Path $LOCAL:sourceDirectory -PathType Container){
    $LOCAL:sourceDirectorySize=(Get-ChildItem -Force -Recurse -Path $LOCAL:sourceDirectory|Measure-Object -Property length -Sum).sum
    if ($LOCAL:sourceDirectorySize -ile $LOCAL:destinationDirectorySize){
        foreach ($_ in (Get-ChildItem -Force -Recurse -Path $LOCAL:sourceDirectory) ){
            if (Test-Path -Path $_.fullname -PathType Leaf){
                $LOCAL:prefixString=Split-Path -Path $_.fullname -Parent |Split-Path -Leaf;
                $LOCAL:newName=$LOCAL:prefixString+"-"+$_.name
                Copy-Item -Path $_.fullname -Destination $LOCAL:destinationDirectory\$LOCAL:newName
            }
        }
    }else{
        throw $LOCAL:notifyMessage[3]
    }
}else{
    throw $LOCAL:notifyMessage[2]
}


上一篇:Windows Server 2003 Access-based Enumeration
下一篇:查看主板设备使用情况