Powershell 使用-Path参数和$input中的一个或两个参数的命令?
我想编写一个PowerShell脚本,它将接受要处理的文件列表,或者能够从stdin获取其输入。该命令将接收文本并生成拉丁语。(你是对的,我实际上在做其他事情,但这是一个场景。) 我想使-Path参数不是必需的Powershell 使用-Path参数和$input中的一个或两个参数的命令?,powershell,Powershell,我想编写一个PowerShell脚本,它将接受要处理的文件列表,或者能够从stdin获取其输入。该命令将接收文本并生成拉丁语。(你是对的,我实际上在做其他事情,但这是一个场景。) 我想使-Path参数不是必需的 [Parameter(Mandatory=$false, HelpMessage='input filename')] [string[]]$Path 我还没有找到一个可以同时使用-Path参数和$input的解决方案。有可能吗?我在这里看到的一个明显问题是,在第一种情况下,您传入的是
[Parameter(Mandatory=$false, HelpMessage='input filename')]
[string[]]$Path
我还没有找到一个可以同时使用-Path参数和$input的解决方案。有可能吗?我在这里看到的一个明显问题是,在第一种情况下,您传入的是文件名,在第二种情况下,您传入的是文件内容。如果将这两个变量都发送到同一个变量,则脚本内部将出现问题 我是否可以建议另一种方法:
function Edit-Piglatin
{
param(
[Parameter(Mandatory=$false,
ValueFromPipeline=$true,
Position=0)]
[string[]]$Content,
[Parameter(Mandatory=$false,
ValueFromPipeline=$false,
Position=1)]
[string[]]$Path
)
#named parameter $path will get the input for the filename
#values from pipeline will go to automatically go to $content
if ($Path)
{
#use this as input
}
elseif ($Content)
{
#use this as input
}
else
{
#no input
}
}
技巧在于valuefrompipline=$true
,以及Position=0
。现在,通过一个简单的if-else
条件,您可以确定在函数中处理哪个变量
function Edit-Piglatin
{
param(
[Parameter(Mandatory=$false,
ValueFromPipeline=$true,
Position=0)]
[string[]]$Content,
[Parameter(Mandatory=$false,
ValueFromPipeline=$false,
Position=1)]
[string[]]$Path
)
#named parameter $path will get the input for the filename
#values from pipeline will go to automatically go to $content
if ($Path)
{
#use this as input
}
elseif ($Content)
{
#use this as input
}
else
{
#no input
}
}