Get Mapped Drives using .net

Ask your 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.
This topic is 6 years and 3 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
MarvelManiac
Posts: 63
Last visit: Thu Sep 13, 2018 3:40 pm

Get Mapped Drives using .net

Post by MarvelManiac »

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)
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Get Mapped Drives using .net

Post by jvierra »

You cannot get drives from a remote registry. If you use the SID the user must be logged in.
User avatar
MarvelManiac
Posts: 63
Last visit: Thu Sep 13, 2018 3:40 pm

Re: Get Mapped Drives using .net

Post by MarvelManiac »

Yup, the user will be logged in.

figured using remote registry would be much faster than the other processes.
User avatar
MarvelManiac
Posts: 63
Last visit: Thu Sep 13, 2018 3:40 pm

Re: Get Mapped Drives using .net

Post by MarvelManiac »

jvierra wrote: Tue Jan 09, 2018 8: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?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Get Mapped Drives using .net

Post by jvierra »

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: 63
Last visit: Thu Sep 13, 2018 3:40 pm

Re: Get Mapped Drives using .net

Post by MarvelManiac »

jvierra wrote: Wed Jan 10, 2018 1: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.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Get Mapped Drives using .net

Post by jvierra »

I repeat ...

What is it you are looking for a solution to? What is the purpose of this?
User avatar
MarvelManiac
Posts: 63
Last visit: Thu Sep 13, 2018 3:40 pm

Re: Get Mapped Drives using .net

Post by MarvelManiac »

jvierra wrote: Wed Jan 10, 2018 1: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
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Get Mapped Drives using .net

Post by jvierra »

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: 63
Last visit: Thu Sep 13, 2018 3:40 pm

Re: Get Mapped Drives using .net

Post by MarvelManiac »

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. $regkey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey([Microsoft.Win32.RegistryHive]::Users, $Computer)
  3. $ref = $regKey.OpenSubKey("S-1-5-21-XXXXXXX\Network");
  4. $keys = $ref.GetSubKeyNames()
  5. $results = foreach ($key in $keys)
  6. {
  7.    $ref2 = $regKey.OpenSubKey("S-1-5-21-XXXXXXX\Network\$key");
  8.    "$key $($ref2.GetValue("RemotePath"))"
  9. }  
  10.  
  11. $results
I'd like to return something like this
mappeddrivesdgv.png
mappeddrivesdgv.png (5.38 KiB) Viewed 3687 times
but I'm stuck. I might just have to put it in a textbox
Last edited by MarvelManiac on Wed Jan 10, 2018 3:19 pm, edited 1 time in total.
This topic is 6 years and 3 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked