Jump to content

How do I stop a service on a remote system?


bmw74
 Share

Recommended Posts

I am trying to deploy a file that is used by the service to the remote machine, so I am in need of attaching to a remote machine, check if a service is running, and shut it down before I deploy the file.

Right now the only way I have been able to do this is by using PSSERVICE, but maybe there is a better way to do it. Here is what I have coded so far to do this:

#include <zip.au3>
Global  $targetComputer = "[color="#0000FF"][default_computer][/color]"
Global  $username = "[color="#0000FF"][default_user][/color]"
Global  $password = "[color="#0000FF"][default_password][/color]"

deployFile($targetComputer)
Exit

Func deployFile ($targetComputer)
    Local  $targetShare = "\\" & $targetComputer & "\c$"
    Dim    $targetDrive

    DriveMapGet ("z:")
    If @error = 0 then
        DriveMapDel ("z:")
    EndIf
    
    DriveMapAdd ("z:", $targetShare, 0, $username, $password)

    If @error then
        Select
            Case @error = 1
                ConsoleWrite ("Error: Undefined / Other error. @extended set with Windows API return")
            Case @error = 2
                ConsoleWrite ("Error: Access to the remote share was denied")
            Case @error = 3
                ConsoleWrite ("Error: The device is already assigned")
            Case @error = 4
                ConsoleWrite ("Error: Invalid device name")
            Case @error = 5
                ConsoleWrite ("Error: Invalid remote share")
            Case @error = 6
                ConsoleWrite ("Error: Invalid password")
        EndSelect
    EndIf

    FileInstall ("[color="#0000FF"][source_file_path_goes_here][/color]", "z:\",1)

    _Zip_UnzipAll ("z:\file.zip", "z:\")

    Run ("psservice.exe \\" & $targetComputer & " stop [service_name_goes_here]")
    

    DriveMapDel ("z:")
Return
EndFunc

The [text inside brackets] needs to be replace for their respective values. Anyone with better ideas on how I can attach to a remote machine, check for a service, and stop/start/restart it?

Thanks everyone!

Link to comment
Share on other sites

Hi.

I'm using PSService, as you do.

I just do *NOT* connect to a share. I leave all current mappings untouched.

I connect to \\destinationPC\IPC$ (inter process communication) to attach to the destination PC.

Regards, Rudi.

Earth is flat, pigs can fly, and Nuclear Power is SAFE!

Link to comment
Share on other sites

Hi.

I'm using PSService, as you do.

I just do *NOT* connect to a share. I leave all current mappings untouched.

I connect to \\destinationPC\IPC$ (inter process communication) to attach to the destination PC.

Regards, Rudi.

I need to map a drive since I need to perform some file operations on the server after the service has been stopped... Can you do that with an IPC connection?

Also, do you mind sharing the portion of the code where you establish the connection and stop the services? Just want to compare and perhaps learn something new.

Thanks,

Carlos

Link to comment
Share on other sites

If you're on XP, try typing "sc" into a command prompt.

DESCRIPTION:
        SC is a command line program used for communicating with the
        NT Service Controller and services.
USAGE:
        sc <server> [command] [service name] <option1> <option2>...

        The option <server> has the form "\\ServerName"
        Further help on commands can be obtained by typing: "sc [command]"
        Commands:
          query-----------Queries the status for a service, or
                          enumerates the status for types of services.
          queryex---------Queries the extended status for a service, or
                          enumerates the status for types of services.
          start-----------Starts a service.
          pause-----------Sends a PAUSE control request to a service.
          interrogate-----Sends an INTERROGATE control request to a service.
          continue--------Sends a CONTINUE control request to a service.
          stop------------Sends a STOP request to a service.
          config----------Changes the configuration of a service (persistant).
          description-----Changes the description of a service.
          failure---------Changes the actions taken by a service upon failure.
          qc--------------Queries the configuration information for a service.
          qdescription----Queries the description for a service.
          qfailure--------Queries the actions taken by a service upon failure.
          delete----------Deletes a service (from the registry).
          create----------Creates a service. (adds it to the registry).
          control---------Sends a control to a service.
          sdshow----------Displays a service's security descriptor.
          sdset-----------Sets a service's security descriptor.
          GetDisplayName--Gets the DisplayName for a service.
          GetKeyName------Gets the ServiceKeyName for a service.
          EnumDepend------Enumerates Service Dependencies.

        The following commands don't require a service name:
        sc <server> <command> <option>
          boot------------(ok | bad) Indicates whether the last boot should
                          be saved as the last-known-good boot configuration
          Lock------------Locks the Service Database
          QueryLock-------Queries the LockStatus for the SCManager Database
EXAMPLE:
        sc start MyService

Would you like to see help for the QUERY and QUERYEX commands? [ y | n ]: n
Edited by exodius
Link to comment
Share on other sites

Hi.

I need to map a drive since I need to perform some file operations on the server after the service has been stopped... Can you do that with an IPC connection?

Connecting to $IPC standard share means to be *AUTHENTICATED*. By that you can do any operations for any data accessible by a remote share as if you would have a mapped drive pointing to some folder.

e.g:

$Source="\\server\share1\data"
$Destin="\\Backup\share2\databak"
$LocalLog="C:\MyRemoteBackupJob.txt"

$result=RunWait("robocopy " & $source & "\ " & $destin & "\ /mir /log:" & $localLog & " /r:0 /w:0 /np",@TempDir,@SW_HIDE)


#cs
    0×10 16 Serious error. Robocopy did not copy any files. This is either a usage error or an error due to insufficient access privileges on the source or destination directories.
    0×08 8 Some files or directories could not be copied (copy errors occurred and the retry limit was exceeded). Check these errors further.
    0×04 4 Some Mismatched files or directories were detected. Examine the output log. Housekeeping is probably necessary.
    0×02 2 Some Extra files or directories were detected. Examine the output log. Some housekeeping may be needed.
    0×01 1 One or more files were copied successfully (that is, new files have arrived).
    0×00 0 No errors occurred, and no copying was done. The source and destination directory trees are completely synchronized.

    If more than one condition is met, the hex exit codes are added. Exit Codes < 8 are usually regular results.
#ce

Regards, Rudi.

Earth is flat, pigs can fly, and Nuclear Power is SAFE!

Link to comment
Share on other sites

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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...