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

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

问题描述

我正在编写用于将GitHub源代码克隆到本地计算机的自动化脚本.
在脚本中安装Git后,我失败了,它要求关闭/打开Powershell.
因此,安装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应该怎么做?

解决方案

您遇到引导问题:

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

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

也就是说,您应该能够模仿在以后的会话中获取$PROFILE时Chocolatey所做的操作,以便能够立即使用refreshenv/Update-SessionEnvironment安装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:ChocolateyInstall\helpers\chocolateyProfile.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的这种更新不会总是 发生,因此不能依靠.

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.

Here is my code:

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 | %{ "$_" } 

Error:

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.

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

解决方案

You have a bootstrapping problem:

  • 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.

  • 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.

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:ChocolateyInstall\helpers\chocolateyProfile.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

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 cannot be relied upon.

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

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