+ Post New Thread
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
Windows Thread, Need some scripting help please in Technical; Hi all, I have got a .bat file which I want to run at log on, this is not a ...
  1. #1
    Mr_M_Cox's Avatar
    Join Date
    May 2007
    Location
    Portsmouth
    Posts
    155
    Thank Post
    5
    Thanked 1 Time in 1 Post
    Rep Power
    0

    Need some scripting help please

    Hi all,

    I have got a .bat file which I want to run at log on, this is not a problem.

    The problem is I only want to run it on certain PC's that have 'HP6' in the computer name. It is not an option to only apply the script to these PC's so I need the script to do some sort of 'if computername=...'

    Any ideas would be great

    Thanks

  2. #2

    Join Date
    Dec 2005
    Posts
    543
    Thank Post
    38
    Thanked 90 Times in 80 Posts
    Rep Power
    40
    Sorry its not a .bat file.... but it is .vbs and runs on logon

    Dim WSHNetwork
    Set WshNetwork = CreateObject("WScript.Network")
    Set WSHPrinters = WSHNetwork.EnumPrinterConnections
    'Find and remove old printers so as to have a clean system to work from
    For LOOP_COUNTER = 0 To WSHPrinters.Count - 1 Step 2
    If Left(WSHPrinters.Item(LOOP_COUNTER +1),15) = "\\dellserver\c3" Then
    WSHNetwork.RemovePrinterConnection WSHPrinters.Item(LOOP_COUNTER +1),True,True
    End If
    Next
    For LOOP_COUNTER = 0 To WSHPrinters.Count - 1 Step 2
    If Left(WSHPrinters.Item(LOOP_COUNTER +1),15) = "\\dellserver\c2" Then
    WSHNetwork.RemovePrinterConnection WSHPrinters.Item(LOOP_COUNTER +1),True,True
    End If
    Next
    For LOOP_COUNTER = 0 To WSHPrinters.Count - 1 Step 2
    If Left(WSHPrinters.Item(LOOP_COUNTER +1),15) = "\\dellserver\c4" Then
    WSHNetwork.RemovePrinterConnection WSHPrinters.Item(LOOP_COUNTER +1),True,True
    End If
    Next
    For LOOP_COUNTER = 0 To WSHPrinters.Count - 1 Step 2
    If Left(WSHPrinters.Item(LOOP_COUNTER +1),15) = "\\dellserver\c1" Then
    WSHNetwork.RemovePrinterConnection WSHPrinters.Item(LOOP_COUNTER +1),True,True
    End If
    Next
    'Add new printers dependent on Computername
    Set WshNetwork = CreateObject("WScript.Network")
    If UCase(Left(WshNetwork.ComputerName,2) = "C3") Then
    PrinterPath1 = "\\dellserver\c3hp4200b&w"
    WshNetwork.AddWindowsPrinterConnection PrinterPath1
    WshNetwork.SetDefaultPrinter PrinterPath1
    End If
    Set WshNetwork = CreateObject("WScript.Network")
    If UCase(Left(WshNetwork.ComputerName,2) = "C2") Then
    PrinterPath1 = "\\dellserver\c2hp4200b&w"
    PrinterPath2 = "\\dellserver\HP K550 Colour"
    WshNetwork.AddWindowsPrinterConnection PrinterPath1
    WshNetwork.AddWindowsPrinterConnection PrinterPath2
    WshNetwork.SetDefaultPrinter PrinterPath1
    End If
    Set WshNetwork = CreateObject("WScript.Network")
    If UCase(Left(WshNetwork.ComputerName,2) = "C1") Then
    PrinterPath1 = "\\dellserver\C1HP2420BW"
    PrinterPath2 = "\\dellserver\HP K550 Colour"
    WshNetwork.AddWindowsPrinterConnection PrinterPath1
    WshNetwork.AddWindowsPrinterConnection PrinterPath2
    WshNetwork.SetDefaultPrinter PrinterPath1
    End If
    Set WshNetwork = CreateObject("WScript.Network")
    If UCase(Left(WshNetwork.ComputerName,2) = "C4") Then
    PrinterPath1 = "\\dellserver\C4HP4200BW"
    PrinterPath2 = "\\dellserver\EPSONC84C4"
    WshNetwork.AddWindowsPrinterConnection PrinterPath1
    WshNetwork.AddWindowsPrinterConnection PrinterPath2
    WshNetwork.SetDefaultPrinter PrinterPath1
    End If
    Set WshNetwork = CreateObject("WScript.Network")
    If UCase(Left(WshNetwork.ComputerName,3) = "DT3") Then
    PrinterPath1 = "\\dt3server\hp4200"
    WshNetwork.AddWindowsPrinterConnection PrinterPath1
    WshNetwork.SetDefaultPrinter PrinterPath1
    End If
    Set WshNetwork = CreateObject("WScript.Network")
    If UCase(Left(WshNetwork.ComputerName,7) = "DT3PC21") Then
    PrinterPath1 = "\\dt3server\hp4200"
    PrinterPath2 = "\\dt3server\A3EPSON1400"
    WshNetwork.AddWindowsPrinterConnection PrinterPath1
    WshNetwork.AddWindowsPrinterConnection PrinterPath2
    WshNetwork.SetDefaultPrinter PrinterPath1
    End If
    If UCase(Left(WshNetwork.ComputerName,7) = "DT3PC22") Then
    PrinterPath1 = "\\dt3server\hp4200"
    PrinterPath2 = "\\dt3server\A3EPSON1400"
    WshNetwork.AddWindowsPrinterConnection PrinterPath1
    WshNetwork.AddWindowsPrinterConnection PrinterPath2
    WshNetwork.SetDefaultPrinter PrinterPath1
    End If
    If UCase(Left(WshNetwork.ComputerName,7) = "DT3PC23") Then
    PrinterPath1 = "\\dt3server\hp4200"
    PrinterPath2 = "\\dt3server\A3EPSON1400"
    WshNetwork.AddWindowsPrinterConnection PrinterPath1
    WshNetwork.AddWindowsPrinterConnection PrinterPath2
    WshNetwork.SetDefaultPrinter PrinterPath1
    End If
    If UCase(Left(WshNetwork.ComputerName,7) = "DT3PC24") Then
    PrinterPath1 = "\\dt3server\hp4200"
    PrinterPath2 = "\\dt3server\A3EPSON1400"
    WshNetwork.AddWindowsPrinterConnection PrinterPath1
    WshNetwork.AddWindowsPrinterConnection PrinterPath2
    WshNetwork.SetDefaultPrinter PrinterPath1
    End If
    If UCase(Left(WshNetwork.ComputerName,7) = "DT3PC25") Then
    PrinterPath1 = "\\dt3server\hp4200"
    PrinterPath2 = "\\dt3server\A3EPSON1400"
    WshNetwork.AddWindowsPrinterConnection PrinterPath1
    WshNetwork.AddWindowsPrinterConnection PrinterPath2
    WshNetwork.SetDefaultPrinter PrinterPath1
    End If
    If UCase(Left(WshNetwork.ComputerName,7) = "DT3PC26") Then
    PrinterPath1 = "\\dt3server\hp4200"
    PrinterPath2 = "\\dt3server\A3EPSON1400"
    WshNetwork.AddWindowsPrinterConnection PrinterPath1
    WshNetwork.AddWindowsPrinterConnection PrinterPath2
    WshNetwork.SetDefaultPrinter PrinterPath1
    End If

  3. #3
    Mr_M_Cox's Avatar
    Join Date
    May 2007
    Location
    Portsmouth
    Posts
    155
    Thank Post
    5
    Thanked 1 Time in 1 Post
    Rep Power
    0
    Thanks for the reply but WHOA! that is alot of code, do i really need it all, there are bits in there about printers and all sorts.

    If you could chop out the bit I can use that would be great

  4. #4
    browolf's Avatar
    Join Date
    Jun 2005
    Location
    Mars
    Posts
    1,529
    Thank Post
    108
    Thanked 89 Times in 75 Posts
    Blog Entries
    47
    Rep Power
    41
    u can do stuff like

    if %computername:~0,3% == 6R- (
    rundll32 printui.dll,PrintUIEntry /in /n\\SERVER2\6RColour
    rundll32 printui.dll,PrintUIEntry /y /n\\SERVER2\6RColour /q
    echo A6 Printer connected.
    )


    but this will only work if HP6 is a prefix

  5. #5
    Mr_M_Cox's Avatar
    Join Date
    May 2007
    Location
    Portsmouth
    Posts
    155
    Thank Post
    5
    Thanked 1 Time in 1 Post
    Rep Power
    0
    THe HP6 is a bit of the computer name which only exists on laptops not on desktops. I need the bat file to only run on HP laptops . THe structure of the name is ******HP6*****. Had to star it out for security reasons but you get the idea. letters before the HP and numbers after the 6.

    Does that make it any clearer?

  6. #6

    Join Date
    Dec 2005
    Posts
    543
    Thank Post
    38
    Thanked 90 Times in 80 Posts
    Rep Power
    40
    As browolf says... mine will only work with prefix computernames.

    What are the names of the computers that you want to select?

  7. #7

    LosOjos's Avatar
    Join Date
    Dec 2009
    Location
    West Midlands
    Posts
    5,578
    Thank Post
    1,473
    Thanked 1,227 Times in 835 Posts
    Rep Power
    753
    I've never tried it, but theoretically this should work:

    Code:
    set str=%COMPUTERNAME:HP6=%
    if not %COMPUTERNAME%==str then [run your commands here, easiest option is to call your original .bat at this point and keep this code in a seperate .bat file]
    the theory is it will remove any instances of "HP6" in %COMPUTERNAME%, saving the resulting string to str. If str is different to %COMPUTERNAME%, then it means "HP6" was found and removed, therefore the %COMPUTERNAME% contains "HP6"
    Last edited by LosOjos; 18th March 2010 at 01:29 PM.

  8. Thanks to LosOjos from:

    srochford (18th March 2010)

  9. #8
    apeo's Avatar
    Join Date
    Sep 2005
    Location
    Lost
    Posts
    1,612
    Thank Post
    95
    Thanked 115 Times in 111 Posts
    Rep Power
    42
    @siuko: WOW, personally i might want to tidy up the code. Like using a function or sub where to repace duplicate code.

    @Mr_M_Cox: Sorry again in vb but heres a script that will do something if HP6 is somewhere in the computername. BTW does your script do anything related to settting up printers?

    Code:
    Set objShell= WScript.CreateObject("WScript.Shell")
    
    Set ObjWshNetwork = WScript.CreateObject("WScript.Network")
    
    Dim strComputer
    
    strComputer = objShell.ExpandEnvironmentStrings("%COMPUTERNAME%")
    
    If instr(strComputer,"HP6") <> 0 Then
    	'Some bit of code here to do something
    End If

  10. #9
    browolf's Avatar
    Join Date
    Jun 2005
    Location
    Mars
    Posts
    1,529
    Thank Post
    108
    Thanked 89 Times in 75 Posts
    Blog Entries
    47
    Rep Power
    41
    there's more advanced things you can do with %computername:~0,3%

    variable substring
    CMD Variable edit replace

  11. #10
    Mr_M_Cox's Avatar
    Join Date
    May 2007
    Location
    Portsmouth
    Posts
    155
    Thank Post
    5
    Thanked 1 Time in 1 Post
    Rep Power
    0
    Quote Originally Posted by apeo View Post
    @siuko: WOW, personally i might want to tidy up the code. Like using a function or sub where to repace duplicate code.

    @Mr_M_Cox: Sorry again in vb but heres a script that will do something if HP6 is somewhere in the computername. BTW does your script do anything related to settting up printers?

    Code:
    Set objShell= WScript.CreateObject("WScript.Shell")
    
    Set ObjWshNetwork = WScript.CreateObject("WScript.Network")
    
    Dim strComputer
    
    strComputer = objShell.ExpandEnvironmentStrings("%COMPUTERNAME%")
    
    If instr(strComputer,"HP6") <> 0 Then
    	'Some bit of code here to do something
    End If
    Hi, thanks for the code, this looks to be exactly what I was after but what is the code to insert to call a .bat file?

  12. #11
    Mr_M_Cox's Avatar
    Join Date
    May 2007
    Location
    Portsmouth
    Posts
    155
    Thank Post
    5
    Thanked 1 Time in 1 Post
    Rep Power
    0
    and no, it does not relate to printers. I need to run a bat file which will call an HP bios setting editor so that I can change the bios settings of every HP laptop on the network

  13. #12
    apeo's Avatar
    Join Date
    Sep 2005
    Location
    Lost
    Posts
    1,612
    Thank Post
    95
    Thanked 115 Times in 111 Posts
    Rep Power
    42
    Quote Originally Posted by Mr_M_Cox View Post
    and no, it does not relate to printers. I need to run a bat file which will call an HP bios setting editor so that I can change the bios settings of every HP laptop on the network
    Ah cool, just wondered because earlier posts related to adding printers.

    To call a bat file or exe for that matter is:

    Code:
    Set objShell= WScript.CreateObject("WScript.Shell")
    
    Dim strComputer
    
    strComputer = objShell.ExpandEnvironmentStrings("%COMPUTERNAME%")
    
    If instr(strComputer,"HP6") <> 0 Then
    	'Call a bat file
    	objShell.run "c:\path to file\script.bat"
    End If
    BTW You should use local drives (i.e. NOT network paths e.g. "\\servername\share\script.bat") and i removed Set ObjWshNetwork = WScript.CreateObject("WScript.Network") because its not needed. I left it in the last time incase you might need it later but you dont.

    Hope that helps.

  14. #13
    Mr_M_Cox's Avatar
    Join Date
    May 2007
    Location
    Portsmouth
    Posts
    155
    Thank Post
    5
    Thanked 1 Time in 1 Post
    Rep Power
    0
    Thanks so much for your help on this.
    I now have

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

    Dim strComputer

    strComputer = objShell.ExpandEnvironmentStrings("%COMPUTERNAME%" )

    If instr(strComputer,"HP6") <> 0 Then
    'Call a bat file
    objShell.run "c:\hptest.bat"
    End If


    The problem i have now stems from the advice you gave be about UNC paths. I need it to either run from a network location using a UNC path or to map a drive first.

    I know I am asking a lot but how would I map the drive?

  15. #14
    Mr_M_Cox's Avatar
    Join Date
    May 2007
    Location
    Portsmouth
    Posts
    155
    Thank Post
    5
    Thanked 1 Time in 1 Post
    Rep Power
    0
    Dont worry about my last request. I managed to sort out the drive mapping myself.

    What would be nice is that if the PC name does not contain HP6 then it brings up a message box for 5 secs saying 'Patch does not apply to your PC' - bear in mind that the messaging service on all our PC's is disabled.

    any help much appreciated

  16. #15
    ronanian's Avatar
    Join Date
    Dec 2007
    Location
    Massachusetts, USA
    Posts
    88
    Thank Post
    18
    Thanked 2 Times in 2 Posts
    Rep Power
    14
    Quote Originally Posted by browolf View Post
    u can do stuff like
    {...}
    but this will only work if HP6 is a prefix
    If it's not a prefix, you could use grep.
    GREP for Windows
    Code:
    echo %COMPUTERNAME% | grep -i hp6 && goto doStuff || goto messageBox
    
    :doStuff
    call installPatch.bat
    
    :messageBox
    c:\messageBox.exe "Patch Does Not Apply"
    (Assumes you have a program called messageBox.exe, of course.)

    That will work in a .bat file instead of a VBScript.



SHARE:
+ Post New Thread
Page 1 of 2 12 LastLast

Similar Threads

  1. Learning Scripting
    By darkmanx in forum Windows Server 2000/2003
    Replies: 21
    Last Post: 24th February 2010, 09:58 PM
  2. New to scripting, need help
    By speckled in forum Scripts
    Replies: 1
    Last Post: 22nd November 2009, 09:09 AM
  3. AD Scripting Help needed
    By Grommit in forum Wireless Networks
    Replies: 2
    Last Post: 22nd October 2008, 09:05 PM
  4. os x scripting
    By mac_shinobi in forum Mac
    Replies: 5
    Last Post: 22nd August 2008, 10:30 AM
  5. New to scripting
    By FN-GM in forum Scripts
    Replies: 26
    Last Post: 10th August 2007, 11:31 PM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •