topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • Friday March 29, 2024, 12:15 am
  • Proudly celebrating 15+ years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Author Topic: Simple network drivemapping utility  (Read 4872 times)

questorfla

  • Supporting Member
  • Joined in 2012
  • **
  • Posts: 570
  • Fighting Slime all the Time
    • View Profile
    • Donate to Member
Simple network drivemapping utility
« on: July 29, 2015, 02:56 PM »
I can probably do this if i get q few minutes of peace and quite but i thought someone may already have one.  I am looking for the method of making this  look like a real program not just a line of text?  I think i have seen a shell generator script that is the standard for most of this small utilities.  I just need them to input their name.  The rest can be done inside the script and i hated to resort to a dos box of a batch file.
« Last Edit: July 29, 2015, 03:49 PM by questorfla, Reason: finshed »

x16wda

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 888
  • what am I doing in this handbasket?
    • View Profile
    • Read more about this member.
    • Donate to Member
Re: Simple network drivemapping utility
« Reply #1 on: July 29, 2015, 08:51 PM »
Batch is great for a lot of things. You could use some of these utilities from Horst Shaeffer to tart up a batch file nicely enough, winput.exe can ask prettily for some input from your users.
vi vi vi - editor of the beast

4wd

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 5,641
    • View Profile
    • Donate to Member
Re: Simple network drivemapping utility
« Reply #2 on: July 29, 2015, 10:37 PM »
DriveMapper ?

2015-07-30 13_32_45.png

So basically the same as that but the user only inputs a user name.

Password, network path, and mapped drive letters are static values.

questorfla

  • Supporting Member
  • Joined in 2012
  • **
  • Posts: 570
  • Fighting Slime all the Time
    • View Profile
    • Donate to Member
Re: Simple network drivemapping utility
« Reply #3 on: July 30, 2015, 09:03 AM »
As usual yu da man!

Thanks exactly what I was looking for
Do you happen to know where I can get the "kit" for building these small neat looking simple "apps in a box"?
it is the layout shell that is a pain to make.

ah. I just read lower.
X16Wda.
Maybe that is what you sent in so thanks to you both!
4wd's will  get me by as it is exactly what I wanted.
but the shell for making my own would be another big WIN.
Many of these scripts I have played with (mostly from You 4WD!) are kind of "off-putting" to normal users when they have to enter data into a black DOS window.

4wd

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 5,641
    • View Profile
    • Donate to Member
Re: Simple network drivemapping utility
« Reply #4 on: July 30, 2015, 07:39 PM »
I can do this in Powershell if you want, simple GUI with a textbox for a user name and a Map button.

eg.

2015-07-31 10_36_17.png

That's the GUI done, but no code behind it.

Give me a few days, (busy weekend).

4wd

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 5,641
    • View Profile
    • Donate to Member
Re: Simple network drivemapping utility
« Reply #5 on: September 15, 2015, 11:48 PM »
UPDATED:
  • Displays error message from NET command
  • Can skip mappings by setting map# to empty string in .ini
  • Clears an error message if no password was entered but then is entered (didn't before)


OK, a little Powershell implementation that's specific to the original request:

MapDrive:

2015-09-16 14_20_33.pngSimple network drivemapping utility

Result:
2015-09-16 14_20_08.pngSimple network drivemapping utility

If there's an error:
2015-09-20 14_12_30.pngSimple network drivemapping utility

INSTALLATION:
Extract the archive to a folder.
Edit the MapDrives.ini for your drive mappings, the format is:

map#=<drive letter with :  eg. E:>
unc#=\\server\path

Where # = zero based numbering for each pair of map/unc items

eg.

map0=
unc0=
map1=
unc1=
map2=
unc2=
etc, etc

See the included MapDrives.ini for an example.

NOTE: The first occurance of map/unc is special in that the username is added to the UNC path for the complete path, ie.

If the Username used for logging on is fred and the .ini contains:

map0=Y:
unc0=\\server01\users

Then drive Y: will be mapped to \\server01\users\fred

If you don't want this function, leave the map# parameter empty in the .ini file - leaving it empty will cause the script to skip that mapping entry, eg.

map0=
unc0=\\server01\users


It relies on the net.exe command for the drive mappings so Admin privileges might be required, (don't know - I only have single user Admin systems).

EXECUTION:
Double-click the included shortcut icon.

NOTE: It will remove ALL current drive mappings before creating the new ones.


DISCLAIMER: It works for me.


MapDrives.ps1
Code: PowerShell [Select]
  1. If($PSVersionTable.PSVersion.Major -lt 3) {
  2.   (new-object -ComObject wscript.shell).Popup("Script requires Powershell V3",0,"OK")
  3.   Exit
  4. }
  5.  
  6. # Read MapDrives.ini file
  7. Get-Content "MapDrives.ini" | foreach-object -begin {$h=@{}} -process { $k = [regex]::split($_,'='); `
  8.   if(($k[0].CompareTo("") -ne 0) -and ($k[0].StartsWith("[") -ne $True)) { $h.Add($k[0], $k[1]) } }
  9.  
  10. Function Load-Dialog {
  11.   Param(
  12.     [Parameter(Mandatory=$True,Position=1)]
  13.     [string]$XamlPath
  14.   )
  15.  
  16.   [xml]$Global:xmlWPF = Get-Content -Path $XamlPath
  17.  
  18.   #Add WPF and Windows Forms assemblies
  19.   try{
  20.     Add-Type -AssemblyName PresentationCore,PresentationFramework,WindowsBase,system.windows.forms
  21.   } catch {
  22.     Throw "Failed to load Windows Presentation Framework assemblies."
  23.   }
  24.  
  25.   #Create the XAML reader using a new XML node reader
  26.   $Global:xamGUI = [Windows.Markup.XamlReader]::Load((new-object System.Xml.XmlNodeReader $xmlWPF))
  27.  
  28.   #Create hooks to each named object in the XAML
  29.   $xmlWPF.SelectNodes("//*[@Name]") | %{
  30.     Set-Variable -Name ($_.Name) -Value $xamGUI.FindName($_.Name) -Scope Global
  31.   }
  32. }
  33.  
  34. Function Remove-Maps {
  35. # Get all currently mapped drives
  36.   $maps = (New-Object -Com WScript.Network).EnumNetworkDrives()
  37.   $maps.Count()
  38.   if ($maps.Count() -gt 0) {
  39.     $Tb_Output.Text = "Remove mapped drives: "
  40. # Remove all current mappings
  41.     for ($i = 0; $i -lt $maps.Count() - 1; $i = $i + 2) {
  42.       (New-Object -Com WScript.Network).RemoveNetworkDrive($maps.Item($i))
  43.     }
  44.     $Tb_Output.AppendText("Done`r")
  45.   } else {
  46.     $Tb_Output.Text = "No existing mapped drives`r"
  47.   }
  48. }
  49.  
  50. Function Map-Drives {
  51.   for ($i = 0; $i -lt [Math]::Truncate($h.Count / 2); $i++) {
  52.     $t1 = "map" + $i
  53.     $t2 = "unc" + $i
  54.     if ($h.Get_Item($t1) -ne "") {
  55.       if ($i -eq 0) {
  56.         $root = $h.Get_Item($t2) + "\" + $user
  57.       } else {
  58.         $root = $h.Get_Item($t2)
  59.       }
  60.       $Tb_Output.AppendText("Mapping " + $h.Get_Item($t1))
  61.  
  62.       $cmd = "use " + $h.Get_Item($t1) + " " + $root + " " + $pass + " /user:" + $user
  63.       $errfile = $env:TEMP + "\MapDrive.txt"
  64.       $result = Start-Process -FilePath "net.exe" -ArgumentList $cmd -Wait -PassThru -WindowStyle Hidden -RedirectStandardError $errfile
  65.       if ($result.ExitCode -ne 0) {
  66.         Write-Host $result.StandardError
  67.         Write-Host $result.StandardOutput
  68.         $Tb_Output.AppendText(" Failed`r")
  69.         $errtxt = Get-Content -Path $errfile
  70.         $Tb_Output.AppendText("[" + $errtxt[2] + "]`r")
  71.         Remove-Item $errfile -Force
  72.       } else {
  73.         $Tb_Output.AppendText(" Done`r")
  74.       }
  75.     }
  76.   }
  77. }
  78.  
  79. #Required to load the XAML form and create the PowerShell Variables
  80. Load-Dialog -XamlPath '.\MapDrives.xaml'
  81.  
  82. #EVENT Handlers
  83. $Bt_Map.add_Click({
  84.   $Tb_Output.Clear()
  85.   $global:user = ""
  86.   $global:pass = ""
  87.   if ($Tb_Username.Text -ne "") { $user = $Tb_Username.Text }
  88.   if ($Pb_Password.Password -ne "") { $pass = $Pb_Password.Password }
  89.   if (($user.Length -ne 0) -and ($pass.Length -ne 0)) {
  90.     Remove-Maps
  91.     Start-Sleep -Milliseconds 500
  92.     Map-Drives
  93.     $Tb_Output.AppendText("Complete`r")
  94.   } else {
  95.     $Tb_Output.Text = ("Missing Username or Password`r")
  96.   }
  97. })
  98.  
  99. $Tb_Username.Add_TextChanged({
  100.   $Tb_Output.Clear()
  101. })
  102.  
  103. $Pb_Password.Add_KeyDown({
  104.   $Tb_Output.Clear()
  105. })
  106.  
  107. #Launch the window
  108. $xamGUI.ShowDialog() | out-null

MapDrives.ini
Code: Text [Select]
  1. [General]
  2. map0=R:
  3. unc0=\\10.0.50.1\users
  4. map1=Y:
  5. unc1=\\10.0.50.1\Data
  6. map2=X:
  7. unc2=\\10.1.51.12\itsme
  8. map3=
  9. unc3=\\server01\clients

MapDrives.xaml (the interface definition)
Code: Text [Select]
  1. <Window
  2.   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3.   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4.   Title="MapDrives" Height="217" Width="354" ResizeMode="NoResize">
  5.   <Grid>
  6.     <TextBox Name="Tb_Username" HorizontalAlignment="Left" Height="23" Margin="87,17,0,0" VerticalAlignment="Top" Width="140" ToolTip="Enter your username" MaxLines="1"/>
  7.     <Label Name="Lb_Username" Content="Username:" Margin="14,14,0,0" VerticalAlignment="Top" HorizontalAlignment="Left" Width="68"/>
  8.     <Label Name="Lb_password" Content="Password:" HorizontalAlignment="Left" Margin="14,42,0,0" VerticalAlignment="Top" Width="68"/>
  9.     <PasswordBox Name="Pb_Password" ToolTip="Enter your password" HorizontalAlignment="Left" Margin="87,45,0,0" VerticalAlignment="Top" Width="140" Height="23"/>
  10.     <Button Name="Bt_Map" Content="Map Drives" HorizontalAlignment="Left" Margin="232,30,0,0" VerticalAlignment="Top" Width="75"/>
  11.     <TextBox Name="Tb_Output" HorizontalAlignment="Left" Height="101" Margin="87,73,0,0" VerticalAlignment="Top" Width="240" IsReadOnly="True" IsUndoEnabled="False" MaxLines="20" TextWrapping="Wrap" VerticalScrollBarVisibility="Auto"/>
  12.     <Label Name="Lb_Output" Content="Output:" HorizontalAlignment="Left" Margin="14,109,0,0" VerticalAlignment="Top" Width="68"/>
  13.   </Grid>
  14. </Window>
« Last Edit: September 19, 2015, 11:30 PM by 4wd »