WshNetwork 对象

提供对 Microsoft Windows 网络的访问,使远程驱动器和打印机的连接与断开变得简单。

示例

下例演示了如何在两个使用不同脚本语言(VBScript and JScript)的任务中使用同一个 .wsf 文件。这两个任务的功能相同——显示当前机器的域名、计算机名、用户名及其驱动器和打印机的网络映射信息:

<package>

<job id="vbs">
<script language="VBScript">
   Set WshNetwork = WScript.CreateObject("WScript.Network")
   Set oDrives = WshNetwork.EnumNetworkDrives
   Set oPrinters = WshNetwork.EnumPrinterConnections
   WScript.Echo "Domain = " & WshNetwork.UserDomain
   WScript.Echo "Computer Name = " & WshNetwork.ComputerName
   WScript.Echo "User Name = " & WshNetwork.UserName
   WScript.Echo 
   WScript.Echo "Network drive mappings:"
   For i = 0 to oDrives.Count - 1 Step 2
      WScript.Echo "Drive " & oDrives.Item(i) & " = " & oDrives.Item(i+1)
   Next
   WScript.Echo 
   WScript.Echo "Network printer mappings:"
   For i = 0 to oPrinters.Count - 1 Step 2
      WScript.Echo "Port " & oPrinters.Item(i) & " = " & oPrinters.Item(i+1)
   Next
</script>
</job>

<job id="js">
<script language="Javascript">
   var WshNetwork = new ActiveXObject("WScript.Network");
   var oDrives = WshNetwork.EnumNetworkDrives();
   var oPrinters = WshNetwork.EnumPrinterConnections();
   WScript.Echo("Domain = " + WshNetwork.UserDomain);
   WScript.Echo("Computer Name = " + WshNetwork.ComputerName);
   WScript.Echo("User Name = " + WshNetwork.UserName);
   WScript.Echo();
   WScript.Echo("Network drive mappings:");
   for(i=0; i<oDrives.Count(); i+=2){
      WScript.Echo("Drive " + oDrives.Item(i) + " = " + oDrives.Item(i+1));
   }
   WScript.Echo();
   WScript.Echo("Network printer mappings:");
   for(i=0; i<oPrinters.Count(); i+=2){
   WScript.Echo("Port " + oPrinters.Item(i) + " = " + oPrinters.Item(i+1));
   }
</script>
</job>

</package>

详细信息请参阅:运行脚本

属性

ComputerName 属性 | UserDomain 属性 | UserName 属性

方法

AddPrinterConnection 方法 | AddWindowsPrinterConnection 方法 | EnumNetworkDrives 方法 | EnumPrinterConnection 方法 | MapNetworkDrive 方法 | RemoveNetworkDrive 方法 | RemovePrinterConnection 方法 | SetDefaultPrinter 方法