如何使用 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.