1

我很难找到一种方法来让 git 和 poshgit 在使用巧克力通过脚本安装它而不关闭 powershell 控制台后工作。

这是我的脚本目前的样子。

Function InstallChocolatey {
    iwr https://chocolatey.org/install.ps1 -UseBasicParsing | iex
    refreshenv  #needed to make poshgit install succesfully without restarting Powershell
    Write-Output "Chocolatey installed and configured."
}

Function InstallSoftware {
    choco install git --params='/NoShellIntegration' -y
    choco install poshgit -y
    . $profile   #reload profile
    Write-Output "Software installed and profile reloaded"
}

InstallChocolatey
InstallSoftware

当我关闭 Powershell 并重新启动它时,一切都按预期工作。但是由于我的脚本以后应该继续执行 git 的东西,我真的很想找到一个解决方案,让它在不关闭控制台的情况下工作。

根据我在 Stackoverflow 和其他网站上使用的发现

. $profile

应该重新加载配置文件。但不幸的是,我的情况似乎没有任何效果。我尝试再次使用 refreshenv。

我的个人资料文件目前只包含一行

 Import-Module 'C:\tools\poshgit\dahlbyk-posh-git-a4faccd\src\posh-git.psd1'

我还尝试在行尾添加 -force 但没有任何改变。

我对 Powershell 很陌生,所以请多多包涵…… :)

4

1 回答 1

0

感谢上面的评论,我能够在不重新启动 PowerShell 的情况下让它工作(见下面的脚本)!

使其发挥作用所需的关键要素是:

安装 Chocolatey 和 Git/Posh Git 后刷新环境变量

refreshenv 

为了使 Git 在不重启 PS 的情况下工作,暂时将 Git 添加到路径

$env:path+='C:\Program Files\Git\cmd' 

最后但并非最不重要的一点是重新加载 Posh Git 配置文件(如 @4c74356b41 建议的那样)

. C:\tools\poshgit\dahlbyk-posh-git-a4faccd\profile.example.ps1

完整的脚本(欢迎改进!):

    Function InstallChocolatey {
    iwr https://chocolatey.org/install.ps1 -UseBasicParsing | iex
    #choco feature enable -n autoUninstaller
    refreshenv
    Write-Output "Chocolatey installed."
}


Function InstallSoftware {
    choco install git --params='/NoShellIntegration' -y
    $env:path+='C:\Program Files\Git\cmd'
    Write-Output "Git installed"
    choco install poshgit -y
    refreshenv
    Write-Output "Posh Git installed"
}

Function WriteSSHKeys { #Writing SSH keys to the user directory
}

Function SetupSSH {
    . C:\tools\poshgit\dahlbyk-posh-git-a4faccd\profile.example.ps1
}

InstallChocolatey
InstallSoftware
WriteSSHKeys
SetupSSH
于 2017-04-20T14:26:06.053 回答