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