0

I am trying to get network mapped drives using below commands.

Get-WmiObject -Class Win32_MappedLogicalDisk |  %{$_.Name}

Get-WmiObject -Class Win32_MappedLogicalDisk |  %{$_.ProviderName}

This works in some system however does not in other systems(may be powershell version issue) So I thought of using net use command. However, I am unable to fetch the values or not sure how to get the values displays when i type 'net use'

when I type net use I get status, Local, Remote and Network column. I tried to use the below command to get the field values.

net use | select local.

but I get blank or nothing

Used below command.

net use | select local.

Need to get Local and Remote values from net use command.

Jeff Zeitlin
  • 9,773
  • 2
  • 21
  • 33
Arun
  • 5
  • 8
  • 3
    `NET` is not a PowerShell command; it's an external program. As such, it does not pass structured objects into the PowerShell pipe, only text. The `Select-Object` cmdlet expects a structured object, and will return the named member property from that object - in other words, in your example, it's expecting a structured object with a member property called "local.". You will need to parse the text. – Jeff Zeitlin May 14 '19 at 13:29
  • To see everything available, use `Get-PSDrive | Get-Member -Force`. To get only FileSystem "drives," use `Get-PSDrive | Where-Object { $_.Provider.Name -eq 'FileSystem' }`. – lit May 14 '19 at 14:40
  • 1
    Not sure if people missed this comment _This works in some system however does not in other systems(may be powershell version issue)_ so PS might not be the solution and the smells like an XY problem at this point. There is an error or issue we don't see – Matt May 14 '19 at 14:45
  • @lit As per MS documentation on [Managing Windows PowerShell Drives](https://learn.microsoft.com/en-us/powershell/scripting/samples/managing-windows-powershell-drives?view=powershell-7.1) you could use `Get-PSDrive -PSProvider FileSystem` – vvilin Mar 31 '21 at 09:57

3 Answers3

0

How about using get-psdrive (the root header actually matches the displayroot property)?

get-psdrive | where displayroot -like '\\*'

Name           Used (GB)     Free (GB) Provider      Root
----           ---------     --------- --------      ----
Y                  91.84          7.82 FileSystem    \\server....
js2010
  • 23,033
  • 6
  • 64
  • 66
0

Depending on the PowerShell versions available you might encounter similar problems with
Get-SmbMapping which wraps the CimClass: ROOT/Microsoft/Windows/SMB:MSFT_SmbMapping.

But has otherwise an output resembling net use.

To process the real net use output and convert to an object with properties,
you may use:

$SmbMapping = (net use) -like '* \\*' | ForEach-Object { 
    $Status,$Local,$Remote,$Null = $_ -split ' +',4
    [PSCustomObject]@{
        Status = $Status
        Local  = $Local
        Remote = $Remote
    }
}

This works at least in my German locale Win10.
(Not sure about different status messages in other locales.)

0

See this for parsing legacy console output ---

How to Convert Text Output of a Legacy Console Application to PowerShell Objects

Yet, along with what LotPings gave you already. Your query could be a duplicate of this ... Equivalent of net use (to list computer's connections) in powershell? ... and it's accepted answer

# For the mapped logical drive you can use WMI class Win32_MappedLogicalDisk :

Get-WmiObject Win32_MappedLogicalDisk

# Here is another way with Win32_LogicalDisk :

PS C:\> Get-WmiObject -Query "Select * From Win32_LogicalDisk Where DriveType = 4"

DeviceID     : V:
DriveType    : 4
ProviderName : \\jpbdellf1\c$
FreeSpace    :
Size         :
VolumeName   :

# Edited
# You are right, you can get what you need with Win32_NetworkConnection :
Get-WmiObject Win32_NetworkConnection

LocalName                     RemoteName                    ConnectionState               Status
---------                     ----------                    ---------------               ------
                              \\jpbasusf1\temp              Connected                     OK

# On Seven or W2K8 be careful to call this with the same user that run the NET USE because it's a session information.
postanote
  • 15,138
  • 2
  • 14
  • 25