根据位置更改默认打印机

时间:2022-09-21 22:41:00

I would like to write a VBScript to change the default printer, based on which printer is connected.
I have a laptop that I use at work and at home, and I would like to run this script when starting windows so the default printer is always the correct one.
If there is another way to do this in XP, I'm all ears.

我想根据连接的打印机编写一个VBScript来更改默认打印机。我有一台笔记本电脑,我在工作和家里使用,我想在启动Windows时运行此脚本,因此默认打印机始终是正确的。如果在XP中有另一种方法可以做到这一点,我会全力以赴。

1 个解决方案

#1


WMI may suit.

WMI可能适合。

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" _
    & strComputer & "\root\cimv2")
Set colInstalledPrinters =  objWMIService.ExecQuery _
    ("Select * from Win32_Printer Where Name = 'ScriptedPrinter'")
For Each objPrinter in colInstalledPrinters
If objPrinter.Name="SomePrinterName" Then 
    objPrinter.SetDefaultPrinter()
End If
Next

-- http://msdn.microsoft.com/en-us/library/aa394598(VS.85).aspx

You can also find out the domain and such like:

您还可以找到域名等:

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" _
    & strComputer & "\root\cimv2")
Set colSettings = objWMIService.ExecQuery _
    ("Select * from Win32_ComputerSystem")
For Each objComputer in colSettings 
    Wscript.Echo "System Name: " & objComputer.Name
    Wscript.Echo "Domain: " & objComputer.Domain
Next

-- http://msdn.microsoft.com/en-us/library/aa394586.aspx

#1


WMI may suit.

WMI可能适合。

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" _
    & strComputer & "\root\cimv2")
Set colInstalledPrinters =  objWMIService.ExecQuery _
    ("Select * from Win32_Printer Where Name = 'ScriptedPrinter'")
For Each objPrinter in colInstalledPrinters
If objPrinter.Name="SomePrinterName" Then 
    objPrinter.SetDefaultPrinter()
End If
Next

-- http://msdn.microsoft.com/en-us/library/aa394598(VS.85).aspx

You can also find out the domain and such like:

您还可以找到域名等:

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" _
    & strComputer & "\root\cimv2")
Set colSettings = objWMIService.ExecQuery _
    ("Select * from Win32_ComputerSystem")
For Each objComputer in colSettings 
    Wscript.Echo "System Name: " & objComputer.Name
    Wscript.Echo "Domain: " & objComputer.Domain
Next

-- http://msdn.microsoft.com/en-us/library/aa394586.aspx