【发布时间】: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。
【问题讨论】:
-
看来
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