上次写了个脚本, 只备份path, 现在不够用了.
我使用scoop安装软件, 装的软件本身是绿色的, 它只会添加一些用户环境变量. 我想把环境变量备份下.
[cmdletbinding()] param ( [Parameter(Mandatory = $true, ValueFromPipeline = $true)] [ValidateSet('backup', 'restore')] [String]$operation ) switch ($operation) { 'backup' { $allEnvVarsName = (Get-ChildItem Env:).name $userEnvVars = @{ } foreach ($varName in $allEnvVarsName) { $varValue = [System.Environment]::GetEnvironmentVariable("$varName", "User") if ($varValue -ne $null) { $userEnvVars.Add($varName, $varValue) } } $userEnvVars | ConvertTo-Json | Out-File "$PSScriptRoot\userEnvVars.json" -Encoding UTF8 } 'restore' { if(!(Test-Path "$PSScriptRoot\userEnvVars.json")){ write-output "Please backup first." } $userEnvVarsCopy = Get-Content "$PSScriptRoot\userEnvVars.json" -Encoding UTF8 | ConvertFrom-Json $userEnvVars = @{} $userEnvVarsCopy.psobject.properties | ForEach-Object { $userEnvVars[$_.Name] = $_.Value } foreach ($var in $userEnvVars.GetEnumerator()) { [System.Environment]::SetEnvironmentVariable($var.name, $var.value, "User") } } }
本文链接: https://www.pstips.net/%e5%a4%87%e4%bb%bd%e5%92%8c%e8%bf%98%e5%8e%9f%e7%94%a8%e6%88%b7%e7%8e%af%e5%a2%83%e5%8f%98%e9%87%8f.html
请尊重原作者和编辑的辛勤劳动,欢迎转载,并注明出处!
请尊重原作者和编辑的辛勤劳动,欢迎转载,并注明出处!