Jump to content

Capture Mapped Network drives for a user & output to bat file


Carm01
 Share

Recommended Posts

I needed to make a no frills way to capture logged in users network drive map paths and create a bat file of those paths and place it on the users auto mapped default personal network drive that gets auto-mapped at log-on, and here is what i came up with below. I did see some other snippets on here but was unable to find one with the use of a bat file for user to run; maybe i didn't search right, oh well.

The bat file was a way that the end user could go to another machine and double click and auto map their drives without any of the techno babble from us ..lol

You do not have to be an admin to run this unless the policy is so strict on the machine that it will not allow.

As a note in out environment when running this as an admin didn't not produce any bat file output, I am guessing because it was run under my account as admin is not seeing the users permission drives. But should hopefully provide a viable solution to the people providing desk-side support

#include <File.au3>
$sFilePath = "Z:\_My_Metwork_Drives.cmd" ; file path set to variable
;FileDelete($sFilePath) ; will delete current file if necessary
;sleep(100)
_FileCreate ( $sFilePath ) ; creates your file
Local $sText = ""
For $i = 65 To 90 ; these are asc values for the letters A thrugh Z
    $sText = $sText & Chr($i) ; Or $sText &= Chr($i) can be used as well.
    $b = Chr($i)
    $c = $b & ":"
    $a = DriveMapGet ($c)
    If $a <> "" Then ; states that if there is a value then we are going to write the below line to a bat file and place it on a predetermined location.
    $e = "net use " & Chr($i) & ": " & '"' & $a & '"' & @CRLF ; this sets the code that will be written the line in the bat file
    FileOpen($sFilePath, $FO_APPEND) ; opens file for writing
    FileWrite( $sFilePath, $e ); actually applies the line to the file
    EndIf
next

FileSetAttrib("Z:\_MapMe.cmd", "+R-A"); sets to non archive and read only to prevent accidental modification
FileClose($sFilePath); close file
Link to comment
Share on other sites

Hi Carm01,

I see some issues with your code. The FileOpen in the loop which creates a file write handle 26 times and yet not any of those handles used once or closed at the end of use. I would expect the file write handle to be opened before entering the loop or perhaps when the loop is done. The later would mean storing the commands in a variable while looping. Using _FileCreate() seems a waste, especially including <file.au3>, as FileOpen or FileWrite used by itself creates the file anyhow. Instead of going from a to z and using those functions in the loop 26 times, I would recommend DriveGetDrive to get the network drives then it would probably be just a couple of loops depending on the mapped drive count.

Here is an alternative example

; cmd script to write net use commands
$cmdfile = 'MapDrive.cmd'

; get network drives and store into an array
$network_drives = DriveGetDrive('NETWORK')
; check that the array is valid
If Not IsArray($network_drives) Then
    MsgBox(0x40030, @ScriptName, 'No network drives found')
    Exit 1
Else
    ; open a handle to write to file using erase mode
    $handle_write = FileOpen($cmdfile, 2)
    ; check that the handle is valid
    If $handle_write = -1 Then
        MsgBox(0x40030, @ScriptName, 'Unable to open cmd file for write')
        Exit 2
    Else
        ; loop through each network drive
        For $1 = 1 To UBound($network_drives) -1
            ; get the unc path of the network drive
            $network_path = DriveMapGet($network_drives[$1])
            ; do not write to file if an error occurred
            If @error Then ContinueLoop
            ; write net use command to the cmd script
            FileWrite($handle_write, 'net use ' & $network_drives[$1] & ' "' & $network_path & '"' & @CRLF)
        Next
        ; close the file handle that was used for writing
        FileClose($handle_write)
    EndIf
EndIf

Perhaps some ideas which you can use to update your code. :)

Link to comment
Share on other sites

Hi Carm01,

I see some issues with your code. The FileOpen in the loop which creates a file write handle 26 times and yet not any of those handles used once or closed at the end of use. I would expect the file write handle to be opened before entering the loop or perhaps when the loop is done. The later would mean storing the commands in a variable while looping. Using _FileCreate() seems a waste, especially including <file.au3>, as FileOpen or FileWrite used by itself creates the file anyhow. Instead of going from a to z and using those functions in the loop 26 times, I would recommend DriveGetDrive to get the network drives then it would probably be just a couple of loops depending on the mapped drive count.

Here is an alternative example

; cmd script to write net use commands
$cmdfile = 'MapDrive.cmd'

; get network drives and store into an array
$network_drives = DriveGetDrive('NETWORK')
; check that the array is valid
If Not IsArray($network_drives) Then
    MsgBox(0x40030, @ScriptName, 'No network drives found')
    Exit 1
Else
    ; open a handle to write to file using erase mode
    $handle_write = FileOpen($cmdfile, 2)
    ; check that the handle is valid
    If $handle_write = -1 Then
        MsgBox(0x40030, @ScriptName, 'Unable to open cmd file for write')
        Exit 2
    Else
        ; loop through each network drive
        For $1 = 1 To UBound($network_drives) -1
            ; get the unc path of the network drive
            $network_path = DriveMapGet($network_drives[$1])
            ; do not write to file if an error occurred
            If @error Then ContinueLoop
            ; write net use command to the cmd script
            FileWrite($handle_write, 'net use ' & $network_drives[$1] & ' "' & $network_path & '"' & @CRLF)
        Next
        ; close the file handle that was used for writing
        FileClose($handle_write)
    EndIf
EndIf

Perhaps some ideas which you can use to update your code. :)

I appreciate the feedback

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...