如何使用 PowerShell 獲取對映的驅動器?


使用 PowerShell 獲取對映的網路驅動器有幾種方法。

  • CMD 命令方法。

可以在 PowerShell 中使用 cmd 命令net use獲取對映的驅動器。

net use

輸出

PS C:\WINDOWS\system32> net use
New connections will be remembered.
Status       Local     Remote                    Network

-------------------------------------------------------------------------------
OK           K:        \localhost\shared folder Microsoft Windows Network
OK           L:        \localhost\Shared        Microsoft Windows Network
The command completed successfully.

要獲取遠端計算機上的對映驅動器,

Invoke-Command -ComputerName RemoteComputer -ScriptBlock{Net use}

Invoke-Command -ComputerName RemoteComputer -ScriptBlock{Invoke-Expression -Command "Net use"}
  • PowerShell WMI 和 CimInstance 方法。

還可以使用 PowerShell WMI方法,並使用類名Win32_MappedLogicalDisk來獲取本地計算機上的對映的網路驅動器。

Get-WmiObject -ClassName Win32_MappedLogicalDisk | Select PSComputerName, Name,ProviderName

PSComputerName  Name ProviderName
--------------  ---- ------------
DESKTOP-9435KM9 K:   \localhost\shared folder
DESKTOP-9435KM9 L:   \localhost\Shared

在遠端計算機上獲取相同的內容。

Get-WmiObject -ClassName Win32_MappedLogicalDisk –ComputerName RemoteComputer | Select PSComputerName, Name,ProviderName

使用CIM方法。

Get-CimInstance -ClassName Win32_MappedLogicalDisk | Select SystemName, DeviceID, ProviderName

在遠端系統上。

Get-CimInstance -ClassName Win32_MappedLogicalDisk –ComputerName RemoteSystem | Select SystemName, DeviceID, ProviderName
  • Get-PSDrive 方法。

在本地計算機上,可以執行Get-PSDrive PowerShell cmdlet,但它會獲取所有可用的驅動器。要獲取網路驅動器,我們需要按如下所示篩選輸出。

Get-PSDrive | where{$_.DisplayRoot -match "\"}

輸出

Name           Used (GB)     Free (GB) Provider      Root
----           ---------     --------- --------      ----
K                 318.16         47.24 FileSystem    \localhost\shared folder
M                 286.07         79.36 FileSystem    \localhost\Shared Folder

在遠端計算機上獲取對映的驅動器。

Invoke-Command –ComputerName RemoteComputer -ScriptBlock{Get-PSDrive | where{$_.DisplayRoot -match "\"}}

更新於:2020-11-11

19 萬+ 次瀏覽

開啟您的職業生涯

完成課程後獲得認證

開始
廣告
© . All rights reserved.