【问题标题】:Find the mapped google drive找到映射的谷歌驱动器
【发布时间】:2021-09-02 02:14:12
【问题描述】:
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 驱动器的方法。

想法?

【问题讨论】:

    标签: powershell flysystem-google-drive


    【解决方案1】:

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

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

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

    考虑到驱动器标签很容易更改,这似乎并不可靠,但我真的找不到任何其他选项。 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"
    

    【讨论】:

    • 本德,你是最伟大的!
    猜你喜欢
    • 1970-01-01
    • 2018-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多