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/refactor #50

Open
wants to merge 5 commits into
base: feature/refactor
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ $UTILITY_PATH = Join-Path -Path $PSScriptRoot -ChildPath 'ps_modules\WindowsServ

if ($DeploymentType -eq 'Agent')
{
Write-Host ("Begining deployment to [{0}]" -f ($Machines -join ', '))
Write-Output ("Begining deployment to [{0}]" -f ($Machines -join ', '))

if (($null -ne $Password) -and ($null -ne $AdminLogin))
{
Expand Down Expand Up @@ -139,24 +139,34 @@ $scriptBlock = {
# Handle TopShelf service instance name
if ($InstanceName.Length -ne 0)
{
Write-Host "[$env:ComputerName]: Instance Name: [$InstanceName]"
Write-Output "[$env:ComputerName]: Instance Name: [$InstanceName]"
$serviceName = "{0}`${1}" -f $ServiceName.split('$')[0], $InstanceName
}

Write-Host "[$env:ComputerName]: Attempting to locate [$ServiceName]"
Write-Output "[$env:ComputerName]: Attempting to locate [$ServiceName]"
$serviceObject = Get-WSMWindowsService -ServiceName $ServiceName
if ($serviceObject)
{
if($RecreateService)
{
Write-Host "[$env:ComputerName]: Recreate service set to [$RecreateService]"
Write-Host "[$env:ComputerName]: Stopping the service [$ServiceName]"
Write-Output "[$env:ComputerName]: Recreate service set to [$RecreateService]"
Write-Output "[$env:ComputerName]: Stopping the service [$ServiceName]"
$serviceObject = Stop-WSMWindowsService -ServiceName $ServiceName
Write-Host "[$env:ComputerName]: Deleting the service [$ServiceName]"
$deleteResults = $serviceObject.Delete()
Write-Output "[$env:ComputerName]: Deleting the service [$ServiceName]"

try {
$deleteResults = $serviceObject.Delete()
Dejulia489 marked this conversation as resolved.
Show resolved Hide resolved
}
catch {
Write-Error "[$env:ComputerName]: Failing remove Service [$ServiceName]. Used ServiceObject [$serviceObject]"
Write-Error $_.Exception.Message
return
}

if($deleteResults.ReturnValue -eq 0)
{
Write-Host "[$env:ComputerName]: Successfully removed [$ServiceName]"
Write-Output "[$env:ComputerName]: Successfully removed [$ServiceName]"
$serviceObject = $null
}
else
{
Expand All @@ -166,15 +176,15 @@ $scriptBlock = {
}
else
{
Write-Host "[$env:ComputerName]: Unable to locate [$ServiceName]."
Write-Output "[$env:ComputerName]: Unable to locate [$ServiceName]."
}

# Install service
if((-not $serviceObject) -and $InstallService)
{
$parentPath = Get-WSMFullExecuteablePath -StringContainingPath $InstallationPath -JustParentPath
New-WSMServiceDirectory -ParentPath $parentPath
Write-Host "[$env:ComputerName]: Creating service [$ServiceName]."
Write-Output "[$env:ComputerName]: Creating service [$ServiceName]."
if ($InstallTopShelfService)
{
# Binaires needed to complete install for TopShelf
Expand All @@ -197,7 +207,7 @@ $scriptBlock = {
$arguments += $InstallArguments
}

Write-Host "[$env:ComputerName]: Installing topshelf with arguments $arguments"
Write-Output "[$env:ComputerName]: Installing topshelf with arguments $arguments"
& $installationPath $arguments
$freshTopShelfInstall = $true
}
Expand All @@ -222,17 +232,17 @@ $scriptBlock = {
}
if ($runAsCredential)
{
Write-Host "[$env:ComputerName]: Setting RunAsCredentials"
Write-Output "[$env:ComputerName]: Setting RunAsCredentials"
$newServiceSplat.Credential = $runAsCredential
Write-Host "[$env:ComputerName]: Granting [$($runAsCredential.UserName)] logon as a service rights"
Write-Output "[$env:ComputerName]: Granting [$($runAsCredential.UserName)] logon as a service rights"
Add-LocalUserToLogonAsAService -user $runAsCredential.UserName
}
$null = New-Service @newServiceSplat
Write-Host "[$env:ComputerName]: Service [$ServiceName] created."
Write-Output "[$env:ComputerName]: Service [$ServiceName] created."

if ($delayed)
{
Write-Host "[$env:ComputerName]: Set [$ServiceName] to delayed start"
Write-Output "[$env:ComputerName]: Set [$ServiceName] to delayed start"
Start-Process -FilePath sc.exe -ArgumentList "config ""$ServiceName"" start=delayed-auto"
}
}
Expand All @@ -251,14 +261,14 @@ $scriptBlock = {
{
$serviceObject = Stop-WSMWindowsService -ServiceName $ServiceName -Timeout $Timeout -StopProcess:$StopProcess
$parentPath = Get-WSMFullExecuteablePath -StringContainingPath $serviceObject.PathName -JustParentPath
Write-Host "[$env:ComputerName]: Identified [$ServiceName] installation directory [$parentPath]"
Write-Output "[$env:ComputerName]: Identified [$ServiceName] installation directory [$parentPath]"

if (Test-Path $parentPath)
{

if ($CleanInstall)
{
Write-Host "[$env:ComputerName]: Clean install set to [$CleanInstall], removing the contents of [$parentPath]"
Write-Output "[$env:ComputerName]: Clean install set to [$CleanInstall], removing the contents of [$parentPath]"
Invoke-WSMCleanInstall -ParentPath $parentPath -StopProcess $StopProcess -Timeout $TimeOut -ServiceName $ServiceName
}
}
Expand All @@ -280,22 +290,7 @@ $scriptBlock = {
}


if ($Machines)
{
$newPSSessionSPlat = @{
ComputerName = $Machines
SessionOption = $sessionOption
UseSSL = $UseSSL
}
if ($credential)
{
$newPSSessionSPlat.Credential = $credential
}
$sessions = New-PSSession @newPSSessionSPlat
}

$invokeCommandSplat = @{
Session = $sessions
ArgumentList = $ServiceName,
$ServiceDisplayName,
$ServiceDescription,
Expand All @@ -315,8 +310,23 @@ $invokeCommandSplat = @{
$InstallService
}

# Import utility script into session
Invoke-Command @invokeCommandSplat -FilePath $UTILITY_PATH
if ($Machines)
{
$newPSSessionSPlat = @{
ComputerName = $Machines
SessionOption = $sessionOption
UseSSL = $UseSSL
}
if ($credential)
{
$newPSSessionSPlat.Credential = $credential
}
$sessions = New-PSSession @newPSSessionSPlat
$invokeCommandSplat.Session = $sessions

# Import utility script into session
Invoke-Command @invokeCommandSplat -FilePath $UTILITY_PATH
Dejulia489 marked this conversation as resolved.
Show resolved Hide resolved
}

# Invoke script block
Invoke-Command @invokeCommandSplat -ScriptBlock $scriptBlock
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,18 @@ function Add-LocalUserToLogonAsAService
$sidstr = $null
}

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

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

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

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

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

$c = Get-Content -Path $tmp
Expand All @@ -54,7 +54,7 @@ function Add-LocalUserToLogonAsAService

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

if ( [string]::IsNullOrEmpty($currentSetting) )
{
Expand All @@ -65,7 +65,7 @@ function Add-LocalUserToLogonAsAService
$currentSetting = "*$($sidstr),$($currentSetting)"
}

Write-Host "$currentSetting"
Write-Output "$currentSetting"

$outfile = @"
[Unicode]
Expand All @@ -80,7 +80,7 @@ SeServiceLogonRight = $($currentSetting)
$tmp2 = [System.IO.Path]::GetTempFileName()


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

#notepad.exe $tmp2
Expand All @@ -89,7 +89,7 @@ SeServiceLogonRight = $($currentSetting)
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 "
#Write-Output "secedit.exe /configure /db ""secedit.sdb"" /cfg ""$($tmp2)"" /areas USER_RIGHTS "
}
finally
{
Expand All @@ -98,10 +98,10 @@ SeServiceLogonRight = $($currentSetting)
}
else
{
Write-Host "NO ACTIONS REQUIRED! Account already in ""Logon as a Service""" -ForegroundColor DarkCyan
Write-Output "NO ACTIONS REQUIRED! Account already in ""Logon as a Service""" -ForegroundColor DarkCyan
}

Write-Host "Done." -ForegroundColor DarkCyan
Write-Output "Done." -ForegroundColor DarkCyan
}
}

Expand Down Expand Up @@ -140,7 +140,7 @@ function Start-WSMWindowsService
(
$ServiceName
)
Write-Host "[$env:ComputerName]: Starting [$ServiceName]"
Write-Output "[$env:ComputerName]: Starting [$ServiceName]"
$serviceObject = Get-WSMWindowsService -ServiceName $ServiceName
$respone = $serviceObject.StartService()
if ($respone.ReturnValue -ne 0)
Expand All @@ -149,7 +149,7 @@ function Start-WSMWindowsService
}
else
{
Write-Host "[$env:ComputerName]: [$ServiceName] started successfully!"
Write-Output "[$env:ComputerName]: [$ServiceName] started successfully!"
}
}

Expand All @@ -173,7 +173,7 @@ function Stop-WSMWindowsService
if ($serviceObject.State -eq 'Running')
{
$stopServiceTimer = [Diagnostics.Stopwatch]::StartNew()
Write-Host "[$env:ComputerName]: Stopping Service [$ServiceName]"
Write-Output "[$env:ComputerName]: Stopping Service [$ServiceName]"
do
{
$serviceObject = Get-WSMWindowsService -ServiceName $ServiceName
Expand Down Expand Up @@ -204,7 +204,7 @@ function Stop-WSMWindowsService
}
while ($serviceObject.State -ne 'Stopped')

Write-Host "[$env:ComputerName]: Stopped Service [$ServiceName]"
Write-Output "[$env:ComputerName]: Stopped Service [$ServiceName]"
}
return $serviceObject
}
Expand Down Expand Up @@ -250,7 +250,7 @@ function New-WSMServiceDirectory
)
if (-not(Test-Path $ParentPath))
{
Write-Host "[$env:ComputerName]: Creating the service directory at [$ParentPath]."
Write-Output "[$env:ComputerName]: Creating the service directory at [$ParentPath]."
$null = New-Item -Path $ParentPath -ItemType 'Directory' -Force
}
}
Expand All @@ -265,7 +265,7 @@ function Copy-WSMServiceBinaries
[string]
$ParentPath
)
Write-Host "[$env:ComputerName]: Copying [$ArtifactPath] to [$ParentPath]"
Write-Output "[$env:ComputerName]: Copying [$ArtifactPath] to [$ParentPath]"
if ($ArtifactPath.EndsWith('.zip'))
{
Expand-Archive -Path $ArtifactPath -DestinationPath $ParentPath -Force -ErrorAction Stop
Expand Down
2 changes: 1 addition & 1 deletion WindowsServiceManager/WindowsServiceManagerV5/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"version": {
"Major": 5,
"Minor": 0,
"Patch": 1
"Patch": 4
},
"instanceNameFormat": "Deploy Windows Service",
"groups": [
Expand Down
2 changes: 1 addition & 1 deletion vss-extension.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"manifestVersion": 1,
"id": "WindowsServiceManager",
"version": "5.0.3",
"version": "5.0.6",
"name": "Windows Service Manager",
"description": "Installs and deploys a windows service or TopShelf service on a target machine or a deployment group target.",
"publisher": "MDSolutions",
Expand Down