pyenv-virtualenv for Windows 1.2
A 'pyenv' plugin to manage Python virtual environments, depending on different Python versions, for various Python projects.
Loading...
Searching...
No Matches
create_junction.ps1
Go to the documentation of this file.
1# Windows PowerShell Script to create a
2# virtual environment directory junction.
3#
4# Dependencies:
5# (None)
6#
7# © 2025 Michael Paul Korthals. All rights reserved.
8# For legal details see documentation.
9#
10# 2025-07-31
11#
12# This script is located in the 'pyenv-virtualenv' plugin root directory.
13#
14# It has 2 command line arguments:
15# 1. Absolute/relative path to the junction.
16# 2. Absolute/relative path to its target.
17#
18# It will be called by 'pyenv-virtualenv' its parent folder.
19#
20# The script returns RC = 0 or another value in case of error.
21
22$rc = 0
23# Check if this script is running as 'Administrator'
24$currentPrincipal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
25$as_admin = $currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
26
27# Get/Check argument 1 (path)
28try {
29 $link_path = $args[0].Trim()
30} catch {
31 $rc = 1
32 Write-Host "$([char]27)[91mERROR Missing argument 1 (path) (RC = $rc).$([char]27)[0m"
33 Write-Host "$([char]27)[37mINFO To define a directory junction, assign 2 arguments 'path' and 'target' as absolute paths.$([char]27)[0m"
34 exit $rc
35}
36
37# Get/Check argument 2 (target)
38try {
39 $link_target = $args[1].Trim()
40} catch {
41 $rc = 1
42 Write-Host "$([char]27)[91mERROR Missing argument 2 (target) (RC = $rc).$([char]27)[0m"
43 Write-Host "$([char]27)[37mINFO To define a directory junction, assign 2 arguments 'path' and 'target' as absolute paths.$([char]27)[0m"
44 exit $rc
45}
46
47# Make symbolic link to patched 'pyenv.bat'
48if ($as_admin) {
49 Write-Host "$([char]27)[37mINFO Writing directory junction: $([char]27)[0m"
50 Write-Host "$([char]27)[37mINFO * Path: '$link_path'.$([char]27)[0m"
51 Write-Host "$([char]27)[37mINFO * Target: '$link_target'.$([char]27)[0m"
52 Write-Host "$([char]27)[37mINFO This could take some seconds ...$([char]27)[0m"
53 try {
54 New-Item -ItemType Junction -Path "$link_path" -Target "$link_target" | Out-Null
55 } catch {
56 $rc = 1
57 Write-Host "$([char]27)[91mERROR Unexpectedly cannot write junction (RC = $rc).$([char]27)[0m"
58 Write-Host "$([char]27)[37mINFO Analyze/configure/repair the situation, why a script running as 'Administrator' fails. Then try again.$([char]27)[0m"
59 exit $rc
60 }
61 Write-Host "$([char]27)[37mINFO Done.$([char]27)[0m"
62} else {
63 $rc = 13
64 Write-Host "$([char]27)[91mERROR Insufficient privileges. (RC = $rc).$([char]27)[0m"
65 Write-Host "$([char]27)[37mINFO To create a junction, you must call this script in a console terminal with 'Administrator' privileges. Then try again.$([char]27)[0m"
66 exit $rc
67}
68
69# Return error level
70exit $rc
71
72
73# --- END OF CODE ------------------------------------------------------
74