1

We are in the process of migrating two file servers to a new server. We have mapped drives via user group in group policy. Many users have manually mapped drives and we need to find these mappings. I have created a PowerShell script to run that remotely get the drive mappings. It works on most computers but there are many that are not returning results and I am not getting any error messages. Each workstation on the list creates a text file and the ones that are not returning results have no text in the files. I can ping these machines. If the machine is not turned on, it does come up error message that the RPC server is not available. My domain user account is in a group that is in the local admin account. I have no idea why some are not working.

Here is the script.

# Load list into variable, which will become an array of strings

If( !(Test-Path C:\Scripts)) { New-Item C:\Scripts -ItemType directory }
If( !(Test-Path C:\Scripts\Computers)) { New-Item C:\Scripts\Computers -ItemType directory }
If( !(Test-Path C:\Scripts\Workstations.txt)) { "No Workstations found. Please enter a list of Workstations under Workstation.txt"; Return}
If( !(Test-Path C:\Scripts\KnownMaps.txt)) { "No Mapping to check against. Please enter a list of Known Mappings under KnownMaps.txt"; Return}

$computerlist = Get-Content C:\Scripts\Workstations.txt

# Loop through each item in the array (each computer in the list of computers we loaded into the variable)
ForEach ($computer in $computerlist)
{

   $diskObject =  Get-WmiObject Win32_MappedLogicalDisk -computerName $computer | Select Name,ProviderName | Out-File C:\Tester\Computers\$computer.txt -width 200

}

Select-String -Path C:\Tester\Computers\*.txt -Pattern cmsfiles | Out-File C:\Tester\Drivemaps-all.txt

$strings = Get-Content C:\Tester\KnownMaps.txt

Select-String -Path C:\Tester\Drivemaps-all.txt -Pattern $strings -notmatch -simplematch | Out-File C:\Tester\Drivemaps-nonmatch.txt -Width 200
Select-String -Path C:\Tester\Drivemaps-all.txt -Pattern $strings -simplematch | Out-File C:\Tester\Drivemaps-match.txt -Width 200

2 Answers 2

3

It appears from some quick testing that Get-WmiObject Win32_MappedLogicalDisk -computerName $computer has some limitations. From what I'm reading, whether the current user has Administrative privileges to their computer can determine whether that returns anything or not.

Another option may be to go through the registry. This command lists the mapped drives for the current user, but you'd have to use Invoke-Command in order to execute it remotely.

Invoke-Command -ComputerName $computer -ScriptBlock {Get-ChildItem HKCU:Network}

If you need to get the drive mappings used by all users this appears to work in my testing:

# Connect to registry on remote computer and pull a list of user keys
$computer="remotecomputer"
$Reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('Users', $computer)
$UserKeys = $Reg.GetSubKeyNames()

# Loop through the user keys and access the Network subkey, which contains the user's drive mappings
for ($i=0; $i -lt $Reg.SubKeyCount; $i++) {
    $NetKey = $Reg.OpenSubKey("$($UserKeys[$i])\\Network")

# If the user has any mapped drives, get the subkeys containing those drives
    if ($NetKey.SubKeyCount -gt 0) {
        $DriveKeys = $NetKey.GetSubKeyNames()
        for ($n=0; $n -lt $DriveKeys.Length; $n++) {
            $DriveKey = $Reg.OpenSubKey("$($UserKeys[$i])\\Network\\$($DriveKeys[$n])")

# Output the drive letter and the network path
        "Drive Letter: " + $DriveKeys[$n]
        "Network Path: " + $DriveKey.GetValue("RemotePath")

# Close each of the loops and conditions
        }
    }
}
0
0

We use this script below at logon. Specified in Group Policy.

Option Explicit

Dim objFSO, objLogFile, objNetwork, objShell, strText, intAns
Dim intConstants, intTimeout, strTitle, intCount, blnLog
Dim strUserName, strComputerName, strShare, strLogFile
Dim objDrives, strDrive, strPath, k, strMessage
Dim objPrinters, strPrinter, strPrinterPath

strShare = "\\BCSRV007\Shared"
strLogFile = "Domain.log"
intTimeout = 20

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objNetwork = CreateObject("Wscript.Network")
Set objShell = CreateObject("Wscript.Shell")

strUserName = objNetwork.UserName
strComputerName = objNetwork.ComputerName

strMessage = "Logon at: " & CStr(Now()) _
    & vbCrLf & "  User: " & strUserName _
    & vbCrLf & "  Computer: " & strComputerName

' Enumerate mapped drives.
Set objDrives = objNetwork.EnumNetworkDrives
For k = 0 To objDrives.Count - 2 Step 2
    strDrive = objDrives(k)
    strPath = objDrives(k + 1)
    If (strDrive = "") Then
        strDrive  = "--"
    End If
    strMessage = strMessage _
        & vbCrLf & "  Drive " & strDrive & " mapped to " & strPath
Next

' Enumerate printers.
Set objPrinters = objNetwork.EnumPrinterConnections
For k = 0 To objPrinters.Count - 2 Step 2
    strPrinter = objPrinters(k)
    strPrinterPath = objPrinters(k + 1)
    If (strPrinter = "") Then
        strPrinter = "--"
    End If
    strMessage = strMessage _
        & vbCrLf & "  Printer " & strPrinter & " mapped to " & strPrinterPath
Next

' Log date/time, user name, computer name, mapped drives, and
' mapped printers.
If (objFSO.FolderExists(strShare) = True) Then
    On Error Resume Next
    Set objLogFile = objFSO.OpenTextFile(strShare & "\" _
        & strLogFile, 8, True, 0)
    If (Err.Number = 0) Then
        ' Make three attempts to write to log file.
        intCount = 1
        blnLog = False
        Do Until intCount = 3
            objLogFile.WriteLine strMessage
            If (Err.Number = 0) Then
                intCount = 3
                blnLog = True
            Else
                Err.Clear
                intCount = intCount + 1
                If (Wscript.Version > 5) Then
                    Wscript.Sleep 200
                End If
            End If
        Loop
        On Error GoTo 0
        If (blnLog = False) Then
            strTitle = "Logon Error"
            strText = "Log cannot be written."
            strText = strText & vbCrlf _
                & "Another process may have log file open."
            intConstants = vbOKOnly + vbExclamation
            intAns = objShell.Popup(strText, intTimeout, strTitle, _
                intConstants)
        End If
        objLogFile.Close
    Else
        On Error GoTo 0
        strTitle = "Logon Error"
        strText = "Log cannot be written."
        strText = strText & vbCrLf & "User may not have permissions,"
        strText = strText & vbCrLf & "or log folder may not be shared."
        intConstants = vbOKOnly + vbExclamation
        intAns = objShell.Popup(strText, intTimeout, strTitle, intConstants)
    End If
    Set objLogFile = Nothing
End If

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .