出于涉及权限提升的原因,我需要能够以与最初运行时完全相同的方式调用脚本。出于这个原因,我希望能够检索用于最初调用脚本的文字,完整,未处理的命令行。最理想的是,这将表现如下:
# DoIt.ps1
Get-CommandLine | Write-Output
PS> ./DoIt.ps1 -Something something abc 123
./DoIt.ps1 -Something something abc 123
C:\> PowerShell.exe ./DoIt.ps1 -Something something abc 123
./DoIt.ps1 -Something something abc 123
但我可以处理保持PowerShell.exe
,显然它不必是一个单独的cmdlet。
根据Mathias和TheMadTechnician的建议,我发现$MyInvocation.Line
正是我正在寻找的东西。不知道谷歌为什么没有帮助,但我找到了答案。