Notices
Computer & Technology Related Post here for help and discussion of computing and related technology. Internet, TVs, phones, consoles, computers, tablets and any other gadgets.

VB Script Help Please

Thread Tools
 
Search this Thread
 
Old 15 March 2007, 01:31 PM
  #1  
kelvin
Scooby Regular
Thread Starter
 
kelvin's Avatar
 
Join Date: Dec 2000
Posts: 754
Likes: 0
Received 0 Likes on 0 Posts
Default VB Script Help Please

I know there are a few indivduals on here who know a thing or two about scripting, I hope someone can help...

I'm trying to run a script which examines the logged in user's printers, and replaces those on an old server with their equivalent replacements on the new one. The script utilises a text file which maps the old names to new.

I've placed a batch file in the login script which copies both the printers.vbs, and printers.txt files to the local machine before firing off printers.vbs. It runs perfectly using my login (full admin rights to the domain, and machine), and also using a test user (standard domain user, no local admin rights), but i'm having some difficulty when deploying it to the other users on site in that it appears to run (command completes successfully), but fails to action any of the printer changes. Both files copy successfully. To rule out a timing issue with the loading of the profile, i've also tried deploying the script remotely to PC's on site using LanDesk with users logged in. Again, this works with my test users, but not with the "real" domain users when run.

I've placed the code below in case it's something silly i've done, but would appreciate any suggestions anyone has?!

Kelvin.

Code:
'Logon Script to Automatically Install New Printers

wscript.echo "### Examining Your Printers. Please Wait... ###"

On error resume next

' This code enumerates the user's current network printer connections.
Set WshNetwork = CreateObject("WScript.Network")
Set oPrinters = WshNetwork.EnumPrinterConnections

For j = 0 to oPrinters.Count - 1 Step 2

  ' This code opens a comma-delimited text file containing  
  ' the corresponding old and new printers and assigns
  ' each line as an array variable.
  Const ForReading = 1
  Set objFSO = CreateObject("Scripting.FileSystemObject")
  Set objTextFile = objFSO.OpenTextFile ("C:\source\printers.txt", ForReading)

   Do Until objTextFile.AtEndOfStream
     strNextLine = objTextFile.Readline
     arrPrinterList = Split(strNextLine , ",")
     ' This section retrieves the registry value corresponding to the  
     ' default printer and assigns the registry value as a string variable.
     const HKEY_CURRENT_USER = &H80000001
     strComputer = "."
     Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" _ 
        & strComputer & "\root\default:StdRegProv")
     strKeyPath = "Software\Microsoft\Windows NT\CurrentVersion\Windows"
     strValueName = "Device"
     oReg.GetStringValue HKEY_CURRENT_USER,strKeyPath,strValueName,strValue
    'wscript.echo(strValue)
 
   ' This code takes both variables defined above and introduces conditional logic.

     For i = 1 to Ubound(arrPrinterList)
       If strValue =(arrPrinterList(0)) then
         Wscript.Echo "Your default printer will be changed to: " & arrPrinterList(i)
         WshNetwork.AddWindowsPrinterConnection arrPrinterList(i)
         WshNetwork.SetDefaultPrinter arrPrinterList(i)
         WshNetwork.RemovePrinterConnection arrPrinterList(0)
       Else
         If oPrinters.Item(j+1)=arrPrinterList(0) then
           WshNetwork.RemovePrinterConnection arrPrinterList(0)
           WshNetwork.AddWindowsPrinterConnection arrPrinterList(i)
         End If
     End If

     Next
  
   Loop
Next
wscript.echo
wscript.echo "Completed Successfully"
Old 15 March 2007, 02:44 PM
  #2  
KiwiGTI
Scooby Regular
 
KiwiGTI's Avatar
 
Join Date: Aug 2004
Posts: 4,631
Likes: 0
Received 0 Likes on 0 Posts
Default

Aren't login scripts run in the context of the System Account?
Old 15 March 2007, 03:07 PM
  #3  
kelvin
Scooby Regular
Thread Starter
 
kelvin's Avatar
 
Join Date: Dec 2000
Posts: 754
Likes: 0
Received 0 Likes on 0 Posts
Default

Originally Posted by KiwiGTI
Aren't login scripts run in the context of the System Account?
Not that i'm aware of, login scripts run as the user... (I believe...)

In any case, i'm using an additional utility when deploying via LanDesk that forces the script to run under the logged in user. Works fine in the test environment too which is what is so damn confusing!!

Kelvin.

Last edited by kelvin; 15 March 2007 at 03:16 PM.
Old 15 March 2007, 05:14 PM
  #4  
David_Wallis
Scooby Regular
 
David_Wallis's Avatar
 
Join Date: Nov 2001
Location: Leeds - It was 562.4bhp@28psi on Optimax, How much closer to 600 with race fuel and a bigger turbo?
Posts: 15,239
Likes: 0
Received 1 Like on 1 Post
Default

use this script...

Code:
On Error Resume Next
Const Title = "Print Connection Migrator"
Const ForReading = 1

Dim strDefaultPrinter 
Dim InstalledPrinters 
Dim Textfile 
Dim OldPrintQueues() 
Dim NewPrintQueues() 
Dim fso 
Dim objTextFile  
Dim strNextLine 
Dim i
Dim WshNetwork
Set WshNetwork = CreateObject("WScript.Network")

Set WshShell = WScript.CreateObject("WScript.Shell")

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile (WshShell.CurrentDirectory & "\changes.txt", ForReading)

i=-1

While not objTextFile.AtEndOfStream
    Redim Preserve OldPrintQueues(i)
    ReDim Preserve NewPrintQueues(i)
    strLine = objTextFile.Readline
    'Do not import the comment lines
    If Left(strLine,2) = "\\" Then
        OldPrintQueues(i) = Left(strLine,InStr(strline,";")-1)
        NewPrintQueues(i) = Mid(strline,InStr(strline,";")+1,Len(strline))
	End If
Wend

objTextFile.Close

' Store the name of the default Printer

strDefaultPrinter = DefaultPrinter

' WMI query for current printer connections

strComputer = "."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colInstalledPrinters = objWMIService.ExecQuery ("Select * from Win32_Printer")

' Loop through printer collection

For Each objPrinter in colInstalledPrinters
   If Left(objPrinter.Name, 2) = "\\" Then 'Work only On network printers
        'Search the corresponding printer and create it
        i = 0 'set the indice at the beginning of the array (prepare to loop)
        Do Until i > UBound(OldPrintQueues)
            If UCase(objPrinter.Name) = UCase(OldPrintQueues(i)) Then
                'Create the connection to the new printer
                WshNetwork.AddWindowsPrinterConnection NewPrintQueues(i)
                If UCase(strDefaultPrinter) = UCase(objPrinter.Name) Then 'This is the default printer
                    'Set the default Printer
                    WshNetwork.SetDefaultPrinter NewPrintQueues(i)
                End If
                'Delete the printer connection
                WshNetwork.RemovePrinterConnection objPrinter.Name
            End If
            i = i + 1
        Loop
    End If 'End of check for network printers
Next 'End of the loop through the printers of this user

Set WshNetwork = Nothing

WriteFlagFile 

' Functions

Function DefaultPrinter
    Dim strComputer
    Dim Result
    
    strComputer = "."
    Result = ""
    
    Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
    Set colInstalledPrinters = objWMIService.ExecQuery ("Select * from Win32_Printer")
    For Each objPrinter in colInstalledPrinters
        If objPrinter.Default = True Then
         Result = objPrinter.Name
        End If
    Next
    DefaultPrinter = Result
End Function

Function FileExist (FileFullPath)
    Dim Fso
    Set Fso = CreateObject("Scripting.FileSystemObject")
    If (Fso.FileExists(FileFullPath)) Then
        FileExist = True
    Else
        FileExist = False
    End If
End Function

Function WriteFlagFile
   Set objFSO = CreateObject("Scripting.FileSystemObject")
   Set MyFile = objfso.CreateTextFile("c:\printer.txt", True)
   MyFile.WriteLine
   MyFile.Close

End Function

this is the file changes.txt

oldUNC;newUNC

Code:
\\server\zone 12;\\prn-01\prn0098
\\server\zone8-a4bw-01;\\prn-01\prn1420
Most likely the problem is when your executing the script on the remote machine its running under a different process, ie not interactively with the user.

save the script and make the users call it as part of the login script.

David
Old 15 March 2007, 05:15 PM
  #5  
David_Wallis
Scooby Regular
 
David_Wallis's Avatar
 
Join Date: Nov 2001
Location: Leeds - It was 562.4bhp@28psi on Optimax, How much closer to 600 with race fuel and a bigger turbo?
Posts: 15,239
Likes: 0
Received 1 Like on 1 Post
Default

Also does your landesk 'service account' have the correct permissions on the real pc's as opposed to the test pc's.

David
Old 15 March 2007, 07:36 PM
  #6  
kelvin
Scooby Regular
Thread Starter
 
kelvin's Avatar
 
Join Date: Dec 2000
Posts: 754
Likes: 0
Received 0 Likes on 0 Posts
Default

David,

Thanks for taking the time to reply.

The LanDesk SA has full access to the local machines - it sits in our Domain Admins group. It copies the files across to the source folder of the target machine without problem (every PC on site now has a copy of printers.vbs, and printers.txt sat on the C drive!), then uses an executeable to run the vbs script under the logged in user (By default, LanDesk runs scripts on remote PC's using the local system account. The executeable is a tool supplied by LanDesk to cater for the scenario where you need to run/extract data via the local user).

I can have my Printers & Faxes console open and watch it making the relevant changes...

Ref. your script, i'm having a bit of trouble getting it to work. I'm running it locally via the command line of my test machine and it's completing in approx 2 seconds, and not doing a great deal. Copy of my "changes.txt" file is below:-

Code:
\\bohprint001\IR 6800 Finance W2k;\\bohprint002\Printer_A
\\bohprint001\IR6800 Finance NT4;\\bohprint002\Printer_A
\\bohprint001\IR3100 ops W2k;\\bohprint002\Printer_B
\\bohprint001\IR3100 Ops NT4;\\bohprint002\Printer_B
\\bohprint001\IR3100 HR NT4;\\bohprint002\Printer_C
\\bohprint001\TankDevils_Maximo;\\bohprint002\Printer_D
\\bohprint001\TankDevils;\\bohprint002\Printer_D
\\bohprint001\IR3100 Tank Devils NT4;\\bohprint002\Printer_D
\\bohprint001\IR3100 Tank Dev W2k;\\bohprint002\Printer_D
\\bohprint001\IR6800 Directors W2k;\\bohprint002\Printer_F
\\bohprint001\IR6800 Directors NT4;\\bohprint002\Printer_F
\\bohprint001\IR6800 Design NT4;\\bohprint002\Printer_G
\\bohprint001\IR3100 Stores B48 NT4;\\bohprint002\Printer_H
\\bohprint001\IR3100 Stores B48 W2k;\\bohprint002\Printer_H
\\bohprint001\Ir 6800 Planning W2k;\\bohprint002\Printer_J
\\bohprint001\IR 6800  Planning NT4;\\bohprint002\Printer_J
\\bohprint001\IR3100 Aerostruc W2k;\\bohprint002\Printer_K
\\bohprint001\IR3100 Aerostruc NT4;\\bohprint002\Printer_K
\\bohprint001\IR3100 Marketing W2k;\\bohprint002\Printer_L
\\bohprint001\IR3100 Marketing;\\bohprint002\Printer_L
\\bohprint001\IR3100 ops 1st W2k;\\bohprint002\Printer_M
\\bohprint001\IR3100 ops 1st NT4;\\bohprint002\Printer_M
\\bohprint001\BOHH004PUR000N;\\bohprint002\Printer_N
\\bohprint001\BOHB009PUR000N;\\bohprint002\Printer_N
\\bohprint001\QA6_5;\\bohprint002\Printer_P
\\bohprint001\IR6800 QA W2k;\\bohprint002\Printer_P
\\bohprint001\IR6800 QA B9 NT4;\\bohprint002\Printer_P
\\bohprint001\BOHH004STO000N;\\bohprint002\Printer_Q
\\bohprint001\Canon iR 3100C EW;\\bohprint002\Printer_R
\\bohprint001\OverHaulMaximoLJ4;\\bohprint002\Printer_S
\\bohprint001\iR 6800C Overhaul W2k;\\bohprint002\Printer_S
\\bohprint001\IR 6800 Overhaul nt4;\\bohprint002\Printer_S
\\bohprint001\BOHHNG1ENG000T;\\bohprint002\Printer_T
\\bohprint001\IR Airtanker NT4;\\bohprint002\Printer_U
\\bohprint001\IR 3100C AIRTANK W2k;\\bohprint002\Printer_U
\\bohprint001\IR3100 Projects W2k;\\bohprint002\Printer_W
\\bohprint001\IR3100 Projects NT4;\\bohprint002\Printer_W
\\bohprint001\BOHH004GOODSIN;\\bohprint002\Printer_X
\\bohprint001\IR3100 Man_acc W2k;\\bohprint002\Printer_Y
\\bohprint001\IR3100 Man_Acc NT4;\\bohprint002\Printer_Y
\\bohprint001\TrainingSchool;\\bohprint002\Printer_Z
\\bohprint001\IR3100 Training W2k;\\bohprint002\Printer_Z
\\bohprint001\IR3100 Training NT4;\\bohprint002\Printer_Z
\\bohprint001\BOHB007DT001;\\bohprint002\BOHB007DT001
\\bohprint001\BOHB007IT001;\\bohprint002\BOHB007IT001
\\bohprint001\BOHB007JH001;\\bohprint002\BOHB007JH001
\\bohprint001\BOHB007ManAcc1;\\bohprint002\BOHB007MA001
\\bohprint001\MD_Office;\\bohprint002\BOHB007MD001
\\bohprint001\BOHB007ME001;\\bohprint002\BOHB007ME001
\\bohprint001\BOHB007MKT001;\\bohprint002\BOHB007MKT001
\\bohprint001\Marketing;\\bohprint002\BOHB007MKT001
\\bohprint001\BOHB009QA001;\\bohprint002\BOHB009QA001
\\bohprint001\BOHB042EW001;\\bohprint002\BOHB042EW001
\\bohprint001\BOHB44MP001;\\bohprint002\BOHB048MY001
\\bohprint001\BOHSITE001;\\bohprint002\BOHB317SS001
\\bohprint001\BOHH001AER001;\\bohprint002\BOHH001AER001
\\bohprint001\WarehouseHURN;\\bohprint002\WarehouseHURN
\\bohprint001\Engineering_Secretary;\\bohprint002\BOHB009LG001
Any more idea's?!!! The text file is in the C:\Documents & Settings\PrinterTest folder that i'm executing the script from.

Appreciate your help so far.

Kelvin.
Old 15 March 2007, 10:12 PM
  #7  
David_Wallis
Scooby Regular
 
David_Wallis's Avatar
 
Join Date: Nov 2001
Location: Leeds - It was 562.4bhp@28psi on Optimax, How much closer to 600 with race fuel and a bigger turbo?
Posts: 15,239
Likes: 0
Received 1 Like on 1 Post
Default

Ill have a look tomorrow when Im at work, to be fair I was in the middle of editing it and removed all the output and havent tested it, so Ill post / email a new version.
Old 15 March 2007, 10:15 PM
  #8  
Miles
Scooby Regular
 
Miles's Avatar
 
Join Date: Oct 1998
Location: The Granite City/Dallas, Tx.
Posts: 2,519
Likes: 0
Received 0 Likes on 0 Posts
Default

Err, there's a much easier way of doing this - look at ChangePrintSrv.exe from Change Print Server

Insert a line in your login script
ChangePrintSrv From <oldserver> to <newserver>
Old 16 March 2007, 09:31 AM
  #9  
kelvin
Scooby Regular
Thread Starter
 
kelvin's Avatar
 
Join Date: Dec 2000
Posts: 754
Likes: 0
Received 0 Likes on 0 Posts
Default

Originally Posted by Miles
Err, there's a much easier way of doing this - look at ChangePrintSrv.exe from Change Print Server

Insert a line in your login script
ChangePrintSrv From <oldserver> to <newserver>
Miles,

That app will only work if the server name is the only thing changing. Our problem lies in the fact that virtually all of the printers on the new server also have different names. It's not a like for like replacement.

Thanks anyway though.

David,

Look forward to hearing from you - thanks very much for your help so far

Kelvin.
Old 16 March 2007, 07:15 PM
  #10  
kelvin
Scooby Regular
Thread Starter
 
kelvin's Avatar
 
Join Date: Dec 2000
Posts: 754
Likes: 0
Received 0 Likes on 0 Posts
Default

Got it!!!

Turns out that the script treats the contents of the text file as absolute - including the case! Evidently "BOHPRINT001" is different to both "bohprint001", and "Bohprint001"- we have just about every variant imaginable

In any case, i've added in variables to account for the old print server in lower case, caps, and Title Case and it now (touch wood) seems to be working as intended - mapping everyone to the new server in a common fashion I hasten to add

So... thankyou ever so much for the advice offered. It's been very much appreciated although it turns out that the script wasn't at fault after all!!

Regards,

Kelvin.
Related Topics
Thread
Thread Starter
Forum
Replies
Last Post
m18use
Computer & Technology Related
4
05 September 2002 09:00 PM
Ricochet
Computer & Technology Related
10
26 August 2002 07:02 PM



Quick Reply: VB Script Help Please



All times are GMT +1. The time now is 12:53 AM.