简体   繁体   English

脚本执行后无法从 powershell 控制台退出

[英]Not able to exit from the powershell console after script execution

I am trying to create a script for my class to automatically create a folder monthDay wise and open it in Visual Studio Code.我正在尝试为我的班级创建一个脚本,以自动创建一个文件夹 monthDay wise 并在 Visual Studio Code 中打开它。 However, after the script executes the powershell console remains open until I close it manually or I close VSCode.但是,在脚本执行后,powershell 控制台保持打开状态,直到我手动关闭它或关闭 VSCode。 I just want the console to go away after it executes.我只希望控制台在执行后消失。 Here is the script: Please tell me how to do it..这是脚本:请告诉我怎么做..

# 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

I have attached the screenshot of the window, I just want the console to go away but NOT vscode.我附上了窗口的屏幕截图,我只希望控制台消失而不是vscode。

Powershell and VSCode Screenshot Powershell 和 VSCode 截图

I made 2 changes here:我在这里做了 2 处更改:

  1. The path creation is handled differently, which has nothing to do with the question, but your way was not creating the path for me.路径创建的处理方式不同,这与问题无关,但是您的方式并没有为我创建路径。
  2. Used Start-Process with hidden window to instantiate VSCode and make the $pid kill method work.使用带有隐藏窗口的 Start-Process 来实例化 VSCode 并使$pid kill 方法起作用。

Full script:完整脚本:

# 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM