+ Post New Thread
Results 1 to 13 of 13
Scripts Thread, Script to run based on computer name in Coding and Web Development; Hi, I want a login script to run only when logged onto computers begging with FN. If the computer begins ...
  1. #1

    FN-GM's Avatar
    Join Date
    Jun 2007
    Location
    UK
    Posts
    15,386
    Thank Post
    797
    Thanked 1,585 Times in 1,388 Posts
    Blog Entries
    10
    Rep Power
    427

    Script to run based on computer name

    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

    Code:
    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

  2. #2
    joe90bass's Avatar
    Join Date
    Oct 2007
    Location
    S Wales
    Posts
    1,345
    Thank Post
    316
    Thanked 106 Times in 95 Posts
    Rep Power
    50
    Can this be done with item targetting in group policy preferences?

  3. Thanks to joe90bass from:

    FN-GM (15th February 2011)

  4. #3

    LosOjos's Avatar
    Join Date
    Dec 2009
    Location
    West Midlands
    Posts
    5,182
    Thank Post
    1,285
    Thanked 1,029 Times in 729 Posts
    Rep Power
    658
    Try this instead:

    Code:
    Set WshNetwork = WScript.CreateObject("WScript.Network")
    
    If (Left(WshNetwork.ComputerName, 2) = "FN") Then
     
    'commands to run go here
    
    End If

  5. Thanks to LosOjos from:

    FN-GM (15th February 2011)

  6. #4

    FN-GM's Avatar
    Join Date
    Jun 2007
    Location
    UK
    Posts
    15,386
    Thank Post
    797
    Thanked 1,585 Times in 1,388 Posts
    Blog Entries
    10
    Rep Power
    427
    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("%CLIENTNA ME%"))

    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

  7. #5
    somabc's Avatar
    Join Date
    Oct 2007
    Location
    London
    Posts
    2,337
    Thank Post
    83
    Thanked 388 Times in 258 Posts
    Rep Power
    111
    You can use the %COMPUTERNAME% variable in a cmd file to call the vbs with something like

    Code:
    IF /i %COMPUTERNAME%==FN THEN (call wscript script.vbs) ELSE GOTO :EOF
    Last edited by somabc; 15th February 2011 at 12:47 PM.

  8. Thanks to somabc from:

    FN-GM (15th February 2011)

  9. #6
    joe90bass's Avatar
    Join Date
    Oct 2007
    Location
    S Wales
    Posts
    1,345
    Thank Post
    316
    Thanked 106 Times in 95 Posts
    Rep Power
    50
    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("%CLIENTNA ME%"))
    ' 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")

  10. #7

    FN-GM's Avatar
    Join Date
    Jun 2007
    Location
    UK
    Posts
    15,386
    Thank Post
    797
    Thanked 1,585 Times in 1,388 Posts
    Blog Entries
    10
    Rep Power
    427
    Quote Originally Posted by joe90bass View Post
    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?
    Last edited by FN-GM; 15th February 2011 at 12:42 PM.

  11. #8
    dezt's Avatar
    Join Date
    Dec 2005
    Location
    Lancs
    Posts
    1,009
    Thank Post
    153
    Thanked 55 Times in 43 Posts
    Rep Power
    28
    Here's our script that we use and got from a thread on here.

    Code:
    ' 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.

  12. 2 Thanks to dezt:

    FN-GM (15th February 2011), MuttsNutts (10th July 2013)

  13. #9
    dezt's Avatar
    Join Date
    Dec 2005
    Location
    Lancs
    Posts
    1,009
    Thank Post
    153
    Thanked 55 Times in 43 Posts
    Rep Power
    28
    Forgot to say, we set this in GPO on logon, it removes networked printers, but leaves local printers and re-attaches any network printers.

  14. #10

    FN-GM's Avatar
    Join Date
    Jun 2007
    Location
    UK
    Posts
    15,386
    Thank Post
    797
    Thanked 1,585 Times in 1,388 Posts
    Blog Entries
    10
    Rep Power
    427
    Quote Originally Posted by dezt View Post
    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.

  15. #11
    dezt's Avatar
    Join Date
    Dec 2005
    Location
    Lancs
    Posts
    1,009
    Thank Post
    153
    Thanked 55 Times in 43 Posts
    Rep Power
    28
    Ah sorry, didn't notice the terminal client bit.

  16. #12
    joe90bass's Avatar
    Join Date
    Oct 2007
    Location
    S Wales
    Posts
    1,345
    Thank Post
    316
    Thanked 106 Times in 95 Posts
    Rep Power
    50
    Quote Originally Posted by FN-GM View Post
    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.

  17. #13

    Join Date
    Oct 2012
    Location
    Padiham
    Posts
    7
    Thank Post
    1
    Thanked 1 Time in 1 Post
    Rep Power
    0
    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

SHARE:
+ Post New Thread

Similar Threads

  1. Replies: 9
    Last Post: 6th October 2010, 03:54 PM
  2. VBS - wshell.run based on file contents?
    By ahuxham in forum Scripts
    Replies: 14
    Last Post: 17th November 2009, 03:10 PM
  3. Replies: 0
    Last Post: 7th May 2008, 12:13 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
  •