如何从 PowerShell 脚本打开另一个 PowerShell 控制台
How to open another PowerShell console from a PowerShell script
在 OSX 中,我打开 bash 终端并进入 PowerShell 控制台。
在我的 PowerShell 脚本中,我想打开另一个 PowerShell 控制台并在那里执行 PowerShell 脚本。
在 Windows 下,我会
Invoke-Expression ('cmd /c start powershell -Command test.ps1')
我怎么能在 OSX 中做同样的事情?
我对 mac 上的 powershell 一无所知,如果它存在的话,我只能在 Mac OS 上打开一个类似终端的 gui 应用程序 X你可以使用打开命令:
open -a /Applications/Utilities/Terminal.app ""
将是一个新的空白 window
open -a /Applications/Utilities/Terminal.app somescrip.sh
将是 运行 一个脚本
或者你可以制作一个 apple 脚本 运行 那
将以下内容保存在文件中 (~/OpenNewTerminal.scp):
tell application "Terminal"
do script " "
activate
end tell
然后你可以 运行 用 osascript
osascript ~/OpenNewTerminal.scp
当然,更 bash 惯用的方式是 运行 在子 shell 或后台
subshell:
output=$(ls)
echo $output
背景:
./command &
具有重定向输出的背景,因此它不会渗透到您当前的 shell:
./command 2>&1 > /dev/null
在 macOSwindow 上的新终端 window 中启动 PowerShell 实例:
无法向其传递参数:
PS> open -a Terminal $PSHOME/powershell
如果你想运行一个给定的命令:
不幸的是,如果您想在新的 PowerShell 实例中将命令传递给 运行,还需要做更多的工作:
本质上,您需要将您的命令放在一个临时的、自删除的、可执行的 shell 脚本中,该脚本通过 shebang 行调用:
注意:确保 运行 至少 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 }
在 OSX 中,我打开 bash 终端并进入 PowerShell 控制台。 在我的 PowerShell 脚本中,我想打开另一个 PowerShell 控制台并在那里执行 PowerShell 脚本。
在 Windows 下,我会
Invoke-Expression ('cmd /c start powershell -Command test.ps1')
我怎么能在 OSX 中做同样的事情?
我对 mac 上的 powershell 一无所知,如果它存在的话,我只能在 Mac OS 上打开一个类似终端的 gui 应用程序 X你可以使用打开命令:
open -a /Applications/Utilities/Terminal.app ""
将是一个新的空白 window
open -a /Applications/Utilities/Terminal.app somescrip.sh
将是 运行 一个脚本
或者你可以制作一个 apple 脚本 运行 那
将以下内容保存在文件中 (~/OpenNewTerminal.scp):
tell application "Terminal"
do script " "
activate
end tell
然后你可以 运行 用 osascript
osascript ~/OpenNewTerminal.scp
当然,更 bash 惯用的方式是 运行 在子 shell 或后台
subshell:
output=$(ls)
echo $output
背景:
./command &
具有重定向输出的背景,因此它不会渗透到您当前的 shell:
./command 2>&1 > /dev/null
在 macOSwindow 上的新终端 window 中启动 PowerShell 实例:
无法向其传递参数:
PS> open -a Terminal $PSHOME/powershell
如果你想运行一个给定的命令:
不幸的是,如果您想在新的 PowerShell 实例中将命令传递给 运行,还需要做更多的工作:
本质上,您需要将您的命令放在一个临时的、自删除的、可执行的 shell 脚本中,该脚本通过 shebang 行调用:
注意:确保 运行 至少 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 }