gongxiangp2/install-git-pwsh.ps1

150 lines
5.0 KiB
PowerShell
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 安装Git和PowerShell Core脚本
# 使用winget自动安装最新版本
Write-Host "========================================" -ForegroundColor Cyan
Write-Host "Git 和 PowerShell Core 安装脚本" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
Write-Host ""
# 设置输出编码为 UTF-8
try {
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
$OutputEncoding = [System.Text.Encoding]::UTF8
chcp 65001 | Out-Null
} catch {
# 忽略编码设置错误
}
function Write-Success {
param([string]$Message)
Write-Host "✅ $Message" -ForegroundColor Green
}
function Write-Error-Custom {
param([string]$Message)
Write-Host "❌ $Message" -ForegroundColor Red
}
function Write-Info {
param([string]$Message)
Write-Host "ℹ️ $Message" -ForegroundColor Cyan
}
function Write-Warning-Custom {
param([string]$Message)
Write-Host "⚠️ $Message" -ForegroundColor Yellow
}
# 检查winget是否可用
Write-Info "检查winget是否可用..."
try {
$wingetVersion = winget --version 2>&1
if ($LASTEXITCODE -eq 0) {
Write-Success "winget已安装: $wingetVersion"
} else {
Write-Error-Custom "winget不可用,请先安装Windows App Installer"
Write-Info "下载地址: https://aka.ms/getwinget"
exit 1
}
} catch {
Write-Error-Custom "winget不可用,请先安装Windows App Installer"
Write-Info "下载地址: https://aka.ms/getwinget"
exit 1
}
Write-Host ""
# 步骤1:安装Git
Write-Host "📦 步骤 1:安装Git" -ForegroundColor Magenta
Write-Info "正在使用winget安装Git for Windows..."
try {
# 使用winget安装Git(--silent静默安装,--accept-package-agreements自动接受协议)
winget install --id Git.Git -e --silent --accept-package-agreements --accept-source-agreements
if ($LASTEXITCODE -eq 0) {
Write-Success "Git安装完成"
} else {
Write-Warning-Custom "Git安装可能未完全成功,退出码:$LASTEXITCODE"
Write-Info "请手动检查Git是否已安装"
}
} catch {
Write-Error-Custom "Git安装失败: $_"
Write-Info "您可以手动下载安装:https://git-scm.com/download/win"
}
Write-Host ""
# 步骤2:安装PowerShell Core
Write-Host "📦 步骤 2:安装PowerShell Core" -ForegroundColor Magenta
Write-Info "正在使用winget安装PowerShell Core (pwsh)..."
try {
# 使用winget安装PowerShell Core
winget install --id Microsoft.PowerShell -e --silent --accept-package-agreements --accept-source-agreements
if ($LASTEXITCODE -eq 0) {
Write-Success "PowerShell Core安装完成"
} else {
Write-Warning-Custom "PowerShell Core安装可能未完全成功,退出码:$LASTEXITCODE"
Write-Info "请手动检查PowerShell Core是否已安装"
}
} catch {
Write-Error-Custom "PowerShell Core安装失败: $_"
Write-Info "您可以手动下载安装:https://github.com/PowerShell/PowerShell/releases/latest"
}
Write-Host ""
# 步骤3:刷新环境变量
Write-Host "🔄 步骤 3:刷新环境变量" -ForegroundColor Magenta
Write-Info "正在刷新环境变量,以便立即使用新安装的软件..."
# 刷新当前会话的环境变量
$env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User")
Write-Host ""
# 步骤4:验证安装
Write-Host "🔍 步骤 4:验证安装" -ForegroundColor Magenta
# 验证Git
Write-Info "检查Git安装..."
try {
$gitVersion = git --version 2>&1
if ($LASTEXITCODE -eq 0) {
Write-Success "Git已安装: $gitVersion"
} else {
Write-Warning-Custom "Git未在PATH中找到,可能需要重新启动终端或重启计算机"
}
} catch {
Write-Warning-Custom "Git未在PATH中找到,可能需要重新启动终端或重启计算机"
}
# 验证PowerShell Core
Write-Info "检查PowerShell Core安装..."
try {
$pwshVersion = pwsh --version 2>&1
if ($LASTEXITCODE -eq 0) {
Write-Success "PowerShell Core已安装: $pwshVersion"
} else {
Write-Warning-Custom "PowerShell Core未在PATH中找到,可能需要重新启动终端或重启计算机"
}
} catch {
Write-Warning-Custom "PowerShell Core未在PATH中找到,可能需要重新启动终端或重启计算机"
}
Write-Host ""
Write-Host "========================================" -ForegroundColor Green
Write-Host "✅ 安装完成" -ForegroundColor Green
Write-Host "========================================" -ForegroundColor Green
Write-Host ""
Write-Info "注意事项:"
Write-Info "1. 如果软件未在PATH中找到,请重新启动终端窗口"
Write-Info "2. 如果重新启动终端后仍然无法使用,请重启计算机"
Write-Info "3. Git配置文件位于: %USERPROFILE%\.gitconfig"
Write-Info "4. 使用 'git config --global user.name \"Your Name\"' 配置Git用户名"
Write-Info "5. 使用 'git config --global user.email \"your.email@example.com\"' 配置Git邮箱"
Write-Host ""