gpt4 book ai didi

powershell - 将参数传递给函数

转载 作者:行者123 更新时间:2023-12-02 23:04:30 25 4
gpt4 key购买 nike

我试图通过向其传递参数来创建一个基于以下内容的简单函数。该函数将搜索我的命令历史记录以查找字符串 - 该命令有效:

history | Where-Object {$_.CommandLine -match 'abc'}

根据我的研究,最接近的事情是:

Function FindHistory {history | Where-Object {$_.CommandLine -match '$args'}}

但是我无法让这个(或任何变体)工作。FindHistory abc - 应返回所有以前使用过“abc”的命令。

我做错了什么?

顺便说一句,这两天我一直是 Powershell 的狂热用户 - 喜欢它:)

最佳答案

Powershell 不会扩展单引号字符串中的变量,因此您必须使用双引号字符串:

Function FindHistory {history | Where-Object {$_.CommandLine -match "$args"}}

虽然$args是所有参数的数组,所以如果您只指定一个参数,它可能会更健壮:

Function FindHistory {PARAM($searchTerm)  history | Where-Object {$_.CommandLine -match "$searchTerm"}}

关于powershell - 将参数传递给函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20110986/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com