批处理中,传递的参数可以与%1一起使用,并且可以继续计数。可以说我有以下“ batch.bat”脚本:
@ echo off
echo %1
pause>nul
如果我从cmd调用此命令,例如:call batch.bat hello
,它将在控制台中输出“ hello”。
ps中是否有做相同事情的变量?
编辑
我发现有胡言乱语,但似乎有点不自然。
$CommandLine = "-File `"" + $MyInvocation.MyCommand.Path + "`" " + $MyInvocation.UnboundArguments
Start-Process -FilePath PowerShell.exe -Verb Runas -ArgumentList $CommandLine
Exit
}
也许还有更优雅的东西吗?
PowerShell具有automatic variable $args
,用于存储传递给脚本的所有参数(除非为脚本定义了参数)。可以通过索引访问各个参数(第一个参数为$args[0]
,第二个参数为$args[1]
,依此类推。)>
但是,通常建议define parameters控制脚本应接受的参数,例如
[CmdletBinding()] Param( [Parameter(Mandatory=$true)] [string]$First, [Parameter(Mandatory=$false)] [integer]$Second = 42 )
具有很多优点,包括(但不限于):
根据@ Kamil-leis的建议查找$args
。