Windows10安装rsync命令一键脚本

简介

windows上如果要安装rsync一般都需要用到WSL或者Cygwin之类的方法,但是这些方法会使性能下降,并高占用

系统资源导致痕不佳的用户体验。因此我们可以在PowerShell上来安装cwRsync,这里我放一个一键命令的脚本。

 

教程

在系统左下角以管理员身份打开PowerShell

在PowerShell当中,运行以下命令以执行安装脚本

Invoke-Expression (Invoke-WebRequest -Uri "https://raw.githubusercontent.com/yishengzuiai1128/Windows-classic-samples/master/InstallRsync.ps1" -UseBasicParsing).Content

脚本内容

# 检查是否具有管理员权限
function Test-IsAdmin {
    $identity = [Security.Principal.WindowsIdentity]::GetCurrent()
    $principal = New-Object Security.Principal.WindowsPrincipal($identity)
    return $principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
}

# 以管理员身份运行当前脚本
if (-not (Test-IsAdmin)) {
    $arguments = "-NoProfile -ExecutionPolicy Bypass -Command & { $($ExecutionContext.InvokeCommand.ExpandString($MyInvocation.MyCommand.Definition)) }"
    Start-Process powershell -Verb runAs -ArgumentList $arguments
    exit
}

# 下载并解压文件
$Url = "https://itefix.net/dl/free-software/cwrsync_6.2.8_x64_free.zip"
$ZipFile = "$env:TEMP\cwrsync.zip"
$RsyncFolder = "C:\Program Files\rsync"

Invoke-WebRequest $Url -OutFile $ZipFile
Expand-Archive $ZipFile -DestinationPath $RsyncFolder

# 添加到系统环境变量 Path
$systemPathKey = [Environment]::GetEnvironmentVariable("Path", [EnvironmentVariableTarget]::Machine)
if (-not ($systemPathKey.Contains("$RsyncFolder\bin"))) {
    $newSystemPathKey = $systemPathKey + ";$RsyncFolder\bin"
    [Environment]::SetEnvironmentVariable("Path", $newSystemPathKey, [EnvironmentVariableTarget]::Machine)
}

# 更新当前 PowerShell 会话的环境变量
$env:Path += ";$RsyncFolder\bin"

# 验证安装成功
$rsyncVersion = & rsync --version
if ($LASTEXITCODE -eq 0) {
    Write-Host "Rsync installation successful!"
    # Write-Host $rsyncVersion
} else {
    Write-Host "Rsync installation failed."
}

# 暂停等待按键
Write-Host "Press any key to exit..."
$host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown") | Out-Null

安装完成后我们输入命令测试一下,有如下图反馈信息就是正常安装成功了

阅读剩余
THE END