$input应该在脚本块的上下文中进行评估,process {}例如:
function Create-Zip
{
param([string]$zipfile)
set-content $zipfile ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18))
(dir $zipfile).IsReadOnly = $false
$shellApplication = new-object -comObject Shell.Application
$zipPackage = $shellApplication.NameSpace($zipfile)
process {
foreach($item in $input)
{
$zipPackage.CopyHere($item.FullName)
Start-sleep -milliseconds 500
}
}
}
但是,如果您使用的是 PowerShell V2,这是传递与文件(字符串路径、FileInfo 等)相关的管道输入的更好方法:
function Verb-Noun
{
[CmdletBinding(DefaultParameterSetName="Path")]
param(
[Parameter(Mandatory=$true, Position=0, ParameterSetName="Path",
ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true,
HelpMessage="Path to ...")]
[ValidateNotNullOrEmpty()]
[string[]]
$Path,
[Alias("PSPath")]
[Parameter(Mandatory=$true, Position=0, ParameterSetName="LiteralPath",
ValueFromPipelineByPropertyName=$true,
HelpMessage="Path to ...")]
[ValidateNotNullOrEmpty()]
[string[]]
$LiteralPath
)
Begin { Set-StrictMode -Version latest }
Process {
if ($psCmdlet.ParameterSetName -eq "Path")
{
# In the -Path (non-literal) case we may need to resolve a wildcarded path
$resolvedPaths = @($Path | Resolve-Path | Convert-Path)
}
else
{
# Must be -LiteralPath
$resolvedPaths = @($LiteralPath | Convert-Path)
}
foreach ($rpath in $resolvedPaths)
{
Write-Verbose "Processing $rpath"
# ... Do something with the raw, resolved $rpath here ...
}
}
}