locked
Script to find a user and restart two services RRS feed

  • Question

  • I am looking to make a script to first find a user that is connected to a server (list) the to stop Spooler, then stop cpsvc. Once the services are stopped, the script would then clear out the folder on "C:\Windows\System32\spool\PRINTERS" Directory. Once that was done, the script would then start Spooler and cpsvc. At the end it would echo the services were restarted and to try to print now.

    

    

    Lee Mossolle

    • Moved by Bill_Stewart Tuesday, June 17, 2014 5:52 PM Abandoned
    Wednesday, February 26, 2014 2:02 PM

Answers

  • A user cannot stop and start services and cannot delete files in the system folders.

    There is no need to look for the services in that way  You will have to do this remotely.  Just find the services if they exisit and stop them then remotely delete the contents of the spooler folder.

    PowerShell remoting would be the easies way to do this.

    strComputer = WScript.Arguments(0)
    WScript.Echo "Running Against Remote Computer Named: " & strComputer
    
    set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
    Set colListOfServices = objWMIService.ExecQuery("Select * from Win32_Service Where Name = 'cpsvc' OR Name = 'spooler'")
    
    for Each objService in colListOfServices
        objService.StopService()
        objService.StartService()
    Next
    
    

    Until you understand how yor code will work avoid over coding.  It just adds confusion and errors.   The above is functionally equivalent to what you posted. 

    I suspect there may be a dependency between cpsvc and spooler.  In that casr yo can only kill them in the correct order.  This will require to loops of a more sophisticated scripting approach.

    We can also use the 'Forse' argument and retriev the master service (spooler) and force it to stop all dependent services.  We would then retrive all services and start them. We can also just retrieve the dependency list and use that.

    The code is simple and will allow the dependencies to be added if needed.

    You can also use PowerShell which is much easier.

    Get-Service spooler -computer remotepc | Stop-Service -force
    Get-Service spooler -computer remotepc | Start-Service

    We can wrap this in a function for convenience.


    ¯\_(ツ)_/¯

    • Marked as answer by lmossolle Monday, November 25, 2019 6:05 PM
    Wednesday, February 26, 2014 4:08 PM
    • Marked as answer by lmossolle Monday, November 25, 2019 6:05 PM
    Wednesday, February 26, 2014 2:21 PM
  • I am trying to paste what I came up with but I am not able to copy and paste in here/

    Try using the 'Insert Code Block' button in the editor. That should pop up a code window you can paste in.

    Don't retire TechNet! - (Don't give up yet - 12,700+ strong and growing)

    • Marked as answer by lmossolle Monday, November 25, 2019 6:05 PM
    Wednesday, February 26, 2014 2:29 PM
  • option explicit
    '
    ' Kill all processes by the name of strProcessName running under the current user
    '
    dim strComputer,strProcessName, strOwner, strUserName
    dim objWMI, objNetwork, colProcesses, dummy, objProcess
     
    strComputer = WScript.Arguments(0)
    'strComputer = WScript.Arguments
    WScript.Echo "Running Against Remote Computer Named: " & strComputer
    Dim oShell
    Dim UserName
    
    Set oShell = Wscript.CreateObject("Wscript.Shell")
    UserName = oShell.ExpandEnvironmentStrings("%USERNAME%")
    
    WScript.Echo "Looking for Microsoft Word for the following loged in user: " &  UserName
    
    
    servList = Array("cpsvc", "Spooler")
     
    set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
    set objNetwork = CreateObject("WScript.Network")
    Set colListOfServices = objWMIService.ExecQuery ("Select * from Win32_Service Where Name = '"& Join(servList,"' OR Name = '") & "'")
     
    strUserName = objNetwork.UserName
    for Each objService in colListOfServices
        objService.StopService()
        WScript.Sleep 1500
        objService.StartService()
    Next
    MsgBox("Citrix Print Manager and the Print Spooler have restarted")
    next
     
    set objWMI = nothing
    set objNetwork = nothing
    set objProcess = nothing


    Lee Mossolle

    • Marked as answer by lmossolle Monday, November 25, 2019 6:05 PM
    Wednesday, February 26, 2014 2:46 PM

All replies

    • Marked as answer by lmossolle Monday, November 25, 2019 6:05 PM
    Wednesday, February 26, 2014 2:21 PM
  • I am trying to paste what I came up with but I am not able to copy and paste in here/

    

    Lee Mossolle

    Wednesday, February 26, 2014 2:25 PM
  • I am trying to paste what I came up with but I am not able to copy and paste in here/

    Try using the 'Insert Code Block' button in the editor. That should pop up a code window you can paste in.

    Don't retire TechNet! - (Don't give up yet - 12,700+ strong and growing)

    • Marked as answer by lmossolle Monday, November 25, 2019 6:05 PM
    Wednesday, February 26, 2014 2:29 PM
  • option explicit
    '
    ' Kill all processes by the name of strProcessName running under the current user
    '
    dim strComputer,strProcessName, strOwner, strUserName
    dim objWMI, objNetwork, colProcesses, dummy, objProcess
     
    strComputer = WScript.Arguments(0)
    'strComputer = WScript.Arguments
    WScript.Echo "Running Against Remote Computer Named: " & strComputer
    Dim oShell
    Dim UserName
    
    Set oShell = Wscript.CreateObject("Wscript.Shell")
    UserName = oShell.ExpandEnvironmentStrings("%USERNAME%")
    
    WScript.Echo "Looking for Microsoft Word for the following loged in user: " &  UserName
    
    
    servList = Array("cpsvc", "Spooler")
     
    set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
    set objNetwork = CreateObject("WScript.Network")
    Set colListOfServices = objWMIService.ExecQuery ("Select * from Win32_Service Where Name = '"& Join(servList,"' OR Name = '") & "'")
     
    strUserName = objNetwork.UserName
    for Each objService in colListOfServices
        objService.StopService()
        WScript.Sleep 1500
        objService.StartService()
    Next
    MsgBox("Citrix Print Manager and the Print Spooler have restarted")
    next
     
    set objWMI = nothing
    set objNetwork = nothing
    set objProcess = nothing


    Lee Mossolle

    • Marked as answer by lmossolle Monday, November 25, 2019 6:05 PM
    Wednesday, February 26, 2014 2:46 PM
  • So what's your question?

    Bill

    Wednesday, February 26, 2014 3:50 PM
  • A user cannot stop and start services and cannot delete files in the system folders.

    There is no need to look for the services in that way  You will have to do this remotely.  Just find the services if they exisit and stop them then remotely delete the contents of the spooler folder.

    PowerShell remoting would be the easies way to do this.

    strComputer = WScript.Arguments(0)
    WScript.Echo "Running Against Remote Computer Named: " & strComputer
    
    set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
    Set colListOfServices = objWMIService.ExecQuery("Select * from Win32_Service Where Name = 'cpsvc' OR Name = 'spooler'")
    
    for Each objService in colListOfServices
        objService.StopService()
        objService.StartService()
    Next
    
    

    Until you understand how yor code will work avoid over coding.  It just adds confusion and errors.   The above is functionally equivalent to what you posted. 

    I suspect there may be a dependency between cpsvc and spooler.  In that casr yo can only kill them in the correct order.  This will require to loops of a more sophisticated scripting approach.

    We can also use the 'Forse' argument and retriev the master service (spooler) and force it to stop all dependent services.  We would then retrive all services and start them. We can also just retrieve the dependency list and use that.

    The code is simple and will allow the dependencies to be added if needed.

    You can also use PowerShell which is much easier.

    Get-Service spooler -computer remotepc | Stop-Service -force
    Get-Service spooler -computer remotepc | Start-Service

    We can wrap this in a function for convenience.


    ¯\_(ツ)_/¯

    • Marked as answer by lmossolle Monday, November 25, 2019 6:05 PM
    Wednesday, February 26, 2014 4:08 PM
  • Actually - if there are no dependencies then this is the fastest method:

    get-service spooler -ComputerName remotepc |Restart-Service

    In many cases restarting the service will unlock the hung file. You can then delete it. 

    Get-ChildItem \\remotepc\c$\Windows\System32\spool\PRINTERS\* | Remove-Item 

    With dependencies we would need to create two enfolded loops to handle the issues.  This would be true to a greater degree in VBScript.


    ¯\_(ツ)_/¯

    Wednesday, February 26, 2014 4:14 PM