Get Mapped Drives using .net

Ask your Windows PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
User avatar
MarvelManiac
Posts: 44
Joined: Mon May 01, 2017 8:28 pm

Get Mapped Drives using .net

Post by MarvelManiac » Tue Jan 09, 2018 9:10 pm

I have found a script online that does this beautifully

https://social.technet.microsoft.com/Fo ... 358ef139e7

2 downfalls. It will not retrieve the drives while my techs are working VPN, and it takes about 10 seconds to return the results

Is there a way I can use this method if I know the SID?

$computers = 'Server1'
$key = 'S-1-5-21-XXXXXXXXX'
$valuename = 'Network'
[Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('Users', $computer)

User avatar
jvierra
Posts: 12096
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Get Mapped Drives using .net

Post by jvierra » Tue Jan 09, 2018 9:17 pm

You cannot get drives from a remote registry. If you use the SID the user must be logged in.

User avatar
MarvelManiac
Posts: 44
Joined: Mon May 01, 2017 8:28 pm

Re: Get Mapped Drives using .net

Post by MarvelManiac » Wed Jan 10, 2018 8:41 am

Yup, the user will be logged in.

figured using remote registry would be much faster than the other processes.

User avatar
MarvelManiac
Posts: 44
Joined: Mon May 01, 2017 8:28 pm

Re: Get Mapped Drives using .net

Post by MarvelManiac » Wed Jan 10, 2018 2:30 pm

jvierra wrote:
Tue Jan 09, 2018 9:17 pm
You cannot get drives from a remote registry. If you use the SID the user must be logged in.
Do you know of a quicker solution?

User avatar
jvierra
Posts: 12096
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Get Mapped Drives using .net

Post by jvierra » Wed Jan 10, 2018 2:39 pm

What is it you are looking for a solution to? What is the purpose of this?

If a user connects over a VPN they are not logged into a remote system. All drives are mapped locally over the VPN. Get-PsDrive is all you need.

User avatar
MarvelManiac
Posts: 44
Joined: Mon May 01, 2017 8:28 pm

Re: Get Mapped Drives using .net

Post by MarvelManiac » Wed Jan 10, 2018 2:50 pm

jvierra wrote:
Wed Jan 10, 2018 2:39 pm
What is it you are looking for a solution to? What is the purpose of this?

If a user connects over a VPN they are not logged into a remote system. All drives are mapped locally over the VPN. Get-PsDrive is all you need.
Trying to return the Drive and Path of a logged on users network drives from HKEY_USERS using remote registry

I have found using remote registry is much quicker than powershell commands


Ignore VPN.

User avatar
jvierra
Posts: 12096
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Get Mapped Drives using .net

Post by jvierra » Wed Jan 10, 2018 2:57 pm

I repeat ...

What is it you are looking for a solution to? What is the purpose of this?

User avatar
MarvelManiac
Posts: 44
Joined: Mon May 01, 2017 8:28 pm

Re: Get Mapped Drives using .net

Post by MarvelManiac » Wed Jan 10, 2018 3:01 pm

jvierra wrote:
Wed Jan 10, 2018 2:57 pm
I repeat ...

What is it you are looking for a solution to? What is the purpose of this?
Show technicians the drives and the paths so our build guys can map them

Yes, I can export the registry but that will map drives they may no longer have access to.. So in our form if I put

Z: \\server1\accounting
Y: \\server1\records

The build team actually maps these and can tell us they no longer have access.

I'm trying to prevent moving bad data from one PC to another

User avatar
jvierra
Posts: 12096
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Get Mapped Drives using .net

Post by jvierra » Wed Jan 10, 2018 3:08 pm

What drivers and data are you looking for in the registry? There are no drivers associated with mapped drives. You seem to use "drivers" a nd "drives" interchangeably. They are not the same.

Your question is much too vague to even guess at an answer.

User avatar
MarvelManiac
Posts: 44
Joined: Mon May 01, 2017 8:28 pm

Re: Get Mapped Drives using .net

Post by MarvelManiac » Wed Jan 10, 2018 3:20 pm

Hmm, not sure where you see I typed Drivers but I did come to this solution
So it's working in the ISE but it only returns the length in a DGV
  1. $computer = 'Server1'
  2.  
  3. $regkey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey([Microsoft.Win32.RegistryHive]::Users, $Computer)
  4.  
  5. $ref = $regKey.OpenSubKey("S-1-5-21-XXXXXXX\Network");
  6.  
  7. $keys = $ref.GetSubKeyNames()
  8.  
  9. $results = foreach ($key in $keys)
  10.  
  11. {
  12.  
  13.    $ref2 = $regKey.OpenSubKey("S-1-5-21-XXXXXXX\Network\$key");
  14.  
  15.    "$key $($ref2.GetValue("RemotePath"))"
  16.  
  17. }  
  18.  
  19.  
  20.  
  21. $results
I'd like to return something like this
mappeddrivesdgv.png
mappeddrivesdgv.png (5.38 KiB) Viewed 682 times
but I'm stuck. I might just have to put it in a textbox
Last edited by MarvelManiac on Wed Jan 10, 2018 4:19 pm, edited 1 time in total.

Locked