2
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 Drive,然后再次测试。

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

想法?

4

1 回答 1

3

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

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

Description基于 的Label属性Win32_Volume,但只要管理用户没有更改驱动器标签,它就应该Google Drive位于 GD 盘符驱动器上。

考虑到驱动器标签很容易更改,这似乎并不可靠,但我真的找不到任何其他选项。GD 驱动器是一个没有物理支持的逻辑磁盘,也没有 PNP 设备 ID 可用于获取驱动程序名称。有趣的是,我的 GDD 系统上似乎也没有任何与 Google Drive 相关的 Google 驱动程序。我不确定 GDD 是如何自行安装的,因为它看起来既不是物理磁盘也不是虚拟磁盘。


就 PowerShell/Windows 而言,显然 Google Drive 看起来像一个本地磁盘。以下内容不适用于此用例,但在通常确定哪些映射驱动器是网络驱动器以及映射到特定服务器或共享时仍然有用。

原始答案

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

Get-PSDrive -PSProvider FileSystem | Select Name, Root

本地驱动器将显示其本地路径安装点Root(例如C:将显示C:\,文件夹安装将显示它们安装的目录),但远程端点应该有一个 UNC 路径。谷歌的根路径应该有一些特定的东西。因此,一旦您知道 Google Drive 根是什么,驱动器号将是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"
于 2021-09-01T16:17:18.487 回答