找到映射的 google 驱动器

Find the mapped google drive

If (!(Get-PSDrive R -PSProvider FileSystem -ErrorAction SilentlyContinue)) {
    #this function checks for the latest exe of google drive.  Then tries to start it.
    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 驱动器,然后再次测试。

最大的问题是假设 google 驱动器是 R: 驱动器。似乎没有命令行可以启动 google 带有驱动器号的驱动器或测试驱动器号是否为 google 驱动器的方法。

想法?

我找到了解决办法。看起来 Google Drive for Desktop(以前称为 Drive File Stream)以本地磁盘的形式呈现给操作系统,这意味着远程功能可能已融入驱动程序。虽然 PowerShell 中有一些方法可以跟踪特定驱动器正在使用的驱动程序,但有一个更简单的解决方案:

$googleDriveLetter = ( Get-PSDrive | Where-Object {
  $_.Description -eq 'Google Drive'
} ).Name

Description 是基于 Win32_VolumeLabel 属性,但只要管理员用户没有更改驱动器标签,它就应该是 Google Drive 在 GD 字母驱动器上。

考虑到驱动器标签很容易更改,这可能看起来不够可靠,但我真的找不到任何其他选项。 GD 驱动器是一个逻辑磁盘,没有物理备份,也没有 PNP 设备 ID 可参考以获取驱动程序名称。有趣的是,似乎也没有任何 Google 与 Google 驱动器相关的驱动程序出现在我的带有 GDD 的系统上。我不太确定 GDD 是如何安装自己的,因为在引擎盖下它看起来既不像物理磁盘也不像虚拟磁盘。


Apparently Google Drive looks like a local disk as far as PowerShell/Windows is concerned. The below won't work for this use case but is still useful in generally figuring out which mapped drives are network drives, and the mapping to specific servers or shares.

原回答

您应该能够使用以下方法查看驱动器映射端点:

Get-PSDrive -PSProvider FileSystem | Select Name, Root

本地驱动器将其本地路径挂载点显示为 Root(例如 C: 将显示 C:\,文件夹安装将显示它们安装的目录),但远程端点应该在那里有一个 UNC 路径。它的根路径应该有特定于 Google 的内容。因此,一旦您知道 Google 驱动器根目录是什么,驱动器号将是 Name 相同 PSDrive:

$googleDriveLetter = ( Get-PSDrive -PSProvider FileSystem | Where-Object {
  $_.Root -match '^GOOGLE_DRIVE_ROOT_PREFIX'
} ).Name

上面使用正则表达式在 Root 值的开头进行搜索,因此请确保在将 GOOGLE_DRIVE_ROOT_PREFIX 替换为时不要删除插入符号 ^不管那个值是什么。


请注意,为了安全起见,您可能希望在匹配之前将前缀放在 [regex]::Escape(string) 之前,因为 UNC 路径几乎总是包含正则表达式使用的特殊字符:

# This is not the GD prefix, just a regular UNC path example
$driveRootPrefix = "\server.domain.tld"
$escapedPrefix = [regex]::Escape($driveRootPrefix)

# In the Where-Object block
$_.Root -match "^$escapedPrefix"