Skip to content

Commit

Permalink
fixed some issues
Browse files Browse the repository at this point in the history
  • Loading branch information
DarkChaos committed Jun 17, 2020
1 parent 8be88c1 commit 8c7902a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,41 +13,31 @@
#function added by Robin Carlsson, robintheswede.com, 2017-03-08

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

PROCESS {
param($accountToAdd)
#written by Ingo Karstein, http://blog.karstein-consulting.com
# v1.0, 01/03/2014

## <--- Configure here

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

## ---> End of Config

$sidstr = $null
try {
$ntprincipal = new-object System.Security.Principal.NTAccount "$accountToAdd"
$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: $($accountToAdd)" -ForegroundColor DarkCyan
Write-Host "Account: $($user)" -ForegroundColor DarkCyan

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

Write-Host "Account SID: $($sidstr)" -ForegroundColor DarkCyan
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ $scriptBlock = {
$freshTopShelfInstall = $true
}
else {
Write-Output "[$env:ComputerName]: Start creating Service [$ServiceName]."
if ($serviceStartupType -eq "Delayed") {
$startupType = "Automatic"
$delayed = $true
Expand All @@ -157,18 +158,27 @@ $scriptBlock = {
Name = $ServiceName
BinaryPathName = $startCommand
DisplayName = $serviceDisplayName
Description = $serviceDescription
StartupType = $startupType
}

# add Description just if Descripion is provided to prevent Parameter null or empty Exception
if($serviceDescription) {
Write-Output "[$env:ComputerName]: Adding Description [$serviceDescription]"
$newServiceSplat.Description = $serviceDescription
}

if ($runAsCredential) {
Write-Output "[$env:ComputerName]: Setting RunAsCredentials"
$newServiceSplat.Credential = $runAsCredential
# load Function
. "$PSScriptRoot\Add-LocalUserToLogonAsAService.ps1"
Add-LocalUserToLogonAsAService -user $runAsCredential
Add-LocalUserToLogonAsAService -user $runAsCredential.UserName
}
$newService = New-Service @newServiceSplat
$newService = New-Service @newServiceSplat
Write-Output "[$env:ComputerName]: Service [$ServiceName] created."

if ($delayed) {
Write-Output "[$env:ComputerName]: Set [$ServiceName] to Delayed start"
Start-Process -FilePath sc.exe -ArgumentList "config ""$ServiceName"" start=delayed-auto"
}
}
Expand Down Expand Up @@ -255,5 +265,5 @@ if ($useSSL) {
$invokeCommandSplat.UseSSL = $true
}

Invoke-Command @invokeCommandSplat -ArgumentList $ServiceName, $serviceDisplayName, $serviceDescription, $serviceStartupType, $TimeOut, $StopProcess, $CleanInstall, $ArtifactPath, $installationPath, $runAsCredential, $installTopShelfService, $instanceName, $installArguments, $startService
Invoke-Command @invokeCommandSplat -ArgumentList $ServiceName, $serviceDisplayName, $serviceDescription, $serviceStartupType, $TimeOut, $StopProcess, $CleanInstall, $ArtifactPath, $installationPath, $startCommand, $runAsCredential, $installTopShelfService, $instanceName, $installArguments, $startService
Trace-VstsLeavingInvocation $MyInvocation
16 changes: 10 additions & 6 deletions WindowsServiceManager/WindowsServiceManagerV4/utility.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,22 @@ function Get-NewPSSessionOption {
$commandString = "New-PSSessionOption $arguments"
Write-Verbose "New-PSSessionOption command: $commandString"
return (Invoke-Expression -Command $commandString)
} finally {
}
finally {
Trace-VstsLeavingInvocation $MyInvocation
}
}

function Get-WindowsService {
param
(
[CmdletBinding()]
param (
$ServiceName
)
return Get-WmiObject -Class Win32_Service | Where-Object { $PSItem.Name -eq $ServiceName }
}

function Start-WindowsService {
[CmdletBinding()]
param
(
$ServiceName
Expand All @@ -38,6 +40,7 @@ function Start-WindowsService {
}

function Stop-WindowsService {
[CmdletBinding()]
param
(
[string]
Expand All @@ -63,7 +66,7 @@ function Stop-WindowsService {
if ($StopProcess) {
Write-Verbose "[$env:ComputerName]: [$ServiceName] did not respond within [$Timeout] seconds, stopping process."

$parentPath = Get-FullExecuteablePath -StringContainingPath $serviceObject.PathName -JustParentPath
$parentPath = Get-FullExecuteablePath -StringContainingPath $serviceObject.PathName -JustParentPath

$allProcesses = Get-Process
$process = $allProcesses | Where-Object { $_.Path -like "$parentPath\*" }
Expand All @@ -87,7 +90,8 @@ function Stop-WindowsService {

function Get-FullExecuteablePath {
[CmdletBinding()]
param (
param
(
[string]
$StringContainingPath,

Expand All @@ -98,7 +102,7 @@ function Get-FullExecuteablePath {
$matchPattern = '( |^)(?<path>([a-zA-Z]):\\([\\\w\/.-]+)(.exe|.dll))|(( "|^")(?<path2>(([a-zA-Z]):\\([\\\w\/. -]+)(.exe|.dll)))(" |"$))'

# check if PathName can be processed
if($StringContainingPath -notmatch $matchPattern) {
if ($StringContainingPath -notmatch $matchPattern) {
return Write-Error -Message "String can't be parsed. The StringContainingPath parameter should contain a valid Path ending with an '.exe' or '.dll'. Current string [$StringContainingPath]"
}

Expand Down

0 comments on commit 8c7902a

Please sign in to comment.