DonationCoder.com forum
 
Read-only archive. This is a static copy of the DonationCoder.com forum. Reading and searching work as before; logging in, posting and private messages do not.
Archive of 2026-10-21 · About this archive · Main site

Author Topic: Simple network drivemapping utility  (Read 7,102 times)

questorfla

  • Fighting Slime all the Time
  • Supporting Member
  • Joined in 2012
  • **
  • Posts: 570
Simple network drivemapping utility
« on: July 29, 2015, 07: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, 08:49 PM by questorfla »

x16wda

  • what am I doing in this handbasket?
  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 888
    • Participated in NANY Challenge Once
Re: Simple network drivemapping utility
« Reply #1 on: July 30, 2015, 01:51 AM »
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

  • Pedantic Old Bastard
  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 5,652
    • Coding Snacks
Re: Simple network drivemapping utility
« Reply #2 on: July 30, 2015, 03:37 AM »
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.
"I will not follow where the path may lead, but I will go where there is no path, and I will leave a trail." - Muriel Strode, 1903

questorfla

  • Fighting Slime all the Time
  • Supporting Member
  • Joined in 2012
  • **
  • Posts: 570
Re: Simple network drivemapping utility
« Reply #3 on: July 30, 2015, 02:03 PM »
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

  • Pedantic Old Bastard
  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 5,652
    • Coding Snacks
Re: Simple network drivemapping utility
« Reply #4 on: July 31, 2015, 12:39 AM »
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).
"I will not follow where the path may lead, but I will go where there is no path, and I will leave a trail." - Muriel Strode, 1903

4wd

  • Pedantic Old Bastard
  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 5,652
    • Coding Snacks
Re: Simple network drivemapping utility
« Reply #5 on: September 16, 2015, 04:48 AM »
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.png

Result:
2015-09-16 14_20_08.png

If there's an error:
2015-09-20 14_12_30.png

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)
If($PSVersionTable.PSVersion.Major -lt 3) {
  (new-object -ComObject wscript.shell).Popup("Script requires Powershell V3",0,"OK")
  Exit
}

# Read MapDrives.ini file
Get-Content "MapDrives.ini" | foreach-object -begin {$h=@{}} -process { $k = [regex]::split($_,'='); `
  if(($k[0].CompareTo("") -ne 0) -and ($k[0].StartsWith("[") -ne $True)) { $h.Add($k[0], $k[1]) } }

Function Load-Dialog {
  Param(
    [Parameter(Mandatory=$True,Position=1)]
    [string]$XamlPath
  )

  [xml]$Global:xmlWPF = Get-Content -Path $XamlPath

  #Add WPF and Windows Forms assemblies
  try{
    Add-Type -AssemblyName PresentationCore,PresentationFramework,WindowsBase,system.windows.forms
  } catch {
    Throw "Failed to load Windows Presentation Framework assemblies."
  }

  #Create the XAML reader using a new XML node reader
  $Global:xamGUI = [Windows.Markup.XamlReader]::Load((new-object System.Xml.XmlNodeReader $xmlWPF))

  #Create hooks to each named object in the XAML
  $xmlWPF.SelectNodes("//*[@Name]") | %{
    Set-Variable -Name ($_.Name) -Value $xamGUI.FindName($_.Name) -Scope Global
  }
}

Function Remove-Maps {
# Get all currently mapped drives
  $maps = (New-Object -Com WScript.Network).EnumNetworkDrives()
  $maps.Count()
  if ($maps.Count() -gt 0) {
    $Tb_Output.Text = "Remove mapped drives: "
# Remove all current mappings
    for ($i = 0; $i -lt $maps.Count() - 1; $i = $i + 2) {
      (New-Object -Com WScript.Network).RemoveNetworkDrive($maps.Item($i))
    }
    $Tb_Output.AppendText("Done`r")
  } else {
    $Tb_Output.Text = "No existing mapped drives`r"
  }
}

Function Map-Drives {
  for ($i = 0; $i -lt [Math]::Truncate($h.Count / 2); $i++) {
    $t1 = "map" + $i
    $t2 = "unc" + $i
    if ($h.Get_Item($t1) -ne "") {
      if ($i -eq 0) {
        $root = $h.Get_Item($t2) + "\" + $user
      } else {
        $root = $h.Get_Item($t2)
      }
      $Tb_Output.AppendText("Mapping " + $h.Get_Item($t1))

      $cmd = "use " + $h.Get_Item($t1) + " " + $root + " " + $pass + " /user:" + $user
      $errfile = $env:TEMP + "\MapDrive.txt"
      $result = Start-Process -FilePath "net.exe" -ArgumentList $cmd -Wait -PassThru -WindowStyle Hidden -RedirectStandardError $errfile
      if ($result.ExitCode -ne 0) {
        Write-Host $result.StandardError
        Write-Host $result.StandardOutput
        $Tb_Output.AppendText(" Failed`r")
        $errtxt = Get-Content -Path $errfile
        $Tb_Output.AppendText("[" + $errtxt[2] + "]`r")
        Remove-Item $errfile -Force
      } else {
        $Tb_Output.AppendText(" Done`r")
      }
    }
  }
}

#Required to load the XAML form and create the PowerShell Variables
Load-Dialog -XamlPath '.\MapDrives.xaml'

#EVENT Handlers
$Bt_Map.add_Click({
  $Tb_Output.Clear()
  $global:user = ""
  $global:pass = ""
  if ($Tb_Username.Text -ne "") { $user = $Tb_Username.Text }
  if ($Pb_Password.Password -ne "") { $pass = $Pb_Password.Password }
  if (($user.Length -ne 0) -and ($pass.Length -ne 0)) {
    Remove-Maps
    Start-Sleep -Milliseconds 500
    Map-Drives
    $Tb_Output.AppendText("Complete`r")
  } else {
    $Tb_Output.Text = ("Missing Username or Password`r")
  }
})

$Tb_Username.Add_TextChanged({
  $Tb_Output.Clear()
})

$Pb_Password.Add_KeyDown({
  $Tb_Output.Clear()
})

#Launch the window
$xamGUI.ShowDialog() | out-null

MapDrives.ini
Code: ( xml)
[General]
map0=R:
unc0=\\10.0.50.1\users
map1=Y:
unc1=\\10.0.50.1\Data
map2=X:
unc2=\\10.1.51.12\itsme
map3=
unc3=\\server01\clients

MapDrives.xaml (the interface definition)
Code: ( xml)
<Window
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="MapDrives" Height="217" Width="354" ResizeMode="NoResize">
  <Grid>
    <TextBox Name="Tb_Username" HorizontalAlignment="Left" Height="23" Margin="87,17,0,0" VerticalAlignment="Top" Width="140" ToolTip="Enter your username" MaxLines="1"/>
    <Label Name="Lb_Username" Content="Username:" Margin="14,14,0,0" VerticalAlignment="Top" HorizontalAlignment="Left" Width="68"/>
    <Label Name="Lb_password" Content="Password:" HorizontalAlignment="Left" Margin="14,42,0,0" VerticalAlignment="Top" Width="68"/>
    <PasswordBox Name="Pb_Password" ToolTip="Enter your password" HorizontalAlignment="Left" Margin="87,45,0,0" VerticalAlignment="Top" Width="140" Height="23"/>
    <Button Name="Bt_Map" Content="Map Drives" HorizontalAlignment="Left" Margin="232,30,0,0" VerticalAlignment="Top" Width="75"/>
    <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"/>
    <Label Name="Lb_Output" Content="Output:" HorizontalAlignment="Left" Margin="14,109,0,0" VerticalAlignment="Top" Width="68"/>
  </Grid>
</Window>
"I will not follow where the path may lead, but I will go where there is no path, and I will leave a trail." - Muriel Strode, 1903
« Last Edit: September 20, 2015, 04:30 AM by 4wd »

Original address: https://www.donationcoder.com/forum/index.php?topic=41351.0