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
jvierra
Posts: 12399
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Get Mapped Drives using .net

Post by jvierra » Wed Jan 10, 2018 4:18 pm

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: 48
Joined: Mon May 01, 2017 8:28 pm

Re: Get Mapped Drives using .net

Post by MarvelManiac » Wed Jan 10, 2018 5:07 pm

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: 48
Joined: Mon May 01, 2017 8:28 pm

Re: Get Mapped Drives using .net

Post by MarvelManiac » Thu Jan 11, 2018 9:39 am

This is my completed script
  1. $script:computer = 'Server1'
  2.  
  3.  
  4.  
  5. $script:user = Invoke-Command -ComputerName $computer -ScriptBlock {
  6.  
  7. (Get-ChildItem  C:\users | Where { $_.Name -ne $env:USERNAME }).name | Select-Object -last 1
  8.  
  9.  
  10.  
  11. }
  12.  
  13.  
  14.  
  15. $objUser = New-Object System.Security.Principal.NTAccount("$user")
  16.  
  17. $strSID = $objUser.Translate([System.Security.Principal.SecurityIdentifier])
  18.  
  19. $script:sid = $strSID.Value
  20.  
  21.  
  22.  
  23. $regkey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey([Microsoft.Win32.RegistryHive]::Users, $Computer)
  24.  
  25. $ref = $regKey.OpenSubKey("$sid\Network")
  26.  
  27. $keys = $ref.GetSubKeyNames()
  28.  
  29.  
  30.  
  31. $results = foreach($key in $keys){
  32.  
  33.     $ref2 = $regKey.OpenSubKey("$sid\Network\$key")
  34.  
  35.     [pscustomobject]@{
  36.  
  37.         Drive = $key
  38.  
  39.         Path = $ref2.GetValue('RemotePath')
  40.  
  41.     }
  42.  
  43. }

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

Re: Get Mapped Drives using .net

Post by jvierra » Thu Jan 11, 2018 10:36 am

Very nice.

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

Re: Get Mapped Drives using .net

Post by MarvelManiac » Thu Jan 11, 2018 11:25 am

jvierra wrote:
Thu Jan 11, 2018 10:36 am
Very nice.
Thanks, I'm extremely happy with my results. I really appreciate your time.

Locked