1. $path = "C:\apps\develop\sdks\python"
    2. $content = "
    3. Usage:
    4. pvm help show the usage of pvm
    5. pvm using show the python version in use
    6. pvm list show all version
    7. pvm use <version> switch version
    8. "
    9. function ListVersion {
    10. # 将前缀是v的文件过滤出来
    11. Get-Item ($path + "\v*") | ForEach-Object { $_.Name }
    12. }
    13. function UseVersion ($version ) {
    14. $arguments = "New-Item -ItemType SymbolicLink -Path '$path\current' -Target '$path\$version' -Force"
    15. $user = [Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()
    16. if (-NOT ($user).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {
    17. Start-Process powershell -Verb runAs -ArgumentList $arguments
    18. return
    19. }
    20. # Invoke-Expression 将字符串解析成命令执行
    21. Invoke-Expression $arguments
    22. }
    23. switch ($args[0]) {
    24. "help" { $content }
    25. "using" {
    26. if (-not (Test-Path "$path\current")) {
    27. Write-Host "Please execute the use command first"
    28. exit
    29. }
    30. python --version
    31. }
    32. "list" { ListVersion }
    33. "use" {
    34. if ($null -eq $args[1]) {
    35. $content
    36. exit
    37. }
    38. $version = $args[1].ToString().ToLower();
    39. if (-not $version.StartsWith("v")) {
    40. $version = "v$version"
    41. }
    42. if ((ListVersion) -contains $version) {
    43. UseVersion $version
    44. }
    45. else {
    46. Write-Host "No matching version"
    47. }
    48. }
    49. Default { $content }
    50. }