You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Google Drive映射盘符检测PowerShell脚本优化方案问询

Google Drive挂载盘检测脚本优化方案

原脚本逻辑说明

原脚本功能为检测R盘是否存在,不存在则启动Google Drive桌面端,重试后仍未检测到R盘则发送告警邮件,代码如下:

If (!(Get-PSDrive R -PSProvider FileSystem -ErrorAction SilentlyContinue)) {
    # 检测最新版本Google Drive可执行文件并启动
    Import-Module C:\Tasks\Modules\Find-LatestFileVersion\Find-LatestFileVersion.psm1
    $GdfsPath = Find-LatestFileVersion -fileName "GoogleDriveFS.exe" -filePath "$Env:Programfiles\Google"
    Try {
        Start-Process $GdfsPath.FilePath | Wait-Process -Timeout 30
    }
    Catch {
        Write-Warning "Drive R to Google Drive can't be mapped. $Email failed to record seperation."

        $mailBody = "Drive R to Google Drive can't be mapped. $Email failed to record seperation."
        $mailSubject = "Drive R to Google Drive can't be mapped. $Email failed to record seperation."
       
        Send-MailMessage @MailArguments -to $ENV:BUILD_USER_EMAIL -Subject $mailSubject -Body $mailBody
        throw "Drive R to Google Drive can't be mapped. $Email failed to record seperation."
    }
    Finally {
        If (!(Get-PSDrive R -PSProvider FileSystem -ErrorAction SilentlyContinue)) {
            Write-Warning "Drive R to Google Drive can't be mapped. $Email failed to record seperation."

            $mailBody = "Drive R to Google Drive can't be mapped. $Email failed to record seperation."
            $mailSubject = "Drive R to Google Drive can't be mapped. $Email failed to record seperation."
           
            Send-MailMessage @MailArguments -to $ENV:BUILD_USER_EMAIL -Subject $mailSubject -Body $mailBody
            throw "Drive R to Google Drive can't be mapped. $Email failed to record seperation." 
        }
    }
}

核心问题解决方案

针对无法通过命令行指定挂载盘符、无法识别Google Drive挂载盘的问题,可选择以下两种方案落地:

方案一:强制固定Google Drive挂载盘符为R盘

不需要通过启动参数指定,直接修改Google Drive的全局默认配置即可永久固定盘符:

  1. 进入Google Drive安装目录C:\Program Files\Google\Drive File Stream
  2. 新建/编辑配置文件drive_fs_defaults.config,添加以下内容:
<config>
    <setting name="mount_point" value="R:" />
</config>
  1. 重启Google Drive服务后,所有用户启动时都会默认挂载到R盘,原脚本无需修改即可正常运行。

方案二:动态检测Google Drive挂载盘符(无需固定盘符)

Google Drive挂载盘的根目录下存在专属隐藏系统文件.shortcut-targets-by-id,可通过该特征精准识别挂载盘符,替换原脚本中硬编码的R盘即可,优化后的脚本如下:

# 封装告警逻辑,减少重复代码
function Send-GDriveErrorAlert {
    param($ErrorMsg)
    Write-Warning $ErrorMsg
    Send-MailMessage @MailArguments -to $ENV:BUILD_USER_EMAIL -Subject $ErrorMsg -Body $ErrorMsg
    throw $ErrorMsg
}

# 检测是否已存在Google Drive挂载盘
$gdDrive = Get-PSDrive -PSProvider FileSystem | Where-Object {
    Test-Path (Join-Path $_.Root ".shortcut-targets-by-id") -ErrorAction SilentlyContinue
} | Select-Object -First 1

if (!$gdDrive) {
    Import-Module C:\Tasks\Modules\Find-LatestFileVersion\Find-LatestFileVersion.psm1
    $GdfsPath = Find-LatestFileVersion -fileName "GoogleDriveFS.exe" -filePath "$Env:Programfiles\Google"
    Try {
        Start-Process $GdfsPath.FilePath | Wait-Process -Timeout 30
        # 启动后预留加载时间再重新检测挂载盘
        Start-Sleep -Seconds 5
        $gdDrive = Get-PSDrive -PSProvider FileSystem | Where-Object {
            Test-Path (Join-Path $_.Root ".shortcut-targets-by-id") -ErrorAction SilentlyContinue
        } | Select-Object -First 1
        if (!$gdDrive) {
            Send-GDriveErrorAlert "Google Drive挂载失败,$Email 未成功记录离职信息"
        }
    }
    Catch {
        Send-GDriveErrorAlert "Google Drive启动失败,$Email 未成功记录离职信息"
    }
}

# 后续业务逻辑直接使用 $gdDrive.Root 作为Google Drive根路径即可,无需硬编码盘符

该方案无需修改用户配置,兼容性更强,适配所有自定义盘符的场景。

内容的提问来源于stack exchange,提问作者dcaz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.06 01:42:02