11<#
22. SYNOPSIS
3- A short one-line action-based description, e.g. 'Tests if a function is valid'
3+ 批量下载指定 GitHub 用户的所有仓库。
4+
45. DESCRIPTION
5- A longer description of the function, its purpose, common use cases, etc.
6- . NOTES
7- Information or caveats about the function e.g. 'This function is not supported in Linux'
8- . LINK
9- Specify a URI to a help page, this will show when Get-Help -Online is used.
10- . EXAMPLE
11- Test-MyTestFunction -Verbose
12- Explanation of the function or its result. You can include multiple examples with additional .EXAMPLE lines
13- #>
6+ 此脚本用于下载指定 GitHub 用户的所有公开仓库。
7+ 可以选择是否在文件夹名称中包含日期。
148
9+ . PARAMETER UserName
10+ GitHub 用户名(必填)
1511
12+ . PARAMETER Path
13+ 下载仓库的本地路径,默认为当前目录
14+
15+ . PARAMETER WithDate
16+ 是否在文件夹名称中包含日期
17+
18+ . EXAMPLE
19+ .\downGithub.ps1 -UserName "octocat"
20+ 下载用户 octocat 的所有仓库到当前目录
1621
22+ . EXAMPLE
23+ .\downGithub.ps1 -UserName "octocat" -Path "D:\GitRepos" -WithDate
24+ 下载用户 octocat 的所有仓库到 D:\GitRepos,并在文件夹名称中包含日期
25+ #>
1726[CmdletBinding ()]
1827param (
1928 [Parameter (Mandatory = $true )]
2029 $UserName = ' ' ,
2130 # 下载执行的路径
2231 [string ]
23- $Path = ' .'
32+ $Path = ' .' ,
33+ [switch ]
34+ $WithDate
2435)
2536
2637$repos = Invoke-RestMethod - Uri " https://api.github.com/users/$UserName /repos" - Headers @ { " User-Agent" = " Mozilla/5.0" }
2738
2839$repos
2940
30- $repoParentPath = Join-Path $Path $UserName
41+ $folderMame = $UserName
42+
43+ if ($WithDate ) {
44+ $dateString = Get-Date - Format " yyyyMMdd"
45+ $folderMame = " $dateString -$UserName "
46+ }
47+
48+
49+ $repoParentPath = Join-Path $Path $folderMame
3150
3251if (-not (Test-Path $repoParentPath )) {
3352 New-Item - ItemType Directory - Force - Path $repoParentPath
@@ -39,9 +58,9 @@ foreach ($repo in $repos) {
3958 $repoUrl = $repo.clone_url
4059 $repoPath = " $repoParentPath \$repoName "
4160
42- # 检查是否已经存在该目录,如果不存在则克隆仓库
61+ # 检查是否已经存在该目录,如果不存在则克隆完整仓库镜像
4362 if (-not (Test-Path - Path $repoPath )) {
44- git clone $repoUrl $repoPath
63+ gh repo clone $repoUrl $repoPath -- -- mirror
4564 }
4665 else {
4766 Write-Output " Repository '$repoName ' already exists, skipping..."
0 commit comments