Share via


Check network drive connection

Question

Wednesday, March 14, 2012 10:57 AM

Hi all,

I need a power shell script to programmatically check if a given workstation has mapped drives using a certain user. I know that mapped network drive settings are stored into HKCU\Network\mapped drive letter} But I don't know if the mughty power shell offers some other way to accomplish this task.

All replies (13)

Thursday, March 15, 2012 7:30 AM ✅Answered

Hi,

It seems like that if we mapped a drive to the computer, then we could achieve all mapped drives by Gwmi -computername.

But if we map drives to users, them we should find out those drives based on reading the key: HKCU:\Network\

I would like suggest you refer to the below link to use Remote Registry PowerShell Module:

http://blogs.microsoft.co.il/blogs/scriptfanatic/archive/2010/01/10/remote-registry-powershell-module.aspx

In addition, here is a VBscript for you, we could also use it with powershell:

http://gallery.technet.microsoft.com/scriptcenter/3dd6af3e-edfa-4581-bc35-805314f26bb8

Hope this helps.

Best Regards,

Yan Li

Yan Li

TechNet Community Support


Wednesday, March 14, 2012 11:06 AM

Get-ItemProperty "HKCU:\Network\"


Wednesday, March 14, 2012 11:28 AM

I need to check a remote computer


Wednesday, March 14, 2012 11:40 AM

I need to check a remote computer

Is computer in a domain and is a logged  a domain user?


Wednesday, March 14, 2012 1:17 PM

yes is a computer member of a domain with a domain user logged on


Wednesday, March 14, 2012 2:55 PM

$pc = "pc"
$user = Get-WmiObject Win32_ComputerSystem -ComputerName $pc
$sid = ([Security.Principal.NTAccount]$user.UserName).Translate([Security.Principal.SecurityIdentifier]).Value
$reg = [wmiclass]"\\$pc\root\default:stdregprov"
$reg.EnumKey(2147483651,"$sid\Network").sNames

Wednesday, March 14, 2012 4:51 PM

I'm trying to build a powershell script that enumerate mapped network drives on a remote machine. I've to browse the remote registry HKEY_USERS and, if exists, I've to loop into the "Network" subkey here is my script:

$srv='pc_to_scan'
$type = [Microsoft.Win32.RegistryHive]::Users
$regKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey($type, $Srv)
$key = "Network"
foreach ($valueName in $regKey.GetSubKeyNames())
{
    $regKey2 = $regKey.OpenSubKey($key)
    foreach ($valueName2 in $regKey2.GetSubKeyNames())
    {
        echo $valueName2
    }
}
 

but the script fails telling:

You cannot call a method on a null-valued expression.
At line:9 char:52

  •     foreach ($valueName2 in $regKey2.GetSubKeyNames <<<< ())
        + CategoryInfo          : InvalidOperation: (GetSubKeyNames:String) [], RuntimeException
        + FullyQualifiedErrorId : InvokeMethodOnNull

Wednesday, March 14, 2012 5:03 PM

 

gwmi win32_logicaldisk -Filter "drivetype='4'"

 

tack on the -computername and it works remotely

 

Justin Rich
http://jrich523.wordpress.com
PowerShell V3 Guide (Technet)
Please remember to mark the replies as answers if they help and unmark them if they provide no help.


Wednesday, March 14, 2012 5:58 PM

Andrea,

The following will work if Powershell Remoting is enabled on the remote machines ( Enable-PSRemoting )

Function Get-MapDriveUsername {             
             
      Param(            
        $computer = $env:COMPUTERNAME,            
        $reg = "hkcu:\network",            
        $username = "Contoso\SP_Admin"            
        )            
             
       $computer | %{            
           $drives = ICM -ComputerName $_ {             
              Param($reg)             
              dir $reg            
                } -ArgumentList $reg             
           $comp = $_            
          $obj = New-Object psobject             
          if($drives -ne $null) {            
             $drives | %{            
             $driveletter = $_.name.split("\")[-1]            
             foreach($i in $driveletter) {            
                $drive = (Get-ItemProperty (Join-Path $reg $i))            
                if($drive.username -like $username) {            
                    $obj | Add-Member Noteproperty Computer $comp            
                    $obj | Add-Member Noteproperty Drive $drive.PSChildName            
                    $obj | Add-Member NoteProperty Path $drive.RemotePath            
      }            
     }            
    }            
   }            
  }            
   Write-Output $obj            
 }

Joe


Wednesday, March 14, 2012 9:02 PM

Andrea,

The following will work if Powershell Remoting is enabled on the remote machines ( Enable-PSRemoting )

Function Get-MapDriveUsername {             
             
      Param(            
        $computer = $env:COMPUTERNAME,            
        $reg = "hkcu:\network",            
        $username = "Contoso\SP_Admin"            
        )            
             
       $computer | %{            
           $drives = ICM -ComputerName $_ {             
              Param($reg)             
              dir $reg            
                } -ArgumentList $reg             
           $comp = $_            
          $obj = New-Object psobject             
          if($drives -ne $null) {            
             $drives | %{            
             $driveletter = $_.name.split("\")[-1]            
             foreach($i in $driveletter) {            
                $drive = (Get-ItemProperty (Join-Path $reg $i))            
                if($drive.username -like $username) {            
                    $obj | Add-Member Noteproperty Computer $comp            
                    $obj | Add-Member Noteproperty Drive $drive.PSChildName            
                    $obj | Add-Member NoteProperty Path $drive.RemotePath            
      }            
     }            
    }            
   }            
  }            
   Write-Output $obj            
 }

Joe

This script will not work,because when will run by another credential.


Thursday, March 15, 2012 1:26 AM

Kazun,

Not sure I understand....what other credentials are needed?

Joe


Thursday, March 15, 2012 1:54 AM

Following command should work for you:

Gwmi Win32_LogicalDisk -filter "DriveType = 4" -Comp $strComputer

Hope this helps...!!!

Thanks & Regards
Bhavik Solanki

Please click “Mark as Answer” if this post answers your question and click "Vote as Helpful if this Post helps you.


Thursday, March 15, 2012 4:53 AM

Hey,

This seems to be duplicate thread, if I understood correctly. Check this out:

http://social.technet.microsoft.com/Forums/en-US/winserverpowershell/thread/505ce71c-7cde-49e3-a626-3e5b16873c3f

Thanks & Regards
Bhavik Solanki

Please click “Mark as Answer” if this post answers your question and click "Vote as Helpful if this Post helps you.