如何从PowerShell脚本中打开另一个PowerShell控制台

5
在OSX上,我打开一个bash终端并输入一个PowerShell控制台。在我的PowerShell脚本中,我想打开另一个PowerShell控制台并在那里执行一个PowerShell脚本。
在Windows下,我会这样做:
Invoke-Expression ('cmd /c start powershell -Command test.ps1')

在OSX中我该如何做同样的事情?


开始 PowerShell - Mathias R. Jessen
没有帮助 :( - Jim
你是否在使用来自https://github.com/PowerShell/PowerShell的OS/X上的PowerShell 6? - lit
是的,我正在使用PS6。 - Jim
即使在Windows下,这仍然是一种糟糕的实现方式... - Joey
2个回答

1

要在 macOS 上打开一个新的终端窗口并启动 PowerShell 实例:


无法将参数传递给它:

PS> open -a Terminal $PSHOME/powershell

如果您想运行特定的命令:

不幸的是,如果您想将命令传递到新的PowerShell实例中运行,需要进行更多的工作:
实质上,您需要将命令放在一个临时的、自删除的可执行shell脚本中,通过shebang line调用该脚本:

注意:确保至少运行PowerShell Core v6.0.0-beta.6才能使此方法生效。

Function Start-InNewWindowMacOS {
  param(
     [Parameter(Mandatory)] [ScriptBlock] $ScriptBlock,
     [Switch] $NoProfile,
     [Switch] $NoExit
  )

  # Construct the shebang line 
  $shebangLine = '#!/usr/bin/env powershell'
  # Add options, if specified:
  # As an aside: Fundamentally, this wouldn't work on Linux, where
  # the shebang line only supports *1* argument, which is `powershell` in this case.
  if ($NoExit) { $shebangLine += ' -NoExit' }
  if ($NoProfile) { $shebangLine += ' -NoProfile' }

  # Create a temporary script file
  $tmpScript = New-TemporaryFile

  # Add the shebang line, the self-deletion code, and the script-block code.
  # Note: 
  #      * The self-deletion code assumes that the script was read *as a whole*
  #        on execution, which assumes that it is reasonably small.
  #        Ideally, the self-deletion code would use 
  #        'Remove-Item -LiteralPath $PSCommandPath`, but, 
  #        as of PowerShell Core v6.0.0-beta.6, this doesn't work due to a bug 
  #        - see https://github.com/PowerShell/PowerShell/issues/4217
  #      * UTF8 encoding is desired, but -Encoding utf8, regrettably, creates
  #        a file with BOM. For now, use ASCII.
  #        Once v6 is released, BOM-less UTF8 will be the *default*, in which
  #        case you'll be able to use `> $tmpScript` instead.
  $shebangLine, "Remove-Item -LiteralPath '$tmpScript'", $ScriptBlock.ToString() | 
    Set-Content -Encoding Ascii -LiteralPath $tmpScript

  # Make the script file executable.
  chmod +x $tmpScript

  # Invoke it in a new terminal window via `open -a Terminal`
  # Note that `open` is a macOS-specific utility.
  open -a Terminal -- $tmpScript

}

定义了这个函数后,你可以按照以下方式使用指定为脚本块的命令来调用PowerShell:

# Sample invocation
Start-InNewWindowMacOS -NoExit { Get-Date }

1
很高兴听到这个消息,Jim(感谢确认,@LotPings)。是的,在之前的测试版中,通过shebang行调用是有问题的;我已经添加了一条注释,说明了最低版本要求。通常情况下,一旦新版本发布,及时更新是值得的。 - mklement0

0

关于Mac上的PowerShell我什么也不知道,如果存在的话,但是要打开一个GUI应用程序,比如在Mac OS X上使用终端,你可以使用open命令:

open -a /Applications/Utilities/Terminal.app "" 将会打开一个新的空白窗口
open -a /Applications/Utilities/Terminal.app somescrip.sh 将会运行一个脚本

或者您可以制作一个AppleScript并运行它

将以下内容保存到文件中(~/OpenNewTerminal.scp):

tell application "Terminal"
    do script " "
    activate
end tell

然后你可以使用osascript运行它

osascript ~/OpenNewTerminal.scp

当然,更符合bash惯用法的方式是在子shell或后台运行

子shell:

output=$(ls)
echo $output

背景:

./command &

使用重定向输出的背景,以便它不会溢出到当前的 shell 中:

./command 2>&1 > /dev/null

我尝试了几种不同的方法,使用“open -a Terminal.app”,但没有运气。我没有尝试第二个解决方案。我想打开另一个 Powershell 控制台并从 Powershell 脚本中执行 Powershell 脚本,我不确定如何实现第二个。 - Jim

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接