如何提取有条件的环境路径?

时间:2018-02-06 02:44:59

标签: powershell

简而言之:如何获取与数组中任何字符串项不匹配的连接行?

这似乎微不足道,但我无法找到解决方案。我尝试使用PowerShell功能管理VSCode Python环境。我使用PowerShell脚本轻松更改vscode终端路径设置。我想获得不是Python路径的环境路径。

$noPythonPath = ($env:path.Split(';') | Where-Object {
    $_ -notmatch 'python' -and
    $_ -notmatch 'anaconda' -and
    $_ -notmatch 'django' -and
    $_ -notmatch 'pspenv'
}) -join ';'

我想知道是否有办法在不手动列出所有路径键的情况下执行此操作。但是下面的代码不起作用。似乎-match不适用于字符串数组。

$key = $PC2paths.Keys
$noPythonPath = ($env:path.Split(';') | Where-Object {
    $_ -notmatch $key.TrimEnd('1234567890')
}) -join ';'

以下是包含代码。

$PC2paths = @{
    'Anaconda3' = "C:\Program Files\Anaconda3\python.exe;C:\Program Files\Anaconda3\Scripts;";
    'DjangoVenv2' = "C:\Users\user\source\repos\CsStudy\.virtualenvs\Djangovenv2\Scripts\python.exe;" ;
    "pypy2" = "C:\pypy2-v5.9.0-win32\pypy2-v5.9.0-win32\pypy.exe;C:\pypy2-v5.9.0-win32\pypy2-v5.9.0-win32\bin;";
    'python2' = "c:\python27\python.exe;C:\Python27\Scripts;";
    'python3' = "C:\Program Files\Python36-32\python.exe;C:\Program Files\Python36-32\Scripts;";
    'ropepython2' = "C:\Users\user\source\repos\rope\ropeTest1\ropeTest1\rope1\.vscode\ropePython\Scripts\python.exe;";
    'pspenv1' = "C:\Users\user\source\repos\psptools\.virtualenvs\pspenv1\Scripts\python.exe;";
    'RefreshEnvironment' = 'C:\Users\user\source\repos\CsStudy\.vscode\refreshenv.cmd';
}

function workon($environment) {
    & cmd /k workon.bat $environment
}

function useEnv([string]$envName) {
    $pathKey = $envName

    $SettingsFilePath = (UpSearch . "settings.json")
    if ($SettingsFilePath -eq "file not found") {
        Write-Output 'not found'
        return
    }

    $key = $PC2paths.Keys
    $noPythonPath = ($env:path.Split(';') | Where-Object {
        $_ -notmatch $key.TrimEnd('1234567890')
    }) -join ';'

    $newpaths = (($PC2paths.$pathKey) + $noPythonPath)
    $newpaths = $newpaths.split(';')
    $exepath = $newpaths[0]
    $newpaths[0] = Split-Path $newpaths[0]
    $ofs = ';'
    $newpath = "$newpaths"
    $env:path = $newpath
    $a = Get-Content $SettingsFilePath -Raw | ConvertFrom-Json
    $a."python.pythonPath" = $exepath
    $a."terminal.integrated.env.windows" | Add-Member -Name "path" -Value $newpath -MemberType NoteProperty -Force
    $a | ConvertTo-Json -Depth 30  | Set-Content $SettingsFilePath
    # $refresh = UpSearch . "RefreshEnv.cmd"
    $refresh = $PC2paths."RefreshEnvironment"
    cmd /c $refresh
}

function useAnaconda3 {
    useEnv -envName 'Anaconda3'
}

function useDjangoVenv2 {    
    useEnv -envName 'DjangoVenv2'
}

function usePypy2 {    
    useEnv -envName 'pypy2'
}

function usePython2 {    
    useEnv -envName 'python2'
}

function usePython3 {    
    useEnv -envName 'python3'
}

function useropepython2 {    
    useEnv -envName 'ropepython2'
}

function usepspenv1 {    
    useEnv -envName 'pspenv1'
}

function UpSearch($pathtosearch, $filename) {
    if ($pathtosearch -eq "") {
        "file not founded"
        return
    }
    $pathtosearch = (Get-Item -Path $pathtosearch -Verbose).FullName
    if (Test-Path "$pathtosearch\.vscode\$filename" ) {
        "$pathtosearch\.vscode\$filename"
    } else {
        UpSearch (Split-Path $pathtosearch) $filename
    }
}

1 个答案:

答案 0 :(得分:2)

正确,-match(以及-notmatch)不能将数组用作第二个操作数。但是,您可以将数组加入alternation正则表达式:

$a = 'foo', 'bar', 'baz'
$re = ($a | ForEach-Object { [regex]::Escape($_) }) -join '|'

'something', 'bar', 'else' -notmatch $re