Thursday 16 January 2020

Deploying the Window Services using Power shell

Log on as a service rights

To establish Log on as a service rights for a service user account:
  1. Open the Local Security Policy editor by running secpol.msc.
  2. Expand the Local Policies node and select User Rights Assignment.
  3. Open the Log on as a service policy.
  4. Select Add User or Group.
  5. Provide the object name (user account) using either of the following approaches:
    1. Type the user account ({DOMAIN OR COMPUTER NAME\USER}) in the object name field and select OK to add the user to the policy.
    2. Select Advanced. Select Find Now. Select the user account from the list. Select OK. Select OK again to add the user to the policy.
  6. Select OK or Apply to accept the changes.

Create and manage the Windows Service

Create a service

Use PowerShell commands to register a service. From an administrative PowerShell 6 command shell, execute the following commands:
$acl = Get-Acl "{EXE PATH}"
$aclRuleArgs = {DOMAIN OR COMPUTER NAME\USER}, "Read,Write,ReadAndExecute", "ContainerInherit,ObjectInherit", "None", "Allow"
$accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule($aclRuleArgs)
$acl.SetAccessRule($accessRule)
$acl | Set-Acl "{EXE PATH}"

New-Service -Name {SERVICE NAME} -BinaryPathName {EXE FILE PATH} -Credential {DOMAIN OR COMPUTER NAME\USER} -Description "{DESCRIPTION}" -DisplayName "{DISPLAY NAME}" -StartupType Automatic
  • {EXE PATH} – Path to the app's folder on the host (for example, d:\myservice). Don't include the app's executable in the path. A trailing slash isn't required.
  • {DOMAIN OR COMPUTER NAME\USER} – Service user account (for example, Contoso\ServiceUser).
  • {SERVICE NAME} – Service name (for example, MyService).
  • {EXE FILE PATH} – The app's executable path (for example, d:\myservice\myservice.exe). Include the executable's file name with extension.
  • {DESCRIPTION} – Service description (for example, My sample service).
  • {DISPLAY NAME} – Service display name (for example, My Service).

Start a service

Start a service with the following PowerShell 6 command:
Start-Service -Name {SERVICE NAME}
The command takes a few seconds to start the service.

Determine a service's status

To check the status of a service, use the following PowerShell 6 command:
Get-Service -Name {SERVICE NAME}
The status is reported as one of the following values:
  • Starting
  • Running
  • Stopping
  • Stopped

Stop a service

Stop a service with the following Powershell 6 command:
Stop-Service -Name {SERVICE NAME}

Remove a service

After a short delay to stop a service, remove a service with the following Powershell 6 command:
Remove-Service -Name {SERVICE NAME}

sc.exe delete {SERVICE NAME}

No comments:

Post a Comment

Hide New... button on lookup controls in model-driven apps

  The 'New ...' button is shown upon opening the lookup search dialog whenever the logged in user has at least user create privileg...