【问题标题】:Not able to exit from the powershell console after script execution脚本执行后无法从 powershell 控制台退出
【发布时间】:2021-09-09 15:36:52
【问题描述】:

我正在尝试为我的班级创建一个脚本,以自动创建文件夹 monthDay 并在 Visual Studio Code 中打开它。但是,在脚本执行后,powershell 控制台保持打开状态,直到我手动关闭它或关闭 VSCode。我只希望控制台在执行后消失。这是脚本:请告诉我该怎么做..

# Hash table for months 
$hash_month = @{1="jan";2="feb";3="mar";4="apr";5="may";6="june";7="july";8="aug";9="sep";10="oct";11="nov";12="dec"}

#extracting date
$date = (Get-Date).Day

#extracing month
$month = (Get-Date).Month

# getting month name from the hash table
$month_name= $hash_month[$month]

# Creating the Directory with monthDate 
# This command will create a dir if it does not exist, or it will simply not execute if the directory exists
[System.IO.Directory]::CreateDirectory("$month_name$date")

# changing dir
cd $month_name$date

# opening the dir in vscode
code .

# currently not able to exit from console after execution of script
exit 0

我已经附上了窗口的屏幕截图,我只是想让控制台消失,但 vscode。

Powershell and VSCode Screenshot

【问题讨论】:

  • 看来code 出乎意料地为你阻塞。您可以尝试使用Start-Process code . 解决这个问题,但更大的问题是为什么code(在Windows 上应该解析为code.cmd - 尝试`Get-Command 代码)在您的情况下会以这种方式运行。
  • 我尝试过 Start-Process code . 它只是打开了 cmd 并一直保留在那里,直到我关闭 vscode 或我手动关闭 cmd。
  • 这是意料之中的:它与您的 code 命令的意外阻塞行为一致。您可以通过在 Start-Process 调用中添加 -WindowStyle Hidden 来隐藏不需要的控制台窗口,但是,更有趣的问题是,为什么 code 在您的计算机上会以这种方式运行,而 Get-Command code 应该有助于调查这一点。跨度>
  • Get-Command code 正在返回此,CommandType Name Version Source ----------- ---- -------- ------ 应用程序代码。 cmd 0.0.0.0 C:\Users\Ka…
  • 到目前为止这是正常的 (code.cmd)。解决这个谜团可能需要在这里的 cmets 中进行调查,但Start-Process -WindowStyle code . 应该是一个可行的解决方法。根据我的经验(Windows 10 20H2,Visual Studio Code 1.57.1​​),code.cmd 从不阻塞。

标签: windows powershell visual-studio-code directory


【解决方案1】:

我在这里做了 2 处更改:

  1. 路径创建的处理方式不同,这与问题无关,但您的方式不是为我创建路径。
  2. 使用带有隐藏窗口的 Start-Process 来实例化 VSCode 并使$pid kill 方法起作用。

完整脚本:

# Hash table for months 
$hash_month = @{1 = "jan"; 2 = "feb"; 3 = "mar"; 4 = "apr"; 5 = "may"; 6 = "june"; 7 = "july"; 8 = "aug"; 9 = "sep"; 10 = "oct"; 11 = "nov"; 12 = "dec" }

#extracting date
$date = (Get-Date).Day

#extracing month
$month = (Get-Date).Month

# getting month name from the hash table
$month_name = $hash_month[$month]

# Creating the Directory with monthDate 
# This command will create a dir if it does not exist, or it will simply not execute if the directory exists
$path = $month_name + $date
if (!(Test-Path $path)) {
    New-Item $path -ItemType Directory
}

# # changing dir
cd $path

# # opening the dir in vscode with Start-Process
Start-Process -FilePath "code" -ArgumentList "." -WindowStyle Hidden

# # currently not able to exit from console after execution of script
Stop-Process $pid

【讨论】:

  • 这个我也试过了,还是不行。
  • 我能够复制您所看到的内容,编辑了答案。
  • 您先生,是我的英雄。太感谢了。一直在苦苦挣扎。
  • 最好了解在什么情况下 VS Code CLI code.cmd 在调用时会意外阻塞(它不应该)。鉴于Start-Process 解决方法,我希望Stop-Process 是不必要的(原来的exit 应该可以工作) - 或者你是说它仍然需要脚本终止?
猜你喜欢
  • 2017-10-22
  • 1970-01-01
  • 2015-10-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-23
  • 1970-01-01
相关资源
最近更新 更多