根据您对如何从交互式PowerShell会话打开Jupyter Notebook的描述(第2行和第4行),您需要将快捷方式文件调用powershell.exe
,Windows PowerShell CLI,并使用上述语句.
以下是以编程方式创建快捷方式文件的PowerShell代码片段:
# Path to the shortcut file; adjust as needed.
$shortcutFile = "$env:USERPROFILE\Desktop\Jupyter Notebook.lnk"
# Create the shortcut file...
$shortcut = (New-Object -ComObject WScript.Shell).CreateShortcut($shortcutFile)
# ... and set its properties.
# Specify the target executable and its arguments.
$shortcut.TargetPath = 'powershell.exe'
$shortcut.Arguments =
'-NoExit -Command .\venv-name\Scripts\activate; jupyter notebook'
# Specify the working directory.
$shortcut.WorkingDirectory = '%USERPROFILE%'
# Set the window style, if needed.
$shortcut.WindowStyle = 7 # Open the PowerShell console window minimized.
$shortcut.Save()
# Test invocation (simulate opening the shortcut interactively)
Invoke-Item $shortcutFile
注:
-NoExit
使PowerShell会话保持打开状态,这样您就可以判断启动时发生了什么;一旦您验证启动笔记本电脑按预期工作,就可以将其删除.
100启动PowerShell会话minimized(因为您可能对查看控制台窗口不感兴趣.因此,结合-NoExit
,如果你想判断在启动时发生了什么,你需要通过任务栏激活最小化的窗口.
赋值为100,即指定目标可执行文件,将导致赋值为instantly resolved to a full path:
默认情况下,已启动进程的工作目录设置为:
当快捷方式文件打开时为directory in which the shortcut file is located interactively(通常是通过任务栏、桌面或文件资源管理器)
相反,在programmatic调用中,它依赖于caller's working directory,有一个微妙的区别.
- 对于
Invoke-Item
,它是调用者process的工作目录,通常与PowerShell's的工作目录不同.[1]
- 对于
Start-Process
,它是呼叫PowerShell session的工作目录(位置).
因此,最好是explicitly通过分配给100属性来分配一个工作目录.
如上面的.WorkingDirectory
赋值中所示,you may use 101-style environment-variable references(如%USERPROFILE%
)和这样的引用在.TargetPath
、.Arguments
和.IconLocation
属性中同样受支持.
一百:
尽管名为.CreateShortcut()
,但.CreateShortcut()
方法也可以打开existing个快捷方式文件,例如判断它们的属性.
如果您(可能无意中)在try 完全(重新)创建一个existing快捷方式文件时只填写了它的some个属性,则任何preexisting个属性值都是preserved.
因此,如果意图是创建快捷方式文件from scratch,则首先删除任何先前存在的版本.
文档链接:
[1]PowerShell支持每个进程多个运行空间(线程),每个运行空间(线程)可以有一个单独的工作目录.这与.NET和其他进程内API使用的single, process-wide工作目录形成对比.因此,给定的PowerShell会话(运行空间)的工作目录不能与进程范围的工作目录保持同步,两者通常为differ.结果是,您应该始终传递full, file-system-native条指向.NET和其他进程内API的路径.有关详细信息,请参阅this answer.