diff --git a/WindowsServiceManager/WindowsServiceManagerV4/Add-LocalUserToLogonAsAService.ps1 b/WindowsServiceManager/WindowsServiceManagerV4/Add-LocalUserToLogonAsAService.ps1 index 2c200ea..3c1c280 100644 --- a/WindowsServiceManager/WindowsServiceManagerV4/Add-LocalUserToLogonAsAService.ps1 +++ b/WindowsServiceManager/WindowsServiceManagerV4/Add-LocalUserToLogonAsAService.ps1 @@ -13,29 +13,20 @@ #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() } @@ -43,11 +34,10 @@ function Add-LocalUserToLogonAsAService { $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 diff --git a/WindowsServiceManager/WindowsServiceManagerV4/DeployWindowsService.ps1 b/WindowsServiceManager/WindowsServiceManagerV4/DeployWindowsService.ps1 index 586a12e..b986b2d 100644 --- a/WindowsServiceManager/WindowsServiceManagerV4/DeployWindowsService.ps1 +++ b/WindowsServiceManager/WindowsServiceManagerV4/DeployWindowsService.ps1 @@ -144,6 +144,7 @@ $scriptBlock = { $freshTopShelfInstall = $true } else { + Write-Output "[$env:ComputerName]: Start creating Service [$ServiceName]." if ($serviceStartupType -eq "Delayed") { $startupType = "Automatic" $delayed = $true @@ -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" } } @@ -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 \ No newline at end of file diff --git a/WindowsServiceManager/WindowsServiceManagerV4/utility.ps1 b/WindowsServiceManager/WindowsServiceManagerV4/utility.ps1 index fbbee8f..4af43ba 100644 --- a/WindowsServiceManager/WindowsServiceManagerV4/utility.ps1 +++ b/WindowsServiceManager/WindowsServiceManagerV4/utility.ps1 @@ -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 @@ -38,6 +40,7 @@ function Start-WindowsService { } function Stop-WindowsService { + [CmdletBinding()] param ( [string] @@ -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\*" } @@ -87,7 +90,8 @@ function Stop-WindowsService { function Get-FullExecuteablePath { [CmdletBinding()] - param ( + param + ( [string] $StringContainingPath, @@ -98,7 +102,7 @@ function Get-FullExecuteablePath { $matchPattern = '( |^)(?([a-zA-Z]):\\([\\\w\/.-]+)(.exe|.dll))|(( "|^")(?(([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]" }