Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/add arguments to service #35

Merged
merged 8 commits into from
Jun 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ After installing the [Windows Service Manager](https://marketplace.visualstudio.
![Task Options](https://github.com/Dejulia489/WindowsServiceManager/blob/master/Images/TaskOptionsInstallation.png?raw=true "Installation Task Options")

1. **Install the windows service** - Enables service installation.
2. **Installation Path** - The path to the executable file.
2. **Start Command** - Command to start the executable including arguments. Containing path is also used to Install Artifacts.
3. **Run As Username** - The username the service should run as.
4. **Run As Password** - The password for the Run As Username. It can accept variable defined in Build/Release definitions as '$(passwordVariable)'. You may mark variable type as 'secret' to secure it.
5. **Install as a TopShelf Service** - Enables [TopShelf](https://github.com/Topshelf/Topshelf) installation.
Expand All @@ -63,6 +63,13 @@ After installing the [Windows Service Manager](https://marketplace.visualstudio.

## Release Notes

#### Version 4.4

Implemented support to set Displayname, Description and StartupType for non TopShelf services.
Implemented support for Command Arguments
Implemented support to install dotnet based Service
Fixed Permission issue (add RunAsService Permission while installation)

#### Version 4

Implemented support for installing [TopShelf](https://github.com/Topshelf/Topshelf) windows services.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<#
.SYNOPSIS
This cmdlet gives a user privileges to LogonAsAService.
.DESCRIPTION
This cmdlet gives a user privileges to LogonAsAService.
.PARAMETER User
The user name of the local user
.EXAMPLE
Add-LocalUserToLogonAsAService -user $UserName
#>

#written by Ingo Karstein, http://blog.karstein-consulting.com, # v1.0, 01/03/2014
#function added by Robin Carlsson, robintheswede.com, 2017-03-08

function Add-LocalUserToLogonAsAService {
[CmdletBinding()]
param (
[parameter(Mandatory = $true)]
[string]$user
)

PROCESS {
if ( [string]::IsNullOrEmpty($user) ) {
return Write-Error "no account specified"
}

$sidstr = $null
try {
$ntprincipal = new-object System.Security.Principal.NTAccount "$user"
$sid = $ntprincipal.Translate([System.Security.Principal.SecurityIdentifier])
$sidstr = $sid.Value.ToString()
}
catch {
$sidstr = $null
}

Write-Host "Account: $($user)" -ForegroundColor DarkCyan

if ( [string]::IsNullOrEmpty($sidstr) ) {
return Write-Error "Account not found!"
}

Write-Host "Account SID: $($sidstr)" -ForegroundColor DarkCyan

$tmp = [System.IO.Path]::GetTempFileName()

Write-Host "Export current Local Security Policy" -ForegroundColor DarkCyan
secedit.exe /export /cfg "$($tmp)"

$c = Get-Content -Path $tmp

$currentSetting = ""

foreach ($s in $c) {
if ( $s -like "SeServiceLogonRight*") {
$x = $s.split("=", [System.StringSplitOptions]::RemoveEmptyEntries)
$currentSetting = $x[1].Trim()
}
}

if ( $currentSetting -notlike "*$($sidstr)*" ) {
Write-Host "Modify Setting ""Logon as a Service""" -ForegroundColor DarkCyan

if ( [string]::IsNullOrEmpty($currentSetting) ) {
$currentSetting = "*$($sidstr)"
}
else {
$currentSetting = "*$($sidstr),$($currentSetting)"
}

Write-Host "$currentSetting"

$outfile = @"
[Unicode]
Unicode=yes
[Version]
signature="`$CHICAGO`$"
Revision=1
[Privilege Rights]
SeServiceLogonRight = $($currentSetting)
"@

$tmp2 = [System.IO.Path]::GetTempFileName()


Write-Host "Import new settings to Local Security Policy" -ForegroundColor DarkCyan
$outfile | Set-Content -Path $tmp2 -Encoding Unicode -Force

#notepad.exe $tmp2
Push-Location (Split-Path $tmp2)

try {
secedit.exe /configure /db "secedit.sdb" /cfg "$($tmp2)" /areas USER_RIGHTS
#write-host "secedit.exe /configure /db ""secedit.sdb"" /cfg ""$($tmp2)"" /areas USER_RIGHTS "
}
finally {
Pop-Location
}
}
else {
Write-Host "NO ACTIONS REQUIRED! Account already in ""Logon as a Service""" -ForegroundColor DarkCyan
}

Write-Host "Done." -ForegroundColor DarkCyan
}
}
#Export-ModuleMember -Function Add-LocalUserToLogonAsAService
Loading