2010年11月10日星期三

Windows Script勉強の5 高級1-ネットワーク

ネットワークに関する制御にはWScript.Networkオブジェクトが必要です。
① MapNetworkDrive 作成
// JScript.
var net;
net = new ActiveXObject("WScript.Network");
net.MapNetworkDrive("I:", "\\\\computer2\\public","True","jdoe","jdoepassword");
' VBScript.
Dim net
Set net = CreateObject("WScript.Network")   
net.MapNetworkDrive "I:", "\\computer2\public","True","jdoe","jdoepassword"

パラメータ説明:
  • ローカル ドライブの割り当て (I: など)
  • マップされたリモート ドライブへのUNC (Universal Naming Convention) パス
  • ドライブを永続的に接続するかどうかを示すブール値 (省略可能)
  • ユーザー名 (省略可能)。別のアカウント情報を使用する場合に指定
  • パスワード (省略可能)。別のユーザー名を使用する場合に指定

②プリンタに接続
// JScript.
var net;
net = new ActiveXObject("WScript.Network");
net.AddWindowsPrinterConnection("\\\\ServerName\\PrinterName");

' VBScript.
Dim net
Set net = CreateObject("WScript.Network")   
net.AddWindowsPrinterConnection \\ServerName\PrinterName

③プリンタを既定に設定
// JScript.
var net;
net = new ActiveXObject("WScript.Network");
net.SetDefaultPrinter("\\\\ServerName\\PrinterName");

' VBScript.
Dim net
Set net = CreateObject("WScript.Network")   
net.SetDefaultPrinter file://servername/PrinterName

④Windows起動後、自動でLoginUserによりプリンタを設定等
// JScript.
var oNet, sUser, cInitial, startTime;
oNet = new ActiveXObject("WScript.Network");
// ユーザー名を取得します。Windows 9x および Windows Me では、スクリプト 実行時にユーザー
// がログオンしていない可能性があります。
// ログオンが完了するまで 0.5 秒間隔でチェックを繰り返します。
sUser = oNet.UserName;
startTime = new Date();
while (sUser == "")
{
   var curTime = new Date();
   if (curTime – startTime > 30000) WScript.Quit();
   WScript.Sleep(500);
   sUser = oNet.UserName;
}
// ユーザー名の頭文字に基づいて「H」ドライブおよびプリンタの共有を作成します。
cInitial = sUser.charAt(0).toUpperCase();
if (cInitial < "L")
{
   oNet.MapNetworkDrive("h:", "\\\\server1\\users\\" + sUser);
   oNet.AddWindowsPrinterConnection("\\\\printer1\\hp", "HP LaserJet 4");
}
else
{
   oNet.MapNetworkDrive("h:", "\\\\server2\\users\\" + sUser);
   oNet.AddWindowsPrinterConnection("\\\\printer2\\hp", "HP LaserJet 4");
}

' VBScript.

Option Explicit
Dim oNet, sUser, cInitial, startTime
' Helper object
Set oNet = CreateObject("WScript.Network")
' ユーザー名を取得します。Windows 9x および Windows Me では、スクリプト 実行時にユーザー
' がログオンしていない可能性があります。
' ログオンが完了するまで 0.5 秒間隔でチェックを繰り返します。
sUser = oNet.UserName
startTime = Now
Do While sUser = ""
   If DateDiff("s", startTime, Now) > 30 Then Wscript.Quit
   Wscript.Sleep 500
   sUser = oNet.UserName
Loop
' ユーザー名の頭文字に基づいて「H」ドライブおよびプリンタの共有を作成します。
cInitial = UCase(Left(sUser, 1))
If (cInitial < "L") Then
   oNet.MapNetworkDrive "h:", "\\server1\users\" & sUser
   oNet.AddWindowsPrinterConnection "\\printer1\hp", "HP LaserJet 4"
Else
   oNet.MapNetworkDrive "h:", "\\server2\users\" & sUser
   oNet.AddWindowsPrinterConnection "\\printer2\hp", "HP LaserJet 4"
End If

没有评论: