Created
May 18, 2024 13:54
-
-
Save AJpon/20e751bb53c310d052b62f26d883fc3e to your computer and use it in GitHub Desktop.
Windows版 GoogleDriveFS (GoogleDriveDesktop) にある、マウントポイントを自動的にLocalizeする機能のオンオフを切り替えるPowerShellスクリプト
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
param( | |
[Parameter()] | |
[ValidateSet("machine", "user")] | |
[string]$scope = "user", | |
[switch]$help | |
) | |
if ($help) { | |
Write-Output "`nThis script accepts the following arguments:" | |
Write-Output "--scope: Specify 'machine' or 'user'. The default is 'user'." | |
Write-Output "-?: Displays this help." | |
exit | |
} | |
function Start-DriveFS { | |
param ( | |
[Parameter(Mandatory = $true)] | |
[int]$maxTryCount | |
) | |
if ($maxTryCount -lt 1) { | |
Write-Error "Failed to start GoogleDriveFS.exe.: Max try count reached." | |
throw "Failed to start GoogleDriveFS.exe." | |
Exit 1 | |
} | |
try { | |
Start-Process -FilePath $driveFS -ErrorAction Stop | |
Write-Output "GoogleDriveFS.exe restarted.`n" | |
# Wait 10 sec for detect the process crash | |
Write-Output "Waiting for GoogleDriveFS.exe to start..." | |
for ($i = 0; $i -le 100; $i++) { | |
Write-Progress -Activity "Processing" -Status "$i% Complete:" -PercentComplete $i | |
Start-Sleep -Milliseconds 100 | |
} | |
Write-Progress -Activity "Processing" -Status "100% Complete:" -Completed | |
# Check if GoogleDriveFS.exe is running | |
$driveFSProcess = Get-Process -Name "GoogleDriveFS" -ErrorAction Stop | |
if ($driveFSProcess) { | |
Write-Output "GoogleDriveFS.exe is running.`nProcess ID: $($driveFSProcess.Id)`n`nProcess complete." | |
Exit 0 | |
} | |
else { | |
throw "GoogleDriveFS.exe is not running." | |
} | |
} | |
catch { | |
Write-Error "Failed to restart GoogleDriveFS.exe.: $_ " | |
Write-Output "Try restoring the registry key from $backupPath." | |
Copy-Item -Path $backupPath -Destination $regPath -ErrorAction SilentlyContinue | |
Write-Output "Registry key restored from $backupPath.`n" | |
Write-Output "Retry to start GoogleDriveFS..." | |
Stert-DriveFS -maxTryCount ($maxTryCount - 1) | |
} | |
} | |
$ErrorActionPreference = "Continue" | |
$drivefsConfigPathUser = "HKCU:\Software\Google\DriveFS" | |
$drivefsConfigPathMachine = "HKLM:\Software\Google\DriveFS" | |
$propertyName = "DisableLocalizedVirtualFolders" | |
$drivefsInfoPath = "HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\{6BBAE539-2232-434A-A4E5-9A33560C6283}" | |
$installLocation = Get-ItemProperty -Path $drivefsInfoPath -Name "InstallLocation" -ErrorAction SilentlyContinue | |
if ($null -ne $installLocation.InstallLocation) { | |
$driveFS = $installLocation.InstallLocation | |
try { | |
Write-Output "`nStopping GoogleDriveFS.exe..." | |
Get-Process | Where-Object { $_.Path -eq $driveFS } | Stop-Process -ErrorAction Stop | |
Write-Output "GoogleDriveFS.exe stopped.`n" | |
} | |
catch { | |
Write-Error "Failed to stop GoogleDriveFS.exe.: $_ `nPlease close Google Drive Desktop manually. And try again." | |
Pause | |
Exit 1 | |
} | |
$regPath = if ($scope -eq "machine") { $drivefsConfigPathMachine } elseif ($scope -eq "user") { $drivefsConfigPathUser } | |
Write-Output "Backing up registry key: $regPath..." | |
$backupPath = "$regPath.bak" | |
if (Test-Path $backupPath) { | |
Remove-Item -Path $backupPath -Force -ErrorAction SilentlyContinue | |
} | |
Copy-Item -Path $regPath -Destination $backupPath -ErrorAction SilentlyContinue | |
Write-Output "Backup created at $backupPath.`n" | |
Write-Output "Toggling $propertyName..." | |
try { | |
$propertyValue = Get-ItemProperty -Path $regPath -Name $propertyName -ErrorAction Stop | |
$currentValue = [bool]$propertyValue.$propertyName | |
Write-Output "$propertyName found in `e[95m$regPath`e[0m.`nValue is ${currentValue}." | |
# Toggle the value | |
# $propertyValue is DWORD | |
$newValue = -not $propertyValue.$propertyName | |
Set-ItemProperty -Path $regPath -Name $propertyName -Value $newValue -ErrorAction Stop | |
Write-Output "`n`e[95m$propertyName`e[0m toggled to `e[96m`e[1m`e[4m$newValue`e[0m." | |
} | |
catch { | |
Write-Output "$propertyName does not exist in `e[95m$regPath`e[0m. `nCreating..." | |
New-ItemProperty -Path $regPath -Name $propertyName -Value 1 -PropertyType DWORD -ErrorAction Stop | |
Write-Output "$propertyName created with value 1." | |
} | |
Write-Output "`nRestarting GoogleDriveFS.exe..." | |
Start-DriveFS -maxTryCount 3 | |
} | |
else { | |
Write-Error "Cannot resolve Google Drive Desktop install location.`nPlease Install or Repair Google Drive Desktop." | |
Pause | |
Exit 1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment