none
Check network drive connection

    שאלה

  • 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.

    יום רביעי 14 מרץ 2012 10:57

תשובות

כל התגובות

  • Get-ItemProperty "HKCU:\Network\*"
    יום רביעי 14 מרץ 2012 11:06
  • I need to check a remote computer
    יום רביעי 14 מרץ 2012 11:28
  • I need to check a remote computer
    Is computer in a domain and is a logged  a domain user?
    יום רביעי 14 מרץ 2012 11:40
  • yes is a computer member of a domain with a domain user logged on
    יום רביעי 14 מרץ 2012 13:17
  • $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

    יום רביעי 14 מרץ 2012 14:55
  • 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

    יום רביעי 14 מרץ 2012 16:51
  •  
    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.
    יום רביעי 14 מרץ 2012 17:03
  • 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
    יום רביעי 14 מרץ 2012 17:58
  • 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.
    יום רביעי 14 מרץ 2012 21:02
  • Kazun,

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

    Joe

    יום חמישי 15 מרץ 2012 01:26
  • 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.

    יום חמישי 15 מרץ 2012 01:54
  • 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.

    יום חמישי 15 מרץ 2012 04:53
  • 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

    יום חמישי 15 מרץ 2012 07:30
    מנחה דיון