这里有一个问题要问你;)
我有这个功能:
function Set-DbFile {
param(
[Parameter(ValueFromPipeline=$true)]
[System.IO.FileInfo[]]
$InputObject,
[Parameter(ValueFromPipelineByPropertyName=$true)]
[scriptblock]
$Properties
)
process {
$InputObject | % {
Write-Host `nInside. Storing $_.Name
$props = & $Properties
Write-Host ' properties for the file are: ' -nonew
write-Host ($props.GetEnumerator()| %{"{0}-{1}" -f $_.key,$_.Value})
}
}
}
看着那(这$Properties
。应针对每个文件进行评估,然后进一步处理该文件和属性。
如何使用它的示例可能是:
Get-ChildItem c:\windows |
? { !$_.PsIsContainer } |
Set-DbFile -prop {
Write-Host Creating properties for $_.FullName
@{Name=$_.Name } # any other properties based on the file
}
当我复制粘贴功能时Set-dbFile
命令行并运行示例片段,一切都很好。
但是,当我将函数存储在模块中、导入它并运行示例时,$_
变量为空。有人知道为什么吗?以及如何解决呢? (也欢迎其他解决方案)
在脚本中定义的函数的结果/在命令行中键入的函数的结果:
Inside. Storing adsvw.ini
Creating properties for C:\windows\adsvw.ini
properties for the file are: Name-adsvw.ini
Inside. Storing ARJ.PIF
Creating properties for C:\windows\ARJ.PIF
properties for the file are: Name-ARJ.PIF
....
模块中定义的函数的结果:
Inside. Storing adsvw.ini
Creating properties for
properties for the file are: Name-
Inside. Storing ARJ.PIF
Creating properties for
properties for the file are: Name-
....