Scrip to list and rename TS printers by removing the "in session X"

Hello,

I Need a script that will list and rename all printers in my Server 2003 Terminal session

Ex: Neest to list and rename the follwing :

EPSON (from STAT-1) in session 2
HP (from STAT-1) in session 2


TO
EPSON (from STAT-1)
HP (from STAT-1)

Basically remove the " in session X"      where X can be anything from 1 to 99  and where thr ptinter names can be anything from DELL to HP to OKIDATA etc....


Any Ideas?

Thx!
diascmAsked:
Who is Participating?
 
Dany BalianCTOCommented:
Here's the final tested code!
Posting this for anyone who might need this as a solution. Also make sure that you run this as an administrator!

strComputer = "."
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colInstalledPrinters = objWMIService.ExecQuery("Select * from Win32_Printer")
dim pname, pos
For each objPrinter in colInstalledPrinters
    pname= objPrinter.Name
    pos=instr(1,pname, " in session ")
    if pos>0 then
       objPrinter.RenamePrinter(left(pname,pos))
    end if
Next

Open in new window

0
 
Dany BalianCTOCommented:
ok, i don't have a windows pc at hand... you can try something like this..

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\" & strComputer & "rootcimv2")
Set colInstalledPrinters =  objWMIService.ExecQuery _
    ("Select * from Win32_PrinterDriver") 
dim pname, pos
For each objPrinter in colInstalledPrinters
    pname= objPrinter.Name
    pos=instr(1,pname, " in session ")
    if pos>0 then
     objPrinter.RenamePrinter(left(pname,pos))
    end if
Next 

Open in new window

0
 
diascmAuthor Commented:
thx, just tested and error attached ... Tested with and without admin privileges
error.jpg
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
Dany BalianCTOCommented:
strComputer = "."
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 
Set colInstalledPrinters = objWMIService.ExecQuery("Select * from Win32_Printer") 
dim pname, pos
For each objPrinter in colInstalledPrinters
    pname= objPrinter.Name
    pos=instr(1,pname, " in session ")
    if pos>0 then
     objPrinter.RenamePrinter(left(pname,pos))
    end if
Next 

Open in new window

0
 
Dany BalianCTOCommented:
you can msgbox pname to verify that it's fetching the list of printers
0
 
diascmAuthor Commented:
inboxed!
0
 
diascmAuthor Commented:
GREAT AND FAST SOLUTION!!!!!!!!!!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.