在OSX中,我打開一個bash終端並輸入一個PowerShell控制檯。 在我的PowerShell腳本中,我想打開另一個PowerShell控制檯並在那裏執行PowerShell腳本。如何從PowerShell腳本打開另一個PowerShell控制檯
在Windows下,我會做
Invoke-Expression ('cmd /c start powershell -Command test.ps1')
我怎麼能這樣做在OSX的samething?
在OSX中,我打開一個bash終端並輸入一個PowerShell控制檯。 在我的PowerShell腳本中,我想打開另一個PowerShell控制檯並在那裏執行PowerShell腳本。如何從PowerShell腳本打開另一個PowerShell控制檯
在Windows下,我會做
Invoke-Expression ('cmd /c start powershell -Command test.ps1')
我怎麼能這樣做在OSX的samething?
要在MacOS在一個新的終端窗口啓動PowerShell的實例:
無能夠傳遞參數:
PS> open -a Terminal $PSHOME/powershell
如果你想運行一個給定的命令:
不幸的是,如果你想通過一個命令在新的PowerShell實例運行需要相當多的工作:
從本質上講,你需要將您的命令在一個臨時的,自我刪除,可執行的shell腳本,通過認領行調用:
注:一定要運行至少PowerShell核心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 }
很高興聽到它,Jim(感謝您確認,@LotPings)是的,通過shebang行的調用在之前的beta版中被打破;我添加了一個說明最低版本要求的註釋。一旦有新版本發佈,就值得更新。 – mklement0
我不知道在Mac PowerShell的東西,如果存在的話,但打開像Mac OS X上的終端就可以使用命令打開一個GUI應用程序:
open -a /Applications/Utilities/Terminal.app ""
將是一個新的空白窗口 open -a /Applications/Utilities/Terminal.app somescrip.sh
將運行一個腳本
,或者你可以讓一個蘋果腳本並運行
保存以下的文件(〜/ 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腳本,我是不知道如何實現2nd。 – Jim
'開始powershell'? –
沒有幫助:( – Jim
您是否在OS/X上使用PowerShell 6從https://github.com/PowerShell/PowerShell – lit