如何使用通配符在 Powershell 中选择最高值文件夹名称?

如何使用通配符在 Powershell 中选择最高值文件夹名称?

我正在编写一个脚本来终止并重新启动桌面版 Google Drive。我还需要用它来处理新版本。在我的测试机器上,可执行文件存储在:

C:\Program Files\Google\Drive File Stream\51.0.16.0

在安装了 Drive 一段时间的生产机器上进行测试时,出现了问题。事实证明,Google Drive 在更新时不会替换版本文件夹,而是只会添加另一个带有新版本号的文件夹,如下所示:

PS C:\Program Files\Google\Drive File Stream> DIR


    Directory: C:\Program Files\Google\Drive File Stream


Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d-----         9/16/2021   9:08 AM                51.0.14.0
d-----        10/15/2021  10:15 AM                51.0.16.0
d-----         9/10/2021  10:56 AM                51.0.9.0
d-----        10/20/2021  12:15 PM                52.0.6.0
d-----         9/10/2021  10:56 AM                Drivers
-a----        12/31/1979  11:00 PM         285478 docs.ico
-a----        12/31/1979  11:00 PM          22721 drive_fs.ico
-a----        12/31/1979  11:00 PM           1544 launch.bat
-a----        12/31/1979  11:00 PM         285478 sheets.ico
-a----        12/31/1979  11:00 PM         285478 slides.ico

我一直使用通配符来选择 exe 所在的文件夹,假设旧版本文件夹将在更新中被覆盖,使用以下行:

Start-Process  "C:\Program Files\Google\Drive File Stream\[0-9]*.*\GoogleDriveFS.exe"

如何让脚本考虑新版本?我不知道如何让它选择最新版本。

以下是完整的上下文脚本:

#Get Drive Process
$drive = Get-Process -Name "GoogleDriveFS" -ErrorAction SilentlyContinue 
if ($drive) {
    write "`n`nGoogle Drive is currently running. Shutting down Google Drive...`n`n"

    #Stop Drive
    $drive | Stop-Process

    #Wait 5 Seconds
    sleep 5

    #Check to see if its dead
    if(!$drive){
        write "`n`nGoogle Drive has successfully shut down, restarting Google Drive...`n"

        #Restart Drive
        Start-Process  "C:\Program Files\Google\Drive File Stream\[0-9]*.*\GoogleDriveFS.exe"

        #Wait 3 Seconds
        sleep 3

        #Check if Drive is running and display error message if it's not
        if ($drive) {
            write "`n`nGoogle Drive has successfully restarted."
        }

        else {
            write "`n`nGoogle Drive did not restart correctly. Please contact IT"
        }
    }
}

# Drive was not running to begin with
else {
    #Should I use:
    #        elseif ($drive -eq $null) instead?


    #Start Drive
    write "`n`nGoogle Drive is not running, Starting Google Drive..."
    Start-Process  "C:\Program Files\Google\Drive File Stream\[0-9]*.*\GoogleDriveFS.exe"

    #Wait 5 Seconds
    sleep 5

    #check
    if($drive) {
        write "`n`nGoogle Drive has successfully started.`n"
    }
 }

 write "`n`nThe Script is Complete"

编辑

谢谢李戴利帮助我找到路径。虽然生成了多个进程。我如何只获取其中一个?我无法仅使用输出来启动进程Select-Object Path

PS C:\Users\Law Clerk> Get-Process -Name "GoogleDriveFS" | Select-Object -Property Name, Id, Path

Name             Id Path                                                                
----             -- ----                                                                
GoogleDriveFS 11216 C:\Program Files\Google\Drive File Stream\52.0.6.0\GoogleDriveFS.exe
GoogleDriveFS 11368 C:\Program Files\Google\Drive File Stream\52.0.6.0\GoogleDriveFS.exe
GoogleDriveFS 21764 C:\Program Files\Google\Drive File Stream\52.0.6.0\GoogleDriveFS.exe
GoogleDriveFS 25184 C:\Program Files\Google\Drive File Stream\52.0.6.0\GoogleDriveFS.exe
GoogleDriveFS 25356 C:\Program Files\Google\Drive File Stream\52.0.6.0\GoogleDriveFS.exe
GoogleDriveFS 25676 C:\Program Files\Google\Drive File Stream\52.0.6.0\GoogleDriveFS.exe
GoogleDriveFS 27016 C:\Program Files\Google\Drive File Stream\52.0.6.0\GoogleDriveFS.exe

相关内容