如何将参数移交给Power Shell脚本?

时间:2015-07-14 13:35:02

标签: powershell

我有一个电源shell脚本来调整一些图像的大小。现在我想将一些参数从一个脚本切换到另一个脚本以调整某个文件夹中的所有图像。我做错了什么:

Param ( [Parameter(Mandatory=$True)] [ValidateNotNull()] $imageSource,
    [Parameter(Mandatory=$True)] [ValidateNotNull()] $imageTarget,
    [Parameter(Mandatory=$true)][ValidateNotNull()] $quality,
    [Parameter(Mandatory=$true)][ValidateNotNull()] $canvasWidth,
    [Parameter(Mandatory=$true)][ValidateNotNull()] $canvasHeight
    )


    if (!(Test-Path $imageSource)){throw( "Cannot find the source image")}
    if(!([System.IO.Path]::IsPathRooted($imageSource))){throw("please enter a full path for your source path")}
    if(!([System.IO.Path]::IsPathRooted($imageTarget))){throw("please enter a full path for your target path")}
    if(!([System.IO.Path]::IsPathRooted($canvasWidth))){throw("please enter your maximum width")}
    if(!([System.IO.Path]::IsPathRooted($canvasHeight))){throw("please enter your maximum height")}
    if ($quality -lt 0 -or $quality -gt 100){throw( "quality must be between 0 and 100.")}

    [void][System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
    $bmp = [System.Drawing.Image]::FromFile($imageSource)

    #hardcoded canvas size...
    #$canvasWidth = 5
    #$canvasHeight = 5

    #Encoder parameter for image quality
    $myEncoder = [System.Drawing.Imaging.Encoder]::Quality
    $encoderParams = New-Object System.Drawing.Imaging.EncoderParameters(1)
    $encoderParams.Param[0] = New-Object System.Drawing.Imaging.EncoderParameter($myEncoder, $quality)
    # get codec
    $myImageCodecInfo = [System.Drawing.Imaging.ImageCodecInfo]::GetImageEncoders()|where {$_.MimeType -eq 'image/jpeg'}

    #compute the final ratio to use
    $ratioX = $canvasWidth / $bmp.Width;
    $ratioY = $canvasHeight / $bmp.Height;
    $ratio = $ratioY
    if($ratioX -le $ratioY){
    $ratio = $ratioX
    }

    #create resized bitmap
    $newWidth = [int] ($bmp.Width*$ratio)
    $newHeight = [int] ($bmp.Height*$ratio)
    $bmpResized = New-Object System.Drawing.Bitmap($newWidth, $newHeight)
    $graph = [System.Drawing.Graphics]::FromImage($bmpResized)

    $graph.Clear([System.Drawing.Color]::White)
    $graph.DrawImage($bmp,0,0 , $newWidth, $newHeight)

    #save to file
    $bmpResized.Save($imageTarget,$myImageCodecInfo, $($encoderParams))
    $bmpResized.Dispose()
    $bmp.Dispose()

我用来执行脚本的脚本:

Get-ChildItem .\img -Recurse -Include *.png | Foreach-Object{
   $newName = $_.FullName.Substring(0, $_.FullName.Length - 4) + "_resized.png"
   .\ImageResize.ps1 $_.FullName $newName 100 3 16
   }

这是我得到的错误:

> Get-ChildItem : please enter your maximum width In
> C:\Users\me\Desktop\Scripte\Image_Resize\Image.ps1:1 Zeichen:1
> + Get-ChildItem .\img -Recurse -Include *.png | Foreach-Object{
> + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>     + CategoryInfo          : OperationStopped: (please enter your maximum width:String) [Get-ChildItem], RuntimeException
>     + FullyQualifiedErrorId : please enter your maximum width,Microsoft.PowerShell.Commands.GetChildItemCommand

1 个答案:

答案 0 :(得分:3)

如评论中所述,for没有任何意义。该方法旨在接收表示路径的字符串参数。

停止使用[System.IO.Path]::IsPathRooted($canvasWidth)

等数字参数调用[System.IO.Path]::IsPathRooted()

PowerShell还支持validation attributes以简化输入验证,无需您手动内联:

$canvasWidth

查看脚本专家帖子Simplify Your PowerShell Script with Parameter Validation

相关问题