powershellにはzshのvaredの類似物がありますか?


1

PowerShellに行ってパスを追加したとします:

$env:path += 'C:\banal\cranial\inversion'

おっと、あなたは忘れてしまいました。その中で、それはあったはずです

$env:path += ';C:\banal\cranial\inversion'

-しかし、あなたの道はすでに台無しにされています。zshでは、次のことができvared PATHます。PowerShellについては、変数を新たにリセットする代わりに編集する方法はありますか?リセットする場合、ラップアラウンドした場合、その一部のみをコピーして貼り付けるにはどうすればよいですか?


zshビルトインではvaredなくが呼び出されvareditます。
デニスウィリアムソン

もちろん; コンテキストスイッチング...修正
アレクシークラブロフ

回答:


1

そうではありませんが、次のようなことができます:

    function Edit-Variable {
#.Parameter name
#    The name (or path) to the variable to edit.
#.Parameter Environment
#    Optional switch to force evaluating the name as an environment variable. You don't need this if you specify the path as env:Path instead of just "Path"
#.Example
#     Edit-Variable -env path
#.Example
#     Edit-Variable profile
#.Example
#     Edit-Variable env:\path
#.Example
#     Edit-Variable variable:profile

param(
    [Parameter(Position=0,ValueFromPipelineByPropertyName=$true,ValueFromPipeline=$true)]
    [string]$name
,
    [switch]$Environment
)
process {
    $path = Resolve-Path $name -ErrorAction SilentlyContinue
    if($Environment) {
        ## Force Env: if they said -Env
        if(!$path -or $Path.Provider.Name -ne "Environment") {
            $path = Resolve-Path "Env:$name"
        }
    } else {
        if($Path -and $Path.Provider.Name -eq "Environment") {
            $Environment = $true
        } elseif(!$path -or $Path.Provider.Name -ne "Variable") {
            $path = Resolve-Path "Variable:$name" -ErrorAction SilentlyContinue
        }
    }

    $temp = [IO.Path]::GetTempFileName()
    if($path) {
        if(!$Environment) {
            $value = (Get-Variable $path.ProviderPath).Value
            $string = $value -is [String]
            if(!$string) {
                Write-Warning "Variable $name is not a string variable, editing as CliXml"
                Export-Clixml $temp -InputObject $Value 
            } else {
                Set-Content $temp $Value
            }
        } else {
            Get-Content $path | Set-Content $temp
        }
    } else {
        $Environment = $false
        New-Variable $Name
        $path = Resolve-Path Variable:$name -ErrorAction SilentlyContinue
    }
    if(!$path) {
        Write-Error "Cannot find variable '$name' because it does not exist."
    } else {
        # Let the user edit it in notepad, and see if they save it
        $pre = Get-ChildItem $temp
        (Start-Process notepad $temp -passthru).WaitForExit()
        $post = Get-ChildItem $temp
        if($post.LastWriteTime -gt $pre.LastWriteTime) {
            if(!$Environment) {
                if(!$string) {
                    Import-CliXml $temp | Set-Variable $path.ProviderPath
                } else {
                    Get-Content $temp | Set-Variable $path.ProviderPath
                }
            } else {
                Get-Content $temp | Set-Content $path
            }
        }
    }
    Remove-Item $temp -ErrorAction SilentlyContinue
}
}

Set-Alias vared Edit-Variable

私はそれがzshの仕組みではないことを知っていますが、メモ帳は便利でした...


とても興味深い!悲しいことに、これをPS 2.0に貼り付けてから言うと、vared env:pathは文字列変数ではないという警告を表示し、CliXmlとして編集します-そして、メモ帳には<Objs ...>でラップされた<Nil />が表示されます。
アレクシークラブロフ

試してみるvared -env pathvared path -env...私はenvスイッチを作成しました。環境変数と通常の変数(およびコンテンツを含む他のもの)で動作します。ドライブを要求vared profilevared env:pathて検出し、それがenvであることを確認することを考えましたが、最終的にはスイッチを使用しました。例を入れるべきだったので、修正します。
ジェイクル

OK、私は完全に今あなたが好きどちらかを行うことができ、エラー処理をやり直し:Edit-Variable env:pathEdit-Variable -env path
Jaykul

0

文字列メソッドを使用して修正を行うことができます

$indexError = $env:Path.LastIndexOf("C:")
$env:Path = $env:Path.remove($indexError, 1)
$env:Path = $env.Insert($indexError, ";C")

PowerShellでは、すべてがオブジェクトであり、プログラミング戦略を使用して必要なものを実現できることを忘れないでください。


これも非常に便利です!できるとすぐにそれをアップします
アレクシークラブロフ

ええ、簡単なスクリプトでこれを処理できることは完全に真実です。個人的に、私はのために行くだろう$Env:Path = $Env:Path -replace '(?<!;)C:',';C:' # replace C: that doesn't follow a ; with ';C:'
Jaykul

@jaykulあなたがしたことはとても素晴らしいです、私はもっと学ぶつもりはありません、さらに検索する主題名は何ですか?
mjsr

これは正規表現です。具体的には、ネガティブな後読みゼロ幅アサーションです。regular-expressions.info/lookaround.html
Jaykul
弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.