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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
Angular Fundamentals

Learn the fundamentals of Angular 2, a JavaScript framework for developing dynamic single page applications.

Dany BalianCTOCommented:
you can msgbox pname to verify that it's fetching the list of printers
0
diascmAuthor Commented:
inboxed!
0
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
diascmAuthor Commented:
GREAT AND FAST SOLUTION!!!!!!!!!!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.