如何使PowerShell在脚本文件中提示命令?

huangapple go评论69阅读模式
英文:

How to have Powershell prompt for commands inside a script file?

问题

让我们假设我有一个脚本文件,并且在其中间(或在末尾或其他地方),我希望用户能够像不运行脚本一样访问当前会话,并随意开始编写命令。

我知道这是可能的,可以在脚本的末尾使用 -NoExit 参数运行 PowerShell。但是:

  • 您必须手动调用 powershell/pwsh 二进制文件并为每次调用指定参数
  • 无法在脚本中间执行此操作

我知道我可以通过执行以下方式打开新会话,类似这样执行 PowerShell 二进制文件:

Some-Command
echo "current PID=$PID" # 假设 PID 为 12345
$var = 123
& pwsh
Another-Command

但我想访问 当前 会话。也就是说,在脚本期间 $PID12345,并且当我输入命令时也是如此。这意味着我可以访问所有变量及其当前值(所以我可以读取 $var 并获得 123),然后在我想要时退出,并继续执行脚本。

我查看了 Enter-PSHostProcess,但它无法连接到当前进程,而且(显然)无法访问变量(我认为这是出于安全原因)。

这一切,总的来说,有可能吗?谢谢。 PS: 如果不清楚,我的意图不仅仅是“在执行后保持脚本打开”,我知道如何做到;也不是“提示用户输入”(意思是数据),我知道如何做到。

英文:

let's say i have a script file, and in the middle of it (or at the end or whatever) i'd like for the user to be able to access the current session as if not running a script, and start writing commands at his will.

I know this is possible, at the end of the script, by running powershell with the -NoExit parameter. But:

  • you have to manually invoke the powershell/pwsh binary and specify the parameter for every invocation
  • you can't do it in the middle of the script

I know i could simply open a new session by executing the powershell binary like this:

Some-Command
echo "current PID=$PID" # let's say PID is 12345
$var = 123
& pwsh
Another-Command

but i would like to access the current session. Meaning that $PID is the 12345 during the script, and when i'm entering commands. Meaning that i could i access all of the variables with their current values (so i could read $var and get 123), then exit whenever i want, and go on with the script.

I looked into Enter-PSHostProcess but it can't connect to the current process, and (apparently) doesn't have access to the variables (i think this is for security reasons).

Is all of this, at all, possible?

thanks

PS: if it's not clear, my intent is not simply to "keep a script open after execution", i know how to do that; and it's not to "prompt for user input" (meaning data), i know how to do that

答案1

得分: 1

以下是翻译好的部分:

至今为止我认为最好的解决方案是
```powershell
while($true){Invoke-Expression $(Read-Host $(prompt))}
 # 或
while($true){Invoke-Expression $(Read-Host 'Custom Prompt> ')}

这看起来相当丑陋:正如 @olaf 指出的,invoke-expression 应该尽量避免使用

(我也担心这种方法在某些方面可能不完全有效?比如作用域问题,管道/中断问题,或其他问题)

英文:

My best solution by far is

while($true){Invoke-Expression $(Read-Host $(prompt))}
 # or
while($true){Invoke-Expression $(Read-Host 'Custom Prompt> ')}

which is pretty ugly: as @olaf pointed out, invoke-expression is to be avoided like hell

(also i fear that this might not be fully functional in some way? like scoping problems, or piping/interrupt problems, or whatever)

答案2

得分: 0

完全不同的可能解决方案[如@mclayton在这里建议的][1]使用断点

```powershell
function debug_break() {}
Get-PSBreakpoint | Remove-PSBreakpoint
Set-PSBreakpoint -Command debug_break

Write-Output '111'
$xxx = 999
debug_break # 在这里我可以看到$xxx的值是999,然后我可以输入"continue"来继续执行脚本
Write-Output '222'

<details>
<summary>英文:</summary>

Totally different possible solution [as suggested here by @mclayton][1], using breakpoints:

```powershell
function debug_break() {}
Get-PSBreakpoint | Remove-PSBreakpoint
Set-PSBreakpoint -Command debug_break


Write-Output &#39;111&#39;
$xxx = 999
debug_break # here i can see $xxx being 999, then i can type &quot;continue&quot; to go on with the script
Write-Output &#39;222&#39;

huangapple
  • 本文由 发表于 2023年4月11日 16:34:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/75983899.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定