admin管理员组

文章数量:1593159

文章目录

    • abstract
    • 脚本说明
      • 可能出现的报错问题👺
      • 还有其他类型的错误👺
        • 下载阶段发生报错或失败
        • 下载成功但是安装时报错
    • 下载脚本
      • 脚本版本更新
      • 权限说明
    • 安装脚本👺
      • Notes
    • 获取脚本文件及其使用
      • 导入
      • 执行方案
      • FAQ
    • 测试部署脚本
    • 演示记录
      • 使用gitee方案部署
      • 使用github镜像加速部署
      • 添加额外buckets
    • 提高可用性的措施👺
    • refs

abstract

  • 本文在线链接: 国内用户快速部署Scoop@一键加速安装scoop@Deploy-ScoopForCNUser_scoop 安装-CSDN博客

  • 适合中国用户一键部署加速过的scoop包管理器:scoop-cn/Deploy-ScoopForCNUser at master · xuchaoxin1375/scoop-cn (github)

  • 脚本参考了多个相关的项目,做了整合,相关参考链接放到函数文档中

  • 直奔本文重点请跳转到 [下载和安装脚本]一节

脚本说明

  • 以下脚本对原脚本的扩展和改进,提供更多的灵活性和安装方案,以及更加详细的注释
  • 对于某些精简版或修改版系统默认将用户权限提到Administrator的情况提供安装支持选项
  • 关于脚本的稳定性和部署速度,这主要取决于加速镜像,Get-AvailableGithubMirros列出一些常见的镜像,它们当中在不同时段加速表现可能各不相同,如果你发现默认镜像或者你挑选的镜像加速效果不佳,那么请更换镜像

可能出现的报错问题👺

  • 在更换镜像后,部署脚本重新执行可能会报错(也可能顺利执行),如果报错,可能是因为之前的错误或者不完整的安装执行操作污染了安装环境,可以尝试scoop清理缓存,提升到管理员权限等措施再重试安装

  • 另一类报错是安装完毕而在使用过程中报错

  • 其中gitee方案部分时候会报错,比如代理服务器代理过程中发生错误(500系列的错误),我们可以选择重试

    PS> sudo pwsh -c scoop install gdb -g
    Installing 'gdb' (14.1) [64bit] from 'main' bucket
    proxy: https://nuwen.net/files/mingw/components-19.0.7z
    The remote server returned an error: (530) Server Error.
    URL https://nuwen.net/files/mingw/components-19.0.7z is not valid
    
    PS> sudo pwsh -c scoop install gdb -g
    #自动清理之前的不成功安装
    WARN  Purging previous failed installation of gdb.
    ERROR 'gdb' isn't installed correctly.
    Removing older version (14.1).
    'gdb' was uninstalled.
    
    Installing 'gdb' (14.1) [64bit] from 'main' bucket
    proxy: https://nuwen/files/mingw/components-19.0.7z
    components-19.0.7z (58.6 MB) [===================================================] 100%
    Checking hash of components-19.0.7z ... ok.                                                                             
    Extracting components-19.0.7z ... done.
    Running pre_install script...done.
    Linking C:\ProgramData\scoop\apps\gdb\current => C:\ProgramData\scoop\apps\gdb\14.1
    Creating shim for 'gdb'.
    Creating shim for 'gdbserver'.                                                                                          
    'gdb' (14.1) was installed successfully!
    

还有其他类型的错误👺

下载阶段发生报错或失败
  • 从默认bucket或者指定bucket下载失败,可以考虑更换其他bucket(如果其他bucket也有的话)
  • 启用aria2下载可能会被拒绝,尤其还不恰当的aria2参数配置,可能导致下载出错,考虑禁用(可以临时禁用)掉aria2
  • 中断和恢复
    • 假设你的buckets中(可能是同一个bucket但是分别用不同的镜像加速成不同名字的bucket,例如extras,extras1,extras2,…)有多个提供了你想要的包Pkgx.那么你可以从中选择一个或者默认不指定由scoop为你选择一个bucket下载并安装
    • 这时候你可能发现它下载的比较慢或者不够快,于是你尝试手动指定bucket换一个下载来源,这种情况下建议第二次安装时使用-g选项,这样会忽略掉之前下载的部分,否则可能因为之前下载的文件因为打断有错误或者版本有差异导致aria2下不动它
下载成功但是安装时报错
  • 未能够通过hash校验,可以尝试scoop install时重新下载-k,或者跳过校验-s选项
  • 全局安装权限不足可能报错
  • 安装过程中由于安装脚本和软件版本适配问题而报错,这种情况就比较复杂,考虑不用scoop安装,采用其他方式安装(这种情况比较少)
  • 需要的bucket未安装,例如spc bucket中有个包quarkpan,但是安装的时候提示你没有aki这个bucket,scoop提示你安装添加aki bucket,我们可以网络搜scoop bucket add aki,获取aki链接(可以配合加速镜像),或者找国内的加速项目获取加速的链接,例如scoop bucket add aki https://gitee/scoop-installer/aki-apps

下载脚本

脚本版本更新

  • 脚本版本说明(建议参考代码仓库以获取最新版本的部署脚本:PS/Deploy/deploy.psm1 · xuchaoxin1375/scripts - Gitee) 或者下载此文件导入到powershell后调用
  • 相关基础软件的我建议用scoop install -g选项全局安装,让windows上的所有用户都可以使用而不需要重复安装

权限说明

  • 通常普通权限用户就可以利用此脚本安装scoop
  • 个别情况下会出现安装失败,那么请使用管理员身份启动一个powershell窗口,然后在其中运行此部署脚本
    • 在管理员窗口下运行此脚本,脚本中提问你是否使用管理员权限安装时选择y才有效

安装脚本👺

  • 下载或者安装下面的脚本,然后用powershell执行(导入)
  • 当然,您也可以直接复制下面的代码,然后粘贴到powershell中(推荐powershell v7+)
  • 相关函数有多个,其中是Deploy-ScoopForCNUser是主函数
  • 代码比较长,推荐到gitee下载最新版脚本文件使用,这里作为备用

$GithubMirrors = @(
    # 注意,如果你的浏览器使用了代理,那么部分镜像站会和代理冲突,所以可能命令行中测试可以访问的链接在浏览器中确打不开镜像站,可以关闭代理或换个浏览器后重新验证该镜像站的可用性
    'https://github.moeyy.xyz', # Moeyy - 提供 GitHub 文件的加速下载功能
    'https://ghproxy',
    'https://ghproxy', # GitHub Proxy by GitHub99 - 提供 GitHub 文件的加速下载和克隆服务 #和用户自己的代理可能发生冲突
    'https://mirror.ghproxy',
    'https://gh.ddlc.top'
    # 'https://github-mirror.us.kg',#请打开此网站找到可用的镜像站,直接使用此来链接不适合实际用途,仅允许小流量代理
    
    # 'https://ghproxy/bad/demo'

    '' #空字符串收尾
)
function Get-AvailableGithubMirrors
{
    <#
    .SYNOPSIS
    列出流行的或可能可用的 GitHub 加速镜像站。
    列表中的镜像站可能会过期,可用性不做稳定性和可用性保证。

    .DESCRIPTION
    推荐使用 aria2 等多线程下载工具来加速下载,让镜像加速效果更加明显。
    

    .LINK
    # 镜像站搜集和参考
    https://github-mirror.us.kg/
    #>
    [CmdletBinding()]
    param(
        $mirrors = $GithubMirrors,
        [switch]$ListView,
        [switch]$PassThru,
        [switch]$SkipCheckAvailability
    )

    # 定义镜像站列表
    # $mirrors = @(
    #     'https://mirror.ghproxy',
    #     'https://ghproxy',
    #     'https://github.moeyy.xyz',
    #     'https://github-mirror.us.kg', 
    #     'https://gh.ddlc.top'
    #     'https://ghproxy/bad/demo'
    # )
    
    # 检查镜像站的可用性
   

    Write-Host 'Checking available mirrors...'
    $availableMirrors = $mirrors
    # 存储可用的镜像列表
    if (!$SkipCheckAvailability)
    {
        $availableMirrors = @()

        foreach ($mirror in $mirrors)
        {
            # Write-Verbose "Testing $mirror..."
            if (Test-MirrorAvailability -Url $mirror)
            {
                Write-Verbose "$mirror is available."
                Write-Host "`t $mirror" -ForegroundColor Green
                $availableMirrors += $mirror
            }
            else
            {
                Write-Verbose "$mirror is not available."
                Write-Host "`t $mirror." -ForegroundColor Red
            }
        }
    }

    # 显示可用的镜像
    Write-Host "`nAvailable mirrors:"
    # 空白镜像保留
    $availableMirrors = @('') + $availableMirrors
    Write-Host ' 0: Use No Mirror' -NoNewline
    $index = 1
    $availableMirrors | ForEach-Object {
        # $index = [array]::IndexOf($availableMirrors, $_)
        # if($availableMirrors[$index] -eq ""){continue}
        if ($_.Trim())
        {

            Write-Host " ${index}: $_" -NoNewline
            $index += 1
        }
   
        
        Write-Host ''
    }

    if ($PassThru)
    {
        return $availableMirrors
    }
}

function Test-MirrorAvailability
{
    [CmdletBinding()]
    param (
        [string]$Url
    )

    try
    {
        # 使用 Invoke-WebRequest 检查可用性
        $response = Invoke-WebRequest -Uri $Url -Method Head -TimeoutSec 5 -ErrorAction Stop
        
        $availability = $response.StatusCode -eq 200
    }
    catch
    {
        $availability = $false
    }
    if ($VerbosePreference)
    {

        if ($availability)
        {

            Write-Host "Mirror $Url is available." -ForegroundColor Green
        }
        else
        {

            Write-Host "Mirror $Url is not available." -ForegroundColor Red
        }
    }
    return   $availability
}

function Get-SelectedMirror
{
    [CmdletBinding()]
    param (
        
        $default = 1
    )
    $mirrors = Get-AvailableGithubMirrors -PassThru
    $numOfMirrors = $mirrors.Count
    $range = "[0~$($numOfMirrors-1)]"
    $num = Read-Host -Prompt "Select the number of the mirror you want to use $range ?(default: $default)"
    # $mirror = 'https://mirror.ghproxy'
    $n = $num -as [int] #可能是数字或者空$null
    if ($VerbosePreference)
    {
 
        Write-Verbose "`$n=$n"
        Write-Verbose "`$num=$num"
        Write-Verbose "`$numOfMirrors=$numOfMirrors"
    }
    #  如果输入的是空白字符,则默认设置为0
    if ( $num.trim().Length -eq 0)
    {
        Write-Host 'choose the Default 1'
        $n = $default
    }
    elseif ($n -notin 0..($numOfMirrors - 1))
    {
        Throw " Input a number within the range! $range"
    }
    
    if ($n -gt 0)
    {
        # 用户选择了一个合法的数值
        Write-Host "You Selected mirror:[$n : $($mirrors[$n])]"
    }
    $mirror = $mirrors[$n]
    return $mirror
}
function Deploy-ScoopByGithubMirrors
{
    
    [CmdletBinding()]
    param (
        
        [switch]$InstallBasicSoftwares,
        $ScriptsDirectory = "$home/Downloads",
        [switch]$InstallForAdmin
    )
  
    # 获取可用的github加速镜像站(用户选择的)
    $mirror = Get-SelectedMirror
    ## 加速下载scoop原生安装脚本
    $script = (Invoke-RestMethod $mirror/https://raw.githubusercontent.com/scoopinstaller/install/master/install.ps1)
 
    $installer = "$ScriptsDirectory/scoop_installer.ps1"
    $installer_cn = "$ScriptsDirectory/scoop_cn_installer.ps1"
    # 利用字符串的Replace方法,将 https://github 替换为 $mirror/https://github加速
    $script> $installer
    $script.Replace('https://github', "$mirror/https://github") > $installer_cn
 
    # 根据scoopd官方文档,管理员(权限)安装scoop时需要添加参数 -RunAsAdmin参数,否则会无法安装
    # 或者你可以直接将上述代码下载下来的家目录scoop_installer_cn文件中的相关代码片段注释掉(Deny-Install 调用语句注释掉)
    # $r = Read-Host -Prompt 'Install scoop as Administrator Privilege? [Y/n]'
    # if ($r)
    # {
    #     #必要时请手动打开管理员权限的powershell,然后运行此脚本
    #     Invoke-Expression "& $installer_cn -RunAsAdmin"
    # }
    # else
    # {
 
    #     Invoke-Expression "& $installer_cn"
    # }
    if ($InstallForAdmin)
    {
        #必要时请手动打开管理员权限的powershell,然后运行此脚本
        Invoke-Expression "& $installer_cn -RunAsAdmin"
    }
    else
    {
        Invoke-Expression "& $installer_cn"
    }
 
    # 将 Scoop 的仓库源替换为代理的
    scoop config scoop_repo $mirror/https://github.com/ScoopInstaller/Scoop
 
    
    
    # 可选部分
    ## 如果没有安装 Git等常用工具,可以解开下面的注释
    ## 先下载几个必需的软件的 JSON,组成一个临时的应用仓库
    if ($InstallBasicSoftwares)
    {
        New-Item -ItemType 'directory' -Path "$env:USERPROFILE\scoop\buckets\scoop-cn\bucket"
        New-Item -ItemType 'directory' -Path "$env:USERPROFILE\scoop\buckets\scoop-cn\scripts\7-zip"
        New-Item -ItemType 'directory' -Path "$env:USERPROFILE\scoop\buckets\scoop-cn\scripts\git"
        # 7zip软件资源
        Invoke-RestMethod -Uri $mirror/https://raw.githubusercontent.com/duzyn/scoop-cn/master/bucket/7zip.json -OutFile "$env:USERPROFILE\scoop\buckets\scoop-cn\bucket\7zip.json"
        #注册7-zip的右键菜单等操作
        Invoke-RestMethod -Uri $mirror/https://raw.githubusercontent.com/duzyn/scoop-cn/master/scripts/7-zip/install-context.reg -OutFile "$env:USERPROFILE\scoop\buckets\scoop-cn\scripts\7-zip\install-context.reg"
        Invoke-RestMethod -Uri $mirror/https://raw.githubusercontent.com/duzyn/scoop-cn/master/scripts/7-zip/uninstall-context.reg -OutFile "$env:USERPROFILE\scoop\buckets\scoop-cn\scripts\7-zip\uninstall-context.reg"

        # git软件资源
        Invoke-RestMethod -Uri $mirror/https://raw.githubusercontent.com/duzyn/scoop-cn/master/bucket/git.json -OutFile "$env:USERPROFILE\scoop\buckets\scoop-cn\bucket\git.json"
     
        #注册git右键菜单等操作
        Invoke-RestMethod -Uri $mirror/https://raw.githubusercontent.com/duzyn/scoop-cn/master/scripts/git/install-context.reg -OutFile "$env:USERPROFILE\scoop\buckets\scoop-cn\scripts\git\install-context.reg"
        Invoke-RestMethod -Uri $mirror/https://raw.githubusercontent.com/duzyn/scoop-cn/master/scripts/git/uninstall-context.reg -OutFile "$env:USERPROFILE\scoop\buckets\scoop-cn\scripts\git\uninstall-context.reg"
        Invoke-RestMethod -Uri $mirror/https://raw.githubusercontent.com/duzyn/scoop-cn/master/scripts/git/install-file-associations.reg -OutFile "$env:USERPROFILE\scoop\buckets\scoop-cn\scripts\git\install-file-associations.reg"
        Invoke-RestMethod -Uri $mirror/https://raw.githubusercontent.com/duzyn/scoop-cn/master/scripts/git/uninstall-file-associations.reg -OutFile "$env:USERPROFILE\scoop\buckets\scoop-cn\scripts\git\uninstall-file-associations.reg"
        #注册aria2
        Invoke-RestMethod -Uri $mirror/https://raw.githubusercontent.com/duzyn/scoop-cn/master/bucket/aria2.json -OutFile "$env:USERPROFILE\scoop\buckets\scoop-cn\bucket\aria2.json"
 
        # 安装时注意顺序是 7-Zip, Git, Aria2
        # 基础软件可以考虑全局安装(所有用户可以用,这需要管理员权限)
        scoop install scoop-cn/7zip -g
        scoop install scoop-cn/git -g
        scoop install scoop-cn/aria2 -g
        # 推荐使用aria2,设置多路下载
        # scoop config aria2-split 16
        scoop config aria2-options '-s 16 -x 16 -k 1M --retry-wait=2 --async-dns false'
    }
     
 
    # 将 Scoop 的 main 仓库源替换为代理加速过的
    if (Test-Path -Path "$env:USERPROFILE\scoop\buckets\main")
    {
        # 先移除默认的源,然后添加同名bucket和加速后的源
        scoop bucket rm main
    }
    Write-Host 'Adding speedup main bucket...'+" powered by: [$mirror]"
    scoop bucket add main $mirror/https://github.com/ScoopInstaller/Main
 
    # 之前的scoop-cn 库是临时的,还不是来自Git拉取的完整库,删掉后,重新添加 Git 仓库
    Write-Host 'remove Temporary scoop-cn bucket...'
    if (Test-Path -Path "$env:USERPROFILE\scoop\buckets\scoop-cn")
    {
        scoop bucket rm scoop-cn
    }
    Write-Host 'Adding scoop-cn bucket (from git repository)...'
    scoop bucket add scoop-cn $mirror/https://github.com/duzyn/scoop-cn
 
    # Set-Location "$env:USERPROFILE\scoop\buckets\scoop-cn"
    # git config pull.rebase true
 
    Write-Host 'scoop and scoop-cn was installed successfully!'
    return $mirror
     
}
function Deploy-ScoopByGitee
{
    [CmdletBinding()]
    param (
        [switch]$InstallForAdmin,
        [switch]$InstallBasicSoftwares
    )
    # 脚本执行策略更改
    Set-ExecutionPolicy -ExecutionPolicy bypass -Scope CurrentUser
    #如果询问, 输入Y或A,同意
    
    # 执行安装命令(默认安装在用户目录下,如需更改请执行“自定义安装目录”命令)
    
    ## 自定义安装目录(注意将目录修改为合适位置)
    if ($InstallForAdmin)
    {
        $Script = "$home\Downloads\install.ps1"
        Invoke-RestMethod scoop.201704.xyz -OutFile $script #'install.ps1'
        # .\install.ps1 -ScoopDir 'D:\Scoop' -ScoopGlobalDir 'D:\GlobalScoopApps'
        & $Script -RunAsAdmin
    }
    else
    {

        Invoke-WebRequest -useb scoop.201704.xyz | Invoke-Expression
    }
    #添加包含国内软件的的scoopcn bucket,其他bucket可以自行添加
    # 更换scoop的repo地址
    scoop config SCOOP_REPO 'https://gitee/scoop-installer/scoop'
    # 拉取新库地址
    scoop update
    Write-Verbose 'Scoop add more buckets(this process may failed to perform!You can retry to add buckets manually later!'
    scoop bucket add extras
    scoop bucket add scoopcn https://gitee.com/scoop-installer/scoopcn
    if ($InstallBasicSoftwares)
    {
        scoop install 7zip git aria2 -g
    }
}
function Add-ScoopBuckets
{
    <# 
    .SYNOPSIS
    基本上,添加spc这个bucket就够了,软件数量很丰富
    .DESCRIPTION
    可以根据自己的需要往里面修改或添加更多的bucket
    .NOTES
    建议在Deploy-ScoopForCNUser调用时就指定相应的参数,不推荐单独调用(需要传入加速镜像地址参数)
    .EXAMPLE
    Add-ScoopBuckets -mirror  'https://mirror.ghproxy'
    #>
    #>
    [CmdletBinding()]
    param (
        # [parameter(Mandatory = $true)]    
        $mirror,
        [switch]$NoMirror
    )
    if ($mirror)
    {

        Write-Verbose "The mirror is: $mirror"    
    }
    elseif ($NoMirror  )
    {
        $mirror = ''
        Write-Verbose 'The mirror is not specified!'
    }
    else
    {
        $mirror = Get-SelectedMirror
        Write-Verbose "The mirror is: $mirror"
    }
    $spc = "$mirror/https://github/lzwme/scoop-proxy-cn".Trim('/')

    Write-Host 'Adding more buckets...(It may take a while, please be patient!)'
    Write-Verbose "The spc bucket is: $spc"
    scoop bucket add spc $spc  
            
}

function Deploy-ScoopAppsPath
{
    <# 
    .SYNOPSIS
    让scoop安装的GUI软件可以从命令行中启动(.LNK)
    .DESCRIPTION
    通过配置Scoop Apps目录添加到Path变量,以及PathExt系统环境变量中添加.LNK实现命令行中启动LNK快捷方式
    包括scoop install 和 scoop install -g 所安装的软件
    # 需要以管理员权限运行此脚本
    #>
    [CmdletBinding()]
    param()
    # 定义 Scoop Apps 目录路径
    $scoopAppsPathEx = [System.Environment]::ExpandEnvironmentVariables('%USERPROFILE%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Scoop Apps')
    $scoopAppsPath = '%USERPROFILE%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Scoop Apps'

    # 修改用户 PATH 环境变量
    $userPath = [System.Environment]::GetEnvironmentVariable('PATH', 'User')
    if ($userPath -notlike "*$scoopAppsPathEx*")
    {
        $newUserPath = $scoopAppsPath + ';' + $userPath
        [System.Environment]::SetEnvironmentVariable('PATH', $newUserPath, 'User')
        Write-Host '已将 Scoop Apps 目录添加到用户 PATH 环境变量中。'
    }
    else
    {
        Write-Host 'Scoop Apps 目录已在用户 PATH 环境变量中。'
    }
    #刷新当前shell中的Path变量(非永久性,当前shell会话有效)
    $env:path += $scoopAppsPath
    # 修改系统 PATHEXT 环境变量
    $systemPathExt = [System.Environment]::GetEnvironmentVariable('PATHEXT', 'Machine')
    if ($systemPathExt -notlike '*.LNK*')
    {
        $newSystemPathExt = '.LNK' + ';' + $systemPathExt
        [System.Environment]::SetEnvironmentVariable('PATHEXT', $newSystemPathExt, 'Machine')
        Write-Host '已将 .LNK 添加到系统 PATHEXT 环境变量中。'
    }
    else
    {
        Write-Host '.LNK 已在系统 PATHEXT 环境变量中。'
    }
    #全局安装的GUI软件添加到Path(系统级Path)
    $systemPath = [System.Environment]::GetEnvironmentVariable('PATH', 'Machine')
    $ScoopAppsG = 'C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Scoop Apps'
    if ($systemPath -notlike "*$ScoopAppsG*")
    {

        $newSystemPath = $scoopAppsG + ';' + $SystemPath
        [System.Environment]::SetEnvironmentVariable( 'Path', $newSystemPath, 'Machine')
        Write-Host '已将 全局Scoop Apps 添加到系统 PATH 环境变量中。'
    }
    else
    {
        Write-Host '全局Scoop Apps 已在系统 PATH 环境变量中。'
    }

    Write-Host '环境变量修改完成。请重新启动命令提示符或 PowerShell 以使更改生效。'
}

function Update-ScoopMirror
{
    [CmdletBinding()]
    param (
        $Mirror,
        [switch]$BasickBucketsOnly,
        $BackupSpcWithName = '',
        [switch]$UpdateSpc,
        [switch]$UpdateSpcOnly
    )
    if (!$Mirror)
    {
        $Mirror = Get-SelectedMirror
    }
    $scoop_repo = "$mirror/https://github/ScoopInstaller/Scoop".Trim('/')
    $Spc = "$mirror/https://github/lzwme/scoop-proxy-cn".Trim('/')
    if ($UpdateSpcOnly)
    {
        scoop bucket rm spc
        scoop bucket add spc $spc
        return
    }
    elseif ($BackupSpcWithName)
    {
        scoop bucket add $BackupSpcWithName $Spc
        return #仅增加spc的冗余bucket,完成后结束函数
    }
    if ($VerbosePreference)
    {
        # 查询旧的配置和bucket
        scoop config
        scoop bucket list    

    }
    # 是否只更新bucket而不更新scoop_repo
    if (!$BasickBucketsOnly)
    {
        scoop config scoop_repo $scoop_repo
        scoop update
    }
    #更新bucket(先移除,后更新)
    # 移除旧bucket
    Write-Verbose 'Removing old buckets...'
    $buckets = @('main', 'extras')
    foreach ($bucket in $buckets)
    {
        Write-Verbose "Removing $bucket bucket..."
        scoop bucket rm $bucket 2> $null #如果不存在,直接重定向到$null,利用 2> 重定向错误输出,正常执行则输出普通信息
    }
    # 添加(更新)基本 bucket
    $main = "$mirror/https://github/ScoopInstaller/Main".Trim('/')
    $extras = "$mirror/https://github/ScoopInstaller/Extras".Trim('/')
    scoop bucket add main $main
    scoop bucket add extras $extras
    # 是否更新spc bucket
    if ($UpdateSpc)
    {
        scoop bucket rm spc
        scoop bucket add spc $Spc
    }

    
}
function Deploy-ScoopForCNUser
{
 
    # & "$PSScriptRoot\scoopDeploy.ps1"
    
    <# 
.SYNOPSIS
国内用户部署scoop
.Description
允许用户在一台没有安装git等软件的windows电脑上部署scoop包管理工具
如果你事先安装好了git,那么可以选择不安装(默认行为)

脚本会通过github镜像站加速各个相关链接进行达到提速的目的
    通过加速站下载原版安装脚本
    通过替换原版安装脚本中的链接为加速链接来加速安装scoop
    根据需要创建临时的bucket,让用户可以通过scoop来安装git等软件
针对某些Administrator用户,scoop默认拒绝安装,这里根据官方指南,做了修改,允许用户选择仍然安装

使用gitee方案的,默认的bucket main 是加速过的,安装 7z,git等软件比较方便,不像镜像加速方案需要先自行建立临时的bucket提供初始下载
所以这里InstallBasicSoftwares参数是工给加速镜像方案的,不为gitee方案使用,让不同方案内体验更一致
.NOTES
代码来自git/gitee上的开源项目(感谢作者的相关工作和贡献)
.EXAMPLE
deploy-ScoopForCNUser
# 采用默认镜像加速方案部署scoop,并且安装基础软件(7z,git,aria2等),适合于新电脑环境下使用(如果需要为管理员权限安装,请追加-InstallForAdmin参数)
deploy-ScoopForCNUser -InstallBasicSoftwares

deploy-ScoopForCNUser -InstallBasicSoftwares -AddScoopBuckets #部署的时候一并添加常用的bucket

# 简洁用法:已经安装了7z git等软件,直接部署镜像加速的scoop
deploy-ScoopForCNUser #不需要参数
# 部署Gitee上的scoop爱好者贡献的加速仓库资源项目加速(最方便,但是可能比消耗资源)
例如,这里选择以管理员权限安装scoop,并且安装基础软件(7z,git,aria2等),使用了一下选项
deploy-ScoopForCNUser -UseGiteeForkAndBucket -InstallBasicSoftwares -InstallForAdmin 
# 

.DESCRIPTION
使用镜像加速下载scoop原生安装脚本并做一定的修改提供加速安装(但是稳定性和可靠性不做保证)
此脚本参考了多个开源方案,为提供了更多的灵活性和备用方案的选择,尤其是可以添加spc这个大型bucket,以提供更多的软件包
.LINK
镜像加速参考
https://github.akams/ 
.LINK
https://gitee/twelve-water-boiling/scoop-cn
.LINK
# 提供 Deploy-ScoopByGitee 实现资源
https://gitee/scoop-installer/scoop
.LINK
# 提供 Deploy-scoopbyGithubMirrors 实现方式
https://lzw.me/a/scoop.html#2%20%E5%AE%89%E8%A3%85%20Scoop
.LINK
# 提供 大型bucket spc 资源
https://github/lzwme/scoop-proxy-cn
.LINK
相关博客
#提供 Deploy-ScoopForCNUser 整合与改进
https://cxxu1375.blog.csdn/article/details/121067836

在这里搜索scoop相关笔记
https://gitee/xuchaoxin1375/blogs/blob/main/windows 

#>
    # [CmdletBinding(DefaultParameterSetName = 'Manual')]
    param(
       
        # 是否仅查看内置的候选镜像列表
        # [switch]$CheckMirrorsBuildin,
        # 从镜像列表中选择镜像
        # [switch]$SelectMirrorFromList,
        # 是否安装基础软件,比如git等(考虑到有些用户已经安装过了,我们可以按需选择)
        # [parameter(ParameterSetName = 'Manual')]
        [switch]$InstallBasicSoftwares,
        [parameter(ParameterSetName = 'Gitee')]
        [switch]$UseGiteeForkAndBucket,
        
        # 是否添加一个大型的bucket
        # [switch]$AddMoreBuckets,

        # 管理员权限下安装
        [switch]$InstallForAdmin,
        # 延迟启动安装,给用户一点时间反悔
        $delay = 1
    )
    
    
    # return $mirror

    # 安装 Scoop
    # Gitee方案(简短,执行完后自动退出)
    if ($UseGiteeForkAndBucket)
    {
        Write-Host 'UseGiteeForkAndBucket scheme...'
        Start-Sleep $delay
        Deploy-ScoopByGitee -InstallBasicSoftwares:$InstallBasicSoftwares -InstallForAdmin:$InstallForAdmin 

 
    }
    # 手动配置镜像方案
    else
    {
        Write-Host 'Use manual scheme...'
        # Start-Sleep $delay
        Deploy-ScoopByGithubMirrors -InstallBasicSoftwares:$InstallBasicSoftwares -InstallForAdmin:$InstallForAdmin

    }


    # if ($addMoreBuckets)
    # {
    #     # 可以单独执行add-scoopbuckets
    #     Add-ScoopBuckets $mirror #无论$mirror取何值(空值或者链接字符串,采用位置参数传参都不影响执行)
    # }
    #检查用户安装了哪些bucket,以及对应的bucket源链接
    scoop bucket list

}

Notes

  • 上述脚本经过测试,可以正常工作
  • 可以新建一个本地账户(利用语句net user tester 1 /add创建一个测试用户tester,密码是1);登陆tester来验证其是否能工作

获取脚本文件及其使用

导入

  • 方案1:从脚本文件导入powershell

    • 可以复制粘贴上述代码到文本文件,然后修改后缀为.ps1

    • 也从仓库的Deploy-ScoopForCNUser中保存脚本文件

    • 然后打开powershell执行这个脚本文件,会导入其中的函数

  • 方案2:通过复制粘贴到powershell中回车导入

执行方案

  • 导入完毕后,选择执行方案,调用语法查看 gcm Deploy-ScoopForCNUser -Syntax

    • PS C:\repos\scoop-cn> gcm Deploy-ScoopForCNUser -Syntax
      
      Deploy-ScoopForCNUser [-InstallBasicSoftwares] [-UseGiteeForkAndBucket] [-InstallForAdmin] [-delay <Object>] [<CommonParameters>]
      
  • 如果用户事先安装过Git,那么可以直接执行Deploy-ScoopForCNUser,其他参数可选

  • 对于没有安装过Git的用户,建议使用(否则会因为确实Git而无法添加Bucket等操作)

    deploy-ScoopForCNUser -InstallBasicSoftwares
    
  • 或者使用另一种Gitee方案安装

    deploy-ScoopForCNUser -UseGiteeForkAndBucket
    
  • 可以追加-InstallForAdmin支持管理员安装

FAQ

  • 对于脚本文件方案,如果执行失败,可以设置执行策略:

    set-executionPolicy -Scope CurrentUser -ExecutionPolicy bypass
    
    • 然后重新尝试

测试部署脚本

  • 修改和更新此部署脚本时,为了不影响已有的scoop环境及其安装的软件,有多重隔离方法
    • 将原来的scoop家目录重命名,然后执行部署脚本
    • 新建一个测试用户tester,登录到tester在执行部署脚本
    • 用虚拟机或者另一台设备进行测试
  • 上面几种方法由简单到复杂,但是隔离效果也越好,通常第一种就够用了

演示记录

使用gitee方案部署

PS> deploy-scoopForCNUser -UseGiteeForkAndBucket -Verbose
UseGiteeForkAndBucket scheme...
VERBOSE: Requested HTTP/1.1 GET with 0-byte payload
VERBOSE: Received HTTP/1.1 response of content type text/plain of unknown size
VERBOSE: -------- PSBoundParameters --------
VERBOSE: -------- Environment Variables --------
VERBOSE: $env:USERPROFILE: C:\Users\cxxu
VERBOSE: $env:ProgramData: C:\ProgramData
VERBOSE: $env:SCOOP:
VERBOSE: $env:SCOOP_CACHE:
VERBOSE: $env:SCOOP_GLOBAL:
VERBOSE: -------- Selected Variables --------
VERBOSE: SCOOP_DIR: C:\Users\cxxu\scoop
VERBOSE: SCOOP_CACHE_DIR: C:\Users\cxxu\scoop\cache
VERBOSE: SCOOP_GLOBAL_DIR: C:\ProgramData\scoop
VERBOSE: SCOOP_CONFIG_HOME: C:\Users\cxxu\.config
Initializing...
Downloading...
VERBOSE: Cloning https://gitee/scoop-installer/scoop.git to C:\Users\cxxu\scoop\apps\scoop\current
VERBOSE: Cloning https://gitee/scoop-installer/Main.git to C:\Users\cxxu\scoop\buckets\main
Creating shim...
Scoop was installed successfully!
Type 'scoop help' for instructions.
'SCOOP_REPO' has been set to 'https://gitee/scoop-installer/scoop'
Updating Scoop...
Updating Buckets...
Scoop was updated successfully!
VERBOSE: Scoop add more buckets(this process may failed to perform!You can retry to add buckets manually later!
VERBOSE: Requested HTTP/1.1 GET with 0-byte payload
VERBOSE: Received HTTP/1.1 468-byte response of content type application/json
direct: https://gitee/scoop-installer/Extras
Checking repo... OK
The extras bucket was added successfully.
VERBOSE: Requested HTTP/1.1 GET with 0-byte payload
VERBOSE: Received HTTP/1.1 468-byte response of content type application/json
direct: https://gitee/scoop-installer/scoopcn
Checking repo... OK
The scoopcn bucket was added successfully.

Name    Source                                     Updated            Manifests
----    ------                                     -------            ---------
main    https://gitee/scoop-installer/Main.git 2024/8/29 8:33:52       1338
extras  https://gitee/scoop-installer/Extras   2024/8/29 10:45:29      2062
scoopcn https://gitee/scoop-installer/scoopcn  2024/8/28 16:34:02        30

使用github镜像加速部署

PS C:\Users\cxxu> deploy-ScoopForCNUser -InstallBasicSoftwares
Use manual scheme...
Available mirrors:
 0: https://mirror.ghproxy.com
 1: https://ghproxy.cc
 2: https://github.moeyy.xyz/
 3: https://ghproxy.net/
 4: https://gh.ddlc.top/
Select the number of the mirror you want to use [0~4] ?(default: 0): 0
$n=0
$num=0
$numOfMirrors=5
You Selected mirror:[0 : https://mirror.ghproxy.com]
Install scoop as Administrator Privilege? [Y/n]: n
Initializing...
Scoop is already installed. Run 'scoop update' to get the latest version.
Abort.
'scoop_repo' has been set to 'https://mirror.ghproxy/https://github/ScoopInstaller/Scoop'
New-Item: An item with the specified name C:\Users\cxxu\scoop\buckets\scoop-cn\bucket already exists.
New-Item: An item with the specified name C:\Users\cxxu\scoop\buckets\scoop-cn\scripts\7-zip already exists.
New-Item: An item with the specified name C:\Users\cxxu\scoop\buckets\scoop-cn\scripts\git already exists.
WARN  Purging previous failed installation of 7zip.
ERROR '7zip' isn't installed correctly.
Removing older version (24.08).
'7zip' was uninstalled.
Installing '7zip' (24.08) [64bit] from 'scoop-cn' bucket
7z2408-x64.msi (1.9 MB) [=====================================================================================] 100%
Checking hash of 7z2408-x64.msi ... ok.
Extracting 7z2408-x64.msi ... done.
Linking ~\scoop\apps\7zip\current => ~\scoop\apps\7zip\24.08
Creating shim for '7z'.
Creating shim for '7zFM'.
Making C:\Users\cxxu\scoop\shims\7zfm.exe a GUI binary.
Creating shim for '7zG'.
Making C:\Users\cxxu\scoop\shims\7zg.exe a GUI binary.
Creating shortcut for 7-Zip (7zFM.exe)
Persisting Codecs
Persisting Formats
Running post_install script...done.
'7zip' (24.08) was installed successfully!
Notes
-----
Add 7-Zip as a context menu option by running: "C:\Users\cxxu\scoop\apps\7zip\current\install-context.reg"
Installing 'git' (2.46.0) [64bit] from 'scoop-cn' bucket
PortableGit-2.46.0-64-bit.7z.exe (58.6 MB) [==================================================================] 100%
Checking hash of PortableGit-2.46.0-64-bit.7z.exe ... ok.
Extracting PortableGit-2.46.0-64-bit.7z.exe ... done.
Linking ~\scoop\apps\git\current => ~\scoop\apps\git\2.46.0
Creating shim for 'sh'.
Creating shim for 'bash'.
Creating shim for 'git'.
Creating shim for 'gitk'.
Making C:\Users\cxxu\scoop\shims\gitk.exe a GUI binary.
Creating shim for 'git-gui'.
Making C:\Users\cxxu\scoop\shims\git-gui.exe a GUI binary.
Creating shim for 'scalar'.
Creating shim for 'tig'.
Creating shim for 'git-bash'.
Making C:\Users\cxxu\scoop\shims\git-bash.exe a GUI binary.
Creating shortcut for Git Bash (git-bash.exe)
Creating shortcut for Git GUI (git-gui.exe)
Running post_install script...done.
'git' (2.46.0) was installed successfully!
Notes
-----
Set Git Credential Manager Core by running: "git config --global credential.helper manager"

To add context menu entries, run 'C:\Users\cxxu\scoop\apps\git\current\install-context.reg'

To create file-associations for .git* and .sh files, run
'C:\Users\cxxu\scoop\apps\git\current\install-file-associations.reg'
Installing 'aria2' (1.37.0-1) [64bit] from 'scoop-cn' bucket
aria2-1.37.0-win-64bit-build1.zip (2.4 MB) [==================================================================] 100%
Checking hash of aria2-1.37.0-win-64bit-build1.zip ... ok.
Extracting aria2-1.37.0-win-64bit-build1.zip ... done.
Linking ~\scoop\apps\aria2\current => ~\scoop\apps\aria2\1.37.0-1
Creating shim for 'aria2c'.
'aria2' (1.37.0-1) was installed successfully!
'aria2-options' has been set to '-s 16 -x 16 -k 1M --retry-wait=2 --async-dns false'
The main bucket was removed successfully.
Adding speedup main bucket... + powered by: [https://mirror.ghproxy]
Checking repo... OK
The main bucket was added successfully.
remove Temporary scoop-cn bucket...
The scoop-cn bucket was removed successfully.
Adding scoop-cn bucket (from git repository)...
Checking repo... OK
The scoop-cn bucket was added successfully.
scoop and scoop-cn was installed successfully!

Name     Source                                                            Updated            Manifests
----     ------                                                            -------            ---------
main     https://mirror.ghproxy/https://github/ScoopInstaller/Main 2024/8/26 12:29:00      1339
scoop-cn https://mirror.ghproxy/https://github/duzyn/scoop-cn      2024/8/26 9:41:36       5675

PS C:\Users\cxxu> Add-ScoopBuckets -mirror  'https://ghproxy.cc' -Verbose
VERBOSE: The mirror is: https://ghproxy
Adding more buckets...(It may take a while, please be patient!)
VERBOSE: The spc bucket is: https://ghproxy / https://github/lzwme/scoop-proxy-cn
Checking repo... ERROR 'https://ghproxy.cc / https://github.com/lzwme/scoop-proxy-cn' doesn't look like a valid git repository

Error given:
fatal: unable to access 'https://ghproxy / https://github/lzwme/scoop-proxy-cn/': URL rejected: Malformed input to a URL function
 
PS C:\Users\cxxu> add-ScoopBuckets -mirror  'https://ghproxy' -Verbose
VERBOSE: The mirror is: https://ghproxy.cc
Adding more buckets...(It may take a while, please be patient!)
VERBOSE: The spc bucket is: https://ghproxy.cc/https://github.com/lzwme/scoop-proxy-cn
Checking repo... OK
The spc bucket was added successfully.

添加额外buckets

PS🌙[BAT:100%][MEM:30.2% (2.37/7.85)GB][11:19:24]
# [cxxu@CXXUREDMIBOOK][<以:192.168.1.198>][~]
PS> Add-ScoopBuckets
Available mirrors:
 0: Use No Mirror
 1: https://mirror.ghproxy.com
 2: https://ghproxy.cc
 3: https://github.moeyy.xyz
 4: https://ghproxy.net
 5: https://gh.ddlc.top
Select the number of the mirror you want to use [0~5] ?(default: 1): 3
You Selected mirror:[3 : https://github.moeyy.xyz]
Adding more buckets...(It may take a while, please be patient!)
direct (Proxy by other URLs): https://github.moeyy.xyz/https://github.com/lzwme/scoop-proxy-cn
Checking repo... OK
The spc bucket was added successfully.

提高可用性的措施👺

  • 为大型仓库spc提供多个镜像加速bucket

  • 例如为spc这个大型仓库提供更多的冗余bucket(镜像加速)

    • scoop bucket add spc https://github.moeyy.xyz/https://github/lzwme/scoop-proxy-cn
      scoop bucket add spc1 https://ghproxy/https://github/lzwme/scoop-proxy-cn 
      scoop bucket add spc2 https://ghproxy/https://github/lzwme/scoop-proxy-cn
      scoop bucket add spc3 'https://mirror.ghproxy/https://github/lzwme/scoop-proxy-cn'
      

refs

.LINK
镜像加速参考
https://github.akams/
.LINK
https://gitee/twelve-water-boiling/scoop-cn
.LINK

提供 Deploy-ScoopByGitee 实现资源

https://gitee/scoop-installer/scoop
.LINK

提供 Deploy-scoopbyGithubMirrors 实现方式

https://lzw.me/a/scoop.html#2%20%E5%AE%89%E8%A3%85%20Scoop
.LINK

提供 大型bucket spc 资源

https://github/lzwme/scoop-proxy-cn
.LINK
相关博客
#提供 Deploy-ScoopForCNUser 整合与改进
https://cxxu1375.blog.csdn/article/details/121067836

在这里搜索scoop相关笔记
https://gitee/xuchaoxin1375/blogs/blob/main/windows

本文标签: 报错一键常见快速国内