Mr_M_Cox Posted March 18, 2010 Posted March 18, 2010 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
siuko Posted March 18, 2010 Posted March 18, 2010 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
Mr_M_Cox Posted March 18, 2010 Author Posted March 18, 2010 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
browolf Posted March 18, 2010 Posted March 18, 2010 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
Mr_M_Cox Posted March 18, 2010 Author Posted March 18, 2010 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?
siuko Posted March 18, 2010 Posted March 18, 2010 As browolf says... mine will only work with prefix computernames. What are the names of the computers that you want to select?
LosOjos Posted March 18, 2010 Posted March 18, 2010 (edited) I've never tried it, but theoretically this should work: 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" Edited March 18, 2010 by LosOjos 1
apeo Posted March 18, 2010 Posted March 18, 2010 @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? 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
browolf Posted March 18, 2010 Posted March 18, 2010 there's more advanced things you can do with %computername:~0,3% variable substring CMD Variable edit replace
Mr_M_Cox Posted March 18, 2010 Author Posted March 18, 2010 @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? 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?
Mr_M_Cox Posted March 18, 2010 Author Posted March 18, 2010 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
apeo Posted March 18, 2010 Posted March 18, 2010 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: 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.
Mr_M_Cox Posted March 18, 2010 Author Posted March 18, 2010 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?
Mr_M_Cox Posted March 18, 2010 Author Posted March 18, 2010 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
ronanian Posted March 18, 2010 Posted March 18, 2010 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 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.
apeo Posted March 18, 2010 Posted March 18, 2010 Not sure if the message will appear but try: Const wshOKDialog = 0 Const wshInformation = 64 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" Else intReturn = objShell.Popup("Patch does not apply to your PC", _ 5, "Patch", wshOKDialog + wshInformation) End If More info about popup function can be found: DevGuru WSH Method: WshShell.Popup
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now