Jump to content

Script to run based on computer name


Recommended Posts

Posted

Hi,

 

I want a login script to run only when logged onto computers begging with FN. If the computer begins with something else i would like the script to quit.

 

I am maybe thinking about something like this

 

if (Left(computerName, 2) = "FN") then

WScript.Echo("yes it is")

end if

wscript.quit

 

But this doesnt work.

 

Does anybody have any ideas please?

 

Thanks

Posted

Try this instead:

 

Set WshNetwork = WScript.CreateObject("WScript.Network")

If (Left(WshNetwork.ComputerName, 2) = "FN") Then

'commands to run go here

End If

  • Thanks 1
Posted

I should start agian really. I wasnt this pritner script to run. Its for mapping printer in a terminal session for thin clients. Its a login script. I tried loopback for whatever reason it wont run.

 

So i am thinking if i set it as a login script it will pickup the name and run the script. If not it will ignore it.

 

This is the script

 

' Script to add network printers to workstations and thin clients

 

 

 

' Declare variables and enumerate existing printer connections

 

On Error Resume Next

 

 

 

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

 

Set WshNetwork = WScript.CreateObject("WScript.Network")

 

Set oPrinters = WshNetwork.EnumPrinterConnections

 

 

 

Dim computerName

 

computerName = LCase(WshNetwork.ComputerName)

 

 

 

' Delete existing connections to network printers

 

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

 

On Error Resume Next

 

if Left(oPrinters.Item(i), 3) <> "lpt" And Left(oPrinters.Item(i), 3) <> "usb" then

 

WshNetwork.RemovePrinterConnection oPrinters.Item(i+1), true, true

 

else WScript.Echo "No network printers found"

 

end if

 

Next

 

 

 

' Citrix specific section

 

if (Left(computerName, 2) = "fn") then

 

computerName = LCase(WshShell.ExpandEnvironmentStrings("%CLIENTNAME%"))

 

end if

 

 

 

' Add printer connections dependant upon location

 

' Variable to select print server

 

Dim printServer

 

printServer = "kp-print"

 

 

 

Select Case (Left(computerName, 4))

 

Case "wt00"

 

WshNetwork.AddWindowsPrinterConnection "\\" & printServer & "\v205mono"

 

WshNetwork.SetDefaultPrinter "\\" & printServer & "\v205mono"

 

End Select

Posted (edited)

You can use the %COMPUTERNAME% variable in a cmd file to call the vbs with something like

 

IF /i %COMPUTERNAME%==FN THEN (call wscript script.vbs) ELSE GOTO :EOF

Edited by somabc
  • Thanks 1
Posted

This is what ours looks like. I never got it to work via GPO/login, instead as it is only needed on our Terminal Servers I have it running under the startup folder.

 

' Script to add network printers to workstations and thin clients

 

 

' Author: Ric Charlton

 

 

 

' Declare variables and enumerate existing printer connections

 

' Msgbox ("Script Starting")

 

On Error Resume Next

 

 

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

Set WshNetwork = WScript.CreateObject("WScript.Network")

Set oPrinters = WshNetwork.EnumPrinterConnections

 

 

 

Dim computerName

 

computerName = LCase(WshNetwork.ComputerName)

 

' Msgbox ("Computer name found: " & computername)

 

' Delete existing connections to network printers

 

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

 

On Error Resume Next

 

if Left(oPrinters.Item(i), 3) <> "lpt" And Left(oPrinters.Item(i), 3) <> "usb" then

 

WshNetwork.RemovePrinterConnection oPrinters.Item(i+1), true, true

 

else WScript.Echo "No network printers found"

 

end if

 

Next

 

 

' Citrix specific section

 

if (Left(computerName, 5) = "mcsts") then

 

computerName = LCase(WshShell.ExpandEnvironmentStrings("%CLIENTNAME%"))

' Msgbox ("Citrix Computer name found: " & computername)

 

end if

 

 

 

 

' Add printer connections dependant upon location

 

' Variable to select print server

 

Dim printServer

 

printServer = "bart"

 

 

 

Select Case (Left(computerName, 3))

 

Case "lrc"

 

 

 

WshNetwork.AddWindowsPrinterConnection "\\" & printServer & "\lrclaser"

 

WshNetwork.SetDefaultPrinter "\\" & printServer & "\lrclaser"

 

Case "h05"

 

WshNetwork.AddWindowsPrinterConnection "\\" & printServer & "\h05laser"

 

WshNetwork.SetDefaultPrinter "\\" & printServer & "\h05laser"

 

Case "t03"

 

 

WshNetwork.AddWindowsPrinterConnection "\\" & printServer & "\t03laser"

 

WshNetwork.SetDefaultPrinter "\\" & printServer & "\t03laser"

 

Case "t04"

 

WshNetwork.AddWindowsPrinterConnection "\\" & printServer & "\t04laser"

 

WshNetwork.SetDefaultPrinter "\\" & printServer & "\t04laser"

 

Case "m01"

 

 

WshNetwork.AddWindowsPrinterConnection "\\" & printServer & "\m01laser"

 

WshNetwork.SetDefaultPrinter "\\" & printServer & "\m01laser"

 

Case "ca1"

 

 

WshNetwork.AddWindowsPrinterConnection "\\" & printServer & "\ca1laser"

 

WshNetwork.SetDefaultPrinter "\\" & printServer & "\ca1laser"

End Select

' MsgBox ("Adding printer \ set default " & "\\" & printServer & "\lrclaser")

Posted (edited)
This is what ours looks like. I never got it to work via GPO/login, instead as it is only needed on our Terminal Servers I have it running under the startup folder.

 

Thats a good point

 

How do you go on with the printer being left in peoples profiles as they logoff?

 

So all i need now is a script that will delete all network printers if the computer begins with FN. I can run this as a logoff script. Does anyone have one handy please?

Edited by FN-GM
Posted

Here's our script that we use and got from a thread on here.

 

' Script to add network printers to workstations for staff
'
' Author: Ric Charlton, 07/07/04 (amended 10/02/11 by Derek Troughton)
'                                     
' Declare variables and enumerate existing printer connections
On Error Resume Next

Set WshShell = WScript.CreateObject("WScript.Shell")
Set WshNetwork = WScript.CreateObject("WScript.Network")
Set oPrinters = WshNetwork.EnumPrinterConnections

Dim computerName
computerName = LCase(WshNetwork.ComputerName)


' Degugging line> WScript.Echo "ComputerName variable: " & computerName

' Delete existing connections to network printers
For i = 0 to oPrinters.Count - 1 Step 2
           On Error Resume Next
	if Left(oPrinters.Item(i), 3) <> "lpt" And Left(oPrinters.Item(i), 3) <> "usb" then
            	WshNetwork.RemovePrinterConnection oPrinters.Item(i+1), true, true
           else WScript.Echo "No network printers found"
           end if
Next

' Add printer connections dependant upon location
Select Case (Left(computerName, 3))
Case "lrc"
	WshNetwork.AddWindowsPrinterConnection "\\norden-printers\LRC-Mono"
	WshNetwork.SetDefaultPrinter "\\norden-printers\LRC-Mono"
Case "lib"
	WshNetwork.AddWindowsPrinterConnection "\\norden-printers\LRC-Mono"
	WshNetwork.SetDefaultPrinter "\\norden-printers\LRC-Mono"
Case "lea"
	WshNetwork.AddWindowsPrinterConnection "\\norden-printers\LRC-Mono"
	WshNetwork.SetDefaultPrinter "\\norden-printers\LRC-Mono"
Case "MED"
	WshNetwork.AddWindowsPrinterConnection "\\norden-printers\English-Mono"
	WshNetwork.SetDefaultPrinter "\\norden-printers\English-Mono"
Case "rep"
	WshNetwork.AddWindowsPrinterConnection "\\norden-printers\AB-Colour"
	WshNetwork.SetDefaultPrinter "\\norden-printers\AB-Colour"

Case Else
	' WScript.Echo "No default printers added"
End Select

 

This checks the first 3 letters of the computer name and adds the printer we specify. If the computer name is not listed then they don't get a network printer.

  • Thanks 2
Posted
Forgot to say, we set this in GPO on logon, it removes networked printers, but leaves local printers and re-attaches any network printers.
Posted
Forgot to say, we set this in GPO on logon, it removes networked printers, but leaves local printers and re-attaches any network printers.

 

The thing is i only want the whole script to run on terminal servers. I dont want it to mess with our existing setup. I dont want it to remvoe printers on the other computers.

Posted
Thats a good point

 

How do you go on with the printer being left in peoples profiles as they logoff?

 

So all i need now is a script that will delete all network printers if the computer begins with FN. I can run this as a logoff script. Does anyone have one handy please?

 

Our users have mandatory profiles on the terminal servers (or at least they did, I spotted yesterday that it's not working......), I haven't got around to getting profiles setup on the PCs for users so they just have a local profile on each PC, never had an issue with printing/profiles.

  • 2 years later...
Posted

Thanks Dezt - your script works a treat for me I just needed to mod the 3 to a 4 as use four digits for our room names......

 

P.S I know who you are ex Lanway man ;-) lol

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 account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...