Skip to content

Instantly share code, notes, and snippets.

@agross
Created January 9, 2013 10:38
Show Gist options
  • Save agross/4492226 to your computer and use it in GitHub Desktop.
Save agross/4492226 to your computer and use it in GitHub Desktop.
$scriptPath = Split-Path -parent $MyInvocation.MyCommand.Definition
$folderMaps = @{
'ALT.NET' = 'd:\Users\agross\Coding\.NET\ALT.NET'
'AppData\Ghisler' = 'c:\Users\agross\AppData\Roaming\Ghisler'
'AppData\GnuCash' = 'c:\Users\agross\AppData\Roaming\GnuCash'
'AppData\Miranda IM' = 'c:\Users\agross\AppData\Roaming\Miranda IM'
'AppData\Outlook-Signaturen' = 'c:\Users\agross\AppData\Roaming\Microsoft\Signatures'
'AppData\RoboForm' = 'c:\Users\agross\AppData\Roaming\RoboForm'
'AppData\Subversion' = 'c:\Users\agross\AppData\Roaming\Subversion'
'AppData\dotPeek' = 'c:\Users\agross\AppData\Roaming\JetBrains\dotPeek\v1.0'
'AppData\WebStorm' = 'c:\Users\agross\.WebIde10\config'
'AppData\RubyMine' = 'c:\Users\agross\.RubyMine40\config'
'Bilder' = 'd:\Users\agross\Pictures'
'Dokumente' = 'd:\Users\agross\Documents\Dokumente'
'Support' = 'd:\Users\agross\Support\'
'ReSharper\VS2010\Plugins' = 'c:\Users\agross\AppData\Roaming\JetBrains\ReSharper\v7.1\Plugins'
'ReSharper\Settings' = 'd:\Users\agross\Coding\.NET\ReSharper'
'Ruby' = 'c:\Ruby'
'Tools' = 'c:\Tools'
'VS2012\Extensions' = 'c:\Users\agross\AppData\Local\Microsoft\VisualStudio\11.0\Extensions'
'VS2012\Settings' = 'd:\Users\agross\Documents\Visual Studio 2012\'
"VS2012\Settings on $env:COMPUTERNAME" = 'd:\Users\agross\Documents\Visual Studio 2012\Settings'
'FeedDemon' = 'c:\Program Files (x86)\FeedDemon\Data\Styles'
'WindowsPowerShell' = 'd:\Users\agross\Documents\WindowsPowerShell'
}
function Folder-Exists($path)
{
return Test-Path -PathType Container -Path $path
}
function Folder-IsLink($path)
{
$folder = Get-Item $path -Force
return [bool]($folder.Attributes -band [System.IO.FileAttributes]::ReparsePoint)
}
function Folder-IsSetup($path)
{
return $(Folder-Exists($path)) -and $(Folder-IsLink($path))
}
function ConvertTo-PlainText([Security.SecureString] $secure)
{
$marshal = [Runtime.InteropServices.Marshal]
$marshal::PtrToStringAuto($marshal::SecureStringToBSTR($secure))
}
$global:password = $null
function Get-AdminPassword()
{
if ($global:password)
{
return ConvertTo-PlainText $global:password
}
$global:password = Read-Host -AsSecureString "Please enter the password for $Env:USERDOMAIN\Administrator"
return ConvertTo-PlainText $global:password
}
function Make-Hardlink($item)
{
$parent = Split-Path $item.Value -Parent
New-Item -Path $parent -Type Directory -ErrorAction SilentlyContinue
$cmd = "cmd.exe /c mklink /d `"{0}`" `"{1}\{2}`" " -f $item.Value, $scriptPath, $item.Key
if ($Env:USERNAME -ne "Administrator")
{
$password = Get-AdminPassword
$cmd = ".\psexec.exe -u $Env:USERDOMAIN\Administrator -p $password $cmd"
}
return $cmd
}
$todo = $folderMaps.GetEnumerator() | Where-Object { $(Folder-IsSetup($_.Value)) -eq $false } | Sort-Object Name
if ($todo -ne $null) # $null = nothing to do
{
Write-Host "Missing folders or folders that are not hard-linked:"
$todo | Format-Table -AutoSize @{Label="Dropbox folder"; Expression={ $_.Key }}, `
@{Label=""; Expression={ " -> " }}, `
@{Label="System folder"; Expression={ $_.Value }}
}
else
{
Write-Host "No missing folders or folders that are not hard-linked"
Return
}
$toDelete = $todo | Where-Object { $(Folder-Exists($_.Value)) -eq $true }
if ($toDelete -ne $null)
{
Write-Host "Deleting existing system folders (will be confirmed)"
$toDelete | ForEach-Object { Remove-Item -Recurse -Confirm -Force -Path $_.Value }
}
else
{
Write-Host "No existing system folders need to be deleted"
}
$notLocal = $todo | Where-Object { $(Folder-Exists($("{0}\{1}" -f $scriptPath, $_.Key))) -eq $false }
if ($notLocal -ne $null)
{
Write-Host "The following folders are not in your Dropbox folder:"
$notLocal | Format-Table -AutoSize @{Label="Dropbox folder"; Expression={ $_.Key }}, `
@{Label=""; Expression={ " -> " }}, `
@{Label="System folder"; Expression={ $_.Value }}
Write-Host "Wait for them to be synchronized. Will exit now."
Read-Host
Return
}
Write-Host "Creating hardlinks between Dropbox and system folders"
$todo | ForEach-Object { Invoke-Expression $(Make-Hardlink($_)) }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment