Sunday, August 2, 2009

Connect to network shared resources through script

Since WSH/VBScript doesn't facilitate password input with mask, we must combine windows scripting with a HTML page.

HTML page (call it password.htm):
<SCRIPT LANGUAGE="VBScript">

Sub RunScript
OKClicked.Value = "OK"
End Sub

Sub CancelScript
OKClicked.Value = "Cancelled"
End Sub

</SCRIPT>

<BODY>
<font size="2" face="Arial">
Login:&nbsp;&nbsp;&nbsp; </font><font face="Arial">
<input type="text" name="UserLogin" size="40"></font></p>

<font size="2" face="Arial">
Password:&nbsp;&nbsp;&nbsp; </font><font face="Arial">
<input type="password" name="UserPassword" size="40"></font></p>

<input type="hidden" name="OKClicked" size = "20">

<input id=runbutton class="button" type="button" value=" OK "
name="ok_button" onClick="RunScript">
&nbsp;&nbsp;&nbsp;
<input id=runbutton class="button" type="button" value="Cancel"
name="cancel_button" onClick="CancelScript">

</BODY>

Script (will call password.htm):
On Error Resume Next

Set objExplorer = WScript.CreateObject("InternetExplorer.Application", "IE_")
set net = Wscript.CreateObject("Wscript.Network")

objExplorer.Navigate "file:///C:\password.htm"
objExplorer.ToolBar = 0
objExplorer.StatusBar = 0
objExplorer.Width = 400
objExplorer.Height = 350
objExplorer.Left = 300
objExplorer.Top = 200
objExplorer.Visible = 1

Do While (objExplorer.Document.Body.All.OKClicked.Value = "")
Wscript.Sleep 250
Loop

strLogin = objExplorer.Document.Body.All.UserLogin.Value
strPassword = objExplorer.Document.Body.All.UserPassword.Value
strButton = objExplorer.Document.Body.All.OKClicked.Value
objExplorer.Quit
Wscript.Sleep 250

If strButton = "Cancelled" Then
Wscript.Quit
Else
'Wscript.Echo strPassword
net.MapNetworkDrive "", "\\fileserv\sharedfolder", , strLogin, strPassword
net.AddWindowsPrinterConnection "\\compname\sharedprinter", strLogin, strPassword
End If

Source:
How Can I Mask Passwords Using an InputBox?
www.microsoft.com/technet/scriptcenter/resources/qanda/feb05/hey0204.mspx
Accessing network resources using credentials with VBScript
www.winfrastructure.net/article.aspx?BlogEntry=Accessing-network-resources-using-credentials-with-VBScript

The downsides of this approach:
1) Only works with IE
2) Unless we set security level to Low (which is not recommended), we have to manually 'allow blocked content' each time we launch this script

A more elegant way is to install PassDlg.dll and use the included sample script (combined with part of the above script that establish connection to network shares)
Note for Windows 7 64bit:
Since PassDlg.dll is 32bit, we must associate (Open with, Always) the script with 32bit scripting host (
%SystemRoot%\SysWOW64\cscript.exe or %SystemRoot%\SysWOW64\wscript.exe)
If association fails (error "ActiveX component can't create object 'xxx.xxx'. Code: 800A01AD), create a .cmd file containing the following command:
%windir%\SysWOW64\wscript.exe "Network Resources.vbs"

lihat script selengkapnya di :
\\<server>\umum\source\windows\PassDlg

No comments:

Post a Comment