Archive for category WMI

Change / Remap printers on client computers

On Error Resume Next
Set WSHNetwork = CreateObject(“WScript.Network”)

‘ create a dictionary to store our printer paths
Set objDictPrnts = CreateObject(“Scripting.Dictionary”)
objDictPrnts.CompareMode = vbTextCompare
objDictPrnts.Add “\\printrete\P1“, “\\printrete\P1
‘objDictPrnts.Add “\\printrete\P2“, “\\printrete\P2
objDictPrnts.Add “\\stampanti\P3“, “\\stampanti\P3

‘ loop through printer connections
Set WSHPrinters = WSHNetwork.EnumPrinterConnections
For LOOP_COUNTER = 0 To WSHPrinters.Count – 1 Step 2   
PrinterPath = WSHPrinters.Item(LOOP_COUNTER +1)   

‘ if the current path exist in our dictionary remove it   
If objDictPrnts.Exists(PrinterPath) Then
objDictPrnts.Remove PrinterPath
WSHNetwork.RemovePrinterConnection PrinterPath
End if
Next

‘ loop through the path’s that were not found and add them
For Each PrinterPath In objDictPrnts.Keys
WSHNetwork.AddWindowsPrinterConnection PrinterPath
Next

Tags:

VBS – Point Client Computers to a New Print Server

Logon script that changes the printer connections on workstations.

  

‘This script works well in a login script. When ran from a workstation
‘it will loop through all the printers installed for the current logged on
‘user and if they are connected to the old server they will be removed
‘and then remapped to the new server. It will also attempt to discover
‘which printer is the default printer in order to reset this if it is one
‘of the printers that will be moved.

‘Note: All of the printers must exist on the new server before this script
‘ should be run. If not then the printer will simply be removed from
‘ users profile and the script will not be able to reconnect them.
‘ “Print Migrator” is a utility that is part of the Windows 2000 and
‘ Windows 2003 resource kit. This will create all of the printers
‘ on the new server while maintaining all of their settings including
‘ print queue security. Keep in mind that for a time you will see
‘ duplicate printer if you do a search on printers, one advertisec
‘ from each server.

‘We used this method to move just over 100 printers from one server to another
‘and then ran this script as a logon script in a group policy. We let it run for
‘a few days in order to allow for some users who don’t logout every day have a
‘chance to run it. All of our users profiles were updated and they didn’t even
‘know it was happening. We then deleted all of the printers from the old server.

‘Note: The script does not run if you are in Terminal Service or SMS remote control.

Option Explicit
Dim from_sv, to_sv, PrinterPath, PrinterName, DefaultPrinterName, DefaultPrinter
Dim DefaultPrinterServer, SetDefault, key
Dim spoint, Loop_Counter, scomma
Dim WshNet, WshShell
Dim WS_Printers
DefaultPrinterName = “”
spoint = 0
scomma = 0
SetDefault = 0
set WshShell = CreateObject(“WScript.shell”)

from_sv = “\\old” ‘This should be the name of the old server.
to_sv = “\\new” ‘This should be the name of your new server.

‘Just incase their are no printers and therefor no defauld printer set
‘ this will prevent the script form erroring out.
On Error Resume Next
key = “HKCU\Software\Microsoft\Windows NT\CurrentVersion\Windows\Device”
DefaultPrinter = LCase(WshShell.RegRead (key))

If Err.Number <> 0 Then
DefaultPrinterName = “”
else
‘If the registry read was successful then parse out the printer name so we can
‘ compare it with each printer later and reset the correct default printer
‘ if one of them matches this one read from the registry.
spoint = instr(3,DefaultPrinter,”\”)+1

DefaultPrinterServer = left(DefaultPrinter,spoint-2)

if lcase(DefaultPrinterServer) = from_sv then
DefaultPrinterName = mid(DefaultPrinter,spoint,len(DefaultPrinter)-spoint+1)
scomma = instr(DefaultPrinterName,”,”)
DefaultPrinterName = left(DefaultPrinterName,scomma -1)
end if
end if

Set WshNet = CreateObject(“WScript.Network”)
Set WS_Printers = WshNet.EnumPrinterConnections

‘You have to step by 2 because only the even numbers will be the print queue’s
‘ server and share name. The odd numbers are the printer names.
For Loop_Counter = 0 To WS_Printers.Count – 1 Step 2
‘Remember the + 1 is to get the full path ie.. \\your_server\your_printer.
PrinterPath = lcase(WS_Printers(Loop_Counter + 1))

‘We only want to work with the network printers that are mapped to the original
‘ server, so we check for “\\Your_server”.
if lcase(LEFT(PrinterPath,len(from_sv))) = from_sv then
‘Now we need to parse the PrinterPath to get rhe Printer Name.
spoint = instr(3,PrinterPath,”\”)+1
PrinterName = mid(PrinterPath,spoint,len(PrinterPath)-spoint+1)
‘Now remove the old printer connection.
WshNet.RemovePrinterConnection from_sv+”\”+PrinterName
‘and then create the new connection.
‘Do not create c6100
if lcase(PrinterName) <> “c6100” then
WshNet.AddWindowsPrinterConnection to_sv+”\”+PrinterName
‘If this printer matches the default printer that we got from the registry then
‘ set it to be the default printer.
if DefaultPrinterName = PrinterName then
WshNet.SetDefaultPrinter to_sv+”\”+PrinterName
end if
end if
end if
Next
Set WS_Printers = Nothing
Set WshNet = Nothing
Set WshShell = Nothing

‘ wscript.echo “Printers Migrated”

Platforms

Windows Server 2008 R2 No
Windows Server 2008 No
Windows Server 2003 No
Windows 7 No
Windows Vista No
Windows XP No
Windows 2000 No

Tags: ,

WMI check OS

Check OS Version
strComputer = “.” Set objWMIService = GetObject(“winmgmts:\\” & strComputer & “\root\CIMV2”) Set colItems = objWMIService.ExecQuery( _ “SELECT * FROM Win32_OperatingSystem”,,48) For Each objItem in colItems Wscript.Echo “———————————–” Wscript.Echo “Win32_OperatingSystem instance” Wscript.Echo “———————————–” Wscript.Echo “Caption: ” & objItem.Caption Next
Check BIOS Version:
strComputer = “.”
Set objWMIService = GetObject(“winmgmts:\\” & strComputer & “\root\CIMV2”)
Set colItems = objWMIService.ExecQuery( _
    “SELECT * FROM Win32_BIOS”,,48)
For Each objItem in colItems
    Wscript.Echo “———————————–”
    Wscript.Echo “Win32_BIOS instance”
    Wscript.Echo “———————————–”
    Wscript.Echo “Caption: ” & objItem.Caption
Next
Check local users:
strComputer = “.”
Set objWMIService = GetObject(“winmgmts:\\” & strComputer & “\root\CIMV2”)
Set colItems = objWMIService.ExecQuery( _
    “SELECT * FROM Win32_UserAccount”,,48)
For Each objItem in colItems
    Wscript.Echo “———————————–”
    Wscript.Echo “Win32_UserAccount instance”
    Wscript.Echo “———————————–”
    Wscript.Echo “Caption: ” & objItem.Caption
    Wscript.Echo “Domain: ” & objItem.Domain
    Wscript.Echo “FullName: ” & objItem.FullName
    Wscript.Echo “PasswordChangeable: ” & objItem.PasswordChangeable
    Wscript.Echo “PasswordExpires: ” & objItem.PasswordExpires
    Wscript.Echo “SID: ” & objItem.SID
Next

Tags: