【发布时间】:2020-04-29 01:02:39
【问题描述】:
我正在尝试使用一个函数启用我的 powershell 配置文件脚本,该函数可以让我在当前的 powershell 终端会话中执行文字和通配符搜索以查找函数的存在。
在我的 powershell 配置文件脚本 [ $env:userprofile\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1 ] 中,我创建了以下函数。
function Get-Fnc {
#Get-ChildItem function:\ | Where-Object { $_.Name -like "$args" }
Get-ChildItem function:\$args
}
使用注释掉的行 Get-ChildItem function:\ | Where-Object { $_.Name -like "$args" } 不起作用,即使我可以在命令行上使用它,例如Get-ChildItem function:\ | Where-Object { $_.Name -like "get-*" } 它按预期工作。使用未注释的行 Get-ChildItem function:\$args 在配置文件脚本函数和命令行中都有效,例如Get-ChildItem function:\get-*.
在网上和 stackoverflow 上搜索,我无法找到有关使用输出管道 | 到另一个 cmdlet 和/或在函数中使用 Where-Object cmdlet 来确定如何制作的任何细节这行得通。当已知相同的东西在命令行上工作时,关于如何使输出通过管道传输到 where-object 在脚本函数中工作的任何见解?
更新 除了提供的答案之外,solutin 还能够使用以下内容
function Get-Fnc {
$argsFncScope = $args # works because we make function scoped copy of args that flows down into Where-Object script block / stack frame
Write-Host "function scoped args assigned variable argsFncScope = $argsFncScope and count = $($argsFncScope.Count) and type = $($argsFncScope.GetType().BaseType)"
Get-ChildItem function:\ | Where-Object { $_.Name -like "$argsFncScope" }
}
调试输出
get-fnc *-env
[DBG]: PS C:\Users\myusrn\Documents\WindowsPowerShell>
function scoped args assigned variable argsFncScope = *-env and count = 1 and type = array
[DBG]: PS C:\Users\myusrn\Documents\WindowsPowerShell>
CommandType Name Version Source
----------- ---- ------- ------
Function Get-Env
【问题讨论】:
-
我怀疑您遇到了范围问题。大多数时候,一个函数有它自己的作用域,而你的函数可能不知道
$Args在那个特定的作用域中拥有什么。 ///// 同样,$Args是保留的 $var 名称之一[并且是一个数组]。除非您确定它在任何给定情况下都是正确的,否则您可能不会使用该确切名称。
标签: function powershell pipe