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 2 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
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 still am lost. We determined you needed to use the SID of a logged on user. That is what you have done now. My first response noted that but your response confused me since you didn't seem to like the answer.

Glad you are set now. Good luck.
User avatar
MarvelManiac
Posts: 63
Last visit: Thu Sep 13, 2018 3:40 pm

Re: Get Mapped Drives using .net

Post by MarvelManiac »

I think you determined I needed the SID but I never stated that my friend

I just wanted to create a faster script than what I linked to using remote registry

Just hung up on creating a data set to import into my DGV

Thanks for the attempt. Sorry I didn't make it clear enough for you.
User avatar
MarvelManiac
Posts: 63
Last visit: Thu Sep 13, 2018 3:40 pm

Re: Get Mapped Drives using .net

Post by MarvelManiac »

This is my completed script
  1. $script:computer = 'Server1'
  2.  
  3. $script:user = Invoke-Command -ComputerName $computer -ScriptBlock {
  4. (Get-ChildItem  C:\users | Where { $_.Name -ne $env:USERNAME }).name | Select-Object -last 1
  5.  
  6. }
  7.  
  8. $objUser = New-Object System.Security.Principal.NTAccount("$user")
  9. $strSID = $objUser.Translate([System.Security.Principal.SecurityIdentifier])
  10. $script:sid = $strSID.Value
  11.  
  12. $regkey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey([Microsoft.Win32.RegistryHive]::Users, $Computer)
  13. $ref = $regKey.OpenSubKey("$sid\Network")
  14. $keys = $ref.GetSubKeyNames()
  15.  
  16. $results = foreach($key in $keys){
  17.     $ref2 = $regKey.OpenSubKey("$sid\Network\$key")
  18.     [pscustomobject]@{
  19.         Drive = $key
  20.         Path = $ref2.GetValue('RemotePath')
  21.     }
  22. }
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 »

Very nice.
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: Thu Jan 11, 2018 9:36 amVery nice.
Thanks, I'm extremely happy with my results. I really appreciate your time.
This topic is 6 years and 2 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