如何在 Chocolatey 安装后刷新 PowerShell 会话的环境而无需打开新会话 [英] How to refresh the environment of a PowerShell session after a Chocolatey install without needing to open a new session

查看:45
本文介绍了如何在 Chocolatey 安装后刷新 PowerShell 会话的环境而无需打开新会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写用于将 GitHub 源代码克隆到本地计算机的自动化脚本.
在我的脚本中安装 Git 后我失败了,它要求关闭/打开 powershell.
所以我安装Git后无法自动克隆代码.

I am writing automated script for cloning GitHub source code to local machine.
I failed after installing Git in my script, it asked for close/open powershell.
So I am not able to clone code automatic after installing Git.

这是我的代码:

iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
 choco install -y git
 refreshenv
 Start-Sleep -Seconds 15

 git clone --mirror https://${username}:${password}@$hostname/${username}/$Projectname.git D:GitTemp -q 2>&1 | %{ "$_" } 

错误:

git : The term 'git' is not recognized as the name of a cmdlet, 
      function, script file, or operable program. 
      Check the spelling of the name, or if a path was included, 
      verify that the path is correct and try again.

请让我在不退出的情况下重新启动 PowerShell 应该放什么?

Please let me what should I put for reboot PowerShell without exiting?

推荐答案

你有一个引导问题:

  • refreshenv(Update-SessionEnvironment)通常是正确的命令,用于在 choco install 之后使用环境变量更改更新当前会话.. 命令.

  • refreshenv (an alias for Update-SessionEnvironment) is generally the right command to use to update the current session with environment-variable changes after a choco install ... command.

但是,在安装 Chocolatey 本身之后立即refreshenv/Update-SessionEnvironment 本身仅在未来 PowerShell 会话,因为加载这些命令是通过添加到配置文件 $PROFILE 的代码发生的,基于环境变量 $env:ChocolateyInstall.

However, immediately after installing Chocolatey itself, refreshenv / Update-SessionEnvironment themselves are only available in future PowerShell sessions, because loading these commands happens via code added to profile $PROFILE, based on environment variable $env:ChocolateyInstall.

也就是说,您应该能够模拟在以后的会话中获取 $PROFILE 时 Chocolatey 所做的事情,以便能够使用 refreshenv/Update-SessionEnvironment 立即安装 Chocolatey:

That said, you should be able to emulate what Chocolatey does when $PROFILE is sourced in future sessions in order to be able to use refreshenv / Update-SessionEnvironment right away, immediately after installing Chocolatey:

iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))

choco install -y git

# Make `refreshenv` available right away, by defining the $env:ChocolateyInstall
# variable and importing the Chocolatey profile module.
# Note: Using `. $PROFILE` instead *may* work, but isn't guaranteed to.
$env:ChocolateyInstall = Convert-Path "$((Get-Command choco).Path)...."   
Import-Module "$env:ChocolateyInstallhelperschocolateyProfile.psm1"

# refreshenv is now an alias for Update-SessionEnvironment
# (rather than invoking refreshenv.cmd, the *batch file* for use with cmd.exe)
# This should make git.exe accessible via the refreshed $env:PATH, so that it
# can be called by name only.
refreshenv

# Verify that git can be called.
git --version

注意:原来的解决方案使用了.$PROFILE 而不是 Import-Module ... 来加载 Chocolatey 配置文件,依赖于 Chocolatey 在那时已经更新了 $PROFILE.但是,ferventcoder 指出,$PROFILE 的这种更新并不总是 发生了,所以不能依赖.

Note: The original solution used . $PROFILE instead of Import-Module ... to load the Chocolatey profile, relying on Chocolatey to have updated $PROFILE already at that point. However, ferventcoder points out that this updating of $PROFILE doesn't always happen, so it cannot be relied upon.

这篇关于如何在 Chocolatey 安装后刷新 PowerShell 会话的环境而无需打开新会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆