简体   繁体   English

在Powershell中调用函数时,使用与参数和存储相同的变量

[英]Using the same variable as parameter and storage when calling a function in powershell

Recently I had this problem. 最近我有这个问题。 After investigating a lot of hours, finally I discovered that the problem was in using the same variable for: 经过大量的研究,终于发现问题出在以下方面:

  • Storing the value returned by the function 存储函数返回的值
  • Pass it as a parameter to the function 将其作为参数传递给函数

So taken into account below function: 因此考虑到以下功能:

Function Create-Filter($filter)
{    
    $filter.Split(',') | ForEach-Object {"*.$($_.Trim())"}
    return
}

(Above function gets a string variable such as "csproj, vbproj" and converts it into *.csproj *.vbproj) (以上函数获取字符串变量,例如“ csproj,vbproj”,并将其转换为* .csproj * .vbproj)

...below code is not working, variable $filter used for -Include parameter does not like to Get-ChildItem and it is returning nothing: ...下面的代码不起作用,用于-include参数的变量$ filter不喜欢Get-ChildItem,它什么也不返回:

$filter = "csproj, vbproj"
$filter = Create-Filter ($filter)
Get-ChildItem "D:\Path\To\My\Root\Folder" -Include $filter -Recurse

Instead below one is working by using a different variable for storing and pass it as a parameter: 相反,下面是通过使用其他变量来存储并将其作为参数传递的方法:

$filter = "csproj, vbproj"  
$formattedfilter = Create-Filter ($filter)
Get-ChildItem "D:\Path\To\My\Root\Folder" -Include $formattedfilter -Recurse

... now Get-ChildItem works. ...现在,Get-ChildItem起作用了。

In other languages one can use the same variable for passing it as a parameter and storing the value returned by the function. 在其他语言中,可以使用相同的变量将其作为参数传递并存储该函数返回的值。 So could you explain me why in powershell this does not work if one uses the same variable? 那么,您能否解释一下为什么在Powershell中如果使用相同的变量却不起作用?

Not sure why your code won't work, as the return in your case is redundant and the result is put on the output stream. 不确定代码为什么不起作用,因为这种情况下的返回是多余的,并且结果将放在输出流中。

So the same code but refactored: 因此,相同的代码但经过了重构:

function New-Filter
{    
    param (
        [Parameter(Mandatory=$true)]
        [String[]] $filter
    )

    return $filter.Split(',') | ForEach-Object {"*.$($_.Trim())"}
}

$filter = @("csproj, vbproj")
$filter = New-Filter $filter
Get-ChildItem -Path "D:\Path\To\My\Root\Folder" -Include $filter -Recurse

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 调用 postgresql function 时如何将多个变量传递给同一个参数 - how to pass multiple variables to same parameter when calling postgresql function Powershell 在 if 语句中使用参数调用 function - Powershell calling function with parameter inside if statement Powershell中调用函数时出错 - error when calling function in powershell Powershell - 在循环、变量初始化和 if 语句中调用 function - Powershell - calling function in loops, variable initialisation and if statements 使用函数参数作为变量 - Using function parameter as variable 只有最后一个 function 在调用相同的 function 时使用不同的参数在反应 useEffect 中创建效果 - Only last function create effect when calling same function at a time with different parameter in react useEffect 函数模板 - 当使用相同类型调用时,编译器选择具有不同参数类型的函数 - Function templates - compiler choosing function with different parameter types when calling with the same type 在Powershell中调用函数时,无法对参数错误进行参数转换 - Cannot process argument transformation on parameter error while calling function in powershell 从Python调用时将参数传递给PowerShell脚本 - pass parameter to PowerShell script when calling from Python Powershell变量被赋予函数的结果和传递给函数的参数I. - Powershell variable is assigned the result of a function AND the parameter I passed to the function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM