从脚本安装 poshgit 后如何重新加载 PowerShell?

How to reload PowerShell after installing poshgit from script?

我很难找到一种方法让 git 和 poshgit 在使用 chocolatey 通过脚本安装后不关闭 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 东西,所以我真的很想找到一个解决方案,让它在不关闭控制台的情况下工作。

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

. $profile

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

我的配置文件目前只有一行

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

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

我是 Powershell 的新手,所以请多多包涵...:)

感谢上面的评论,我能够在不重新启动 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