2

Windows安装rsync命令一键脚本

 1 year ago
source link: https://www.wyr.me/post/749
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

在Windows上安装rsync通常需要使用诸如WSL2或Cygwin等解决方案。然而,这些方法可能会导致性能下降、系统资源占用高以及用户体验不佳等问题。因此,我们将通过一个简单的PowerShell脚本来安装cwRsync,避免上述问题。

请注意: 执行这个脚本意味着您同意cwRsync的相关协议。

如何执行脚本?

  1. 通过以下命令启动PowerShell窗口:
powershell.exe
  1. 在PowerShell窗口中,运行以下命令以执行远程脚本:
Invoke-Expression (Invoke-WebRequest -Uri "https://tool.yizcore.xyz/InstallRsync.ps1" -UseBasicParsing).Content

这个命令会下载并运行位于 https://tool.yizcore.xyz/InstallRsync.ps1的脚本。

# 检查是否具有管理员权限
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

脚本做了什么事情?

这个脚本执行了以下操作:

  1. 检查管理员权限,如果没有,则以管理员身份重新启动。
  2. 下载并解压cwRsync到"C:\Program Files\rsync"。
  3. 将cwRsync的可执行文件添加到系统环境变量Path,以便在命令提示符或PowerShell中直接使用。
  4. 验证安装成功,并显示rsync版本信息。
rsync.png

完成这些操作后,您可以在Windows上直接使用rsync命令。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK