-
Notifications
You must be signed in to change notification settings - Fork 1
/
backup-remotes.ps1
64 lines (56 loc) · 1.98 KB
/
backup-remotes.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# Define the output file path
$currentDir = Get-Location
$outputDir = Join-Path -Path $currentDir -ChildPath "private"
$outputFilePath = Join-Path -Path $outputDir -ChildPath "remotes.ps1"
# Create the directory if it doesn't exist
if (-Not (Test-Path $outputDir)) {
New-Item -Path $outputDir -ItemType Directory
}
# Clear the output file if it exists
if (Test-Path $outputFilePath) {
Clear-Content $outputFilePath
}
function Get-ChildItemLimitedDepth {
param (
[Alias("PSPath")]
[Parameter(Position = 0, Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
[String]$Path,
[Parameter(Position = 1)]
[Int32]$Depth = 0
)
if (Test-Path $Path) {
Get-ChildItem -Path $Path -Force -ErrorAction SilentlyContinue | ForEach-Object {
$_
if ($Depth -gt 0 -and $_.PSIsContainer) {
Get-ChildItemLimitedDepth -Path $_.FullName -Depth ($Depth - 1)
}
}
}
}
$homePath = [Environment]::GetFolderPath('UserProfile')
$sourcePath = Join-Path -Path $homePath -ChildPath 'Documents'
Get-ChildItemLimitedDepth -Path $sourcePath -Depth 3 | Where-Object { $_.Name -eq '.git' -and $_.PSIsContainer } | ForEach-Object {
Set-Location -Path $_.Parent.FullName
$parentDir = $_.Parent.FullName
$remotes = git remote -v
if (![string]::IsNullOrEmpty($remotes)) {
$remotes.Split("`n") | Where-Object { $_ -match '(origin).*(fetch)' } | ForEach-Object {
$remoteUrl = ($_ -split '\s+')[1]
$output = @"
`n# Create directory and navigate to it
mkdir `"$parentDir`" -Force
takeown /F `"$parentDir`" /R
cd `"$parentDir`"
# Initialize git and add remote
git init
git remote add origin $remoteUrl
cd `"$currentDir`"
"@
# Write to the output file
$output | Out-File -FilePath $outputFilePath -Append
}
}
else {
"No remotes found for $parentDir" | Out-File -FilePath $outputFilePath -Append
}
}